├── .eslintignore
├── .eslintrc
├── .gitignore
├── .prettierrc
├── .vscode
└── launch.json
├── LICENSE
├── README.md
├── babel.config.js
├── browser-sandbox
├── babel.config.js
├── build
│ └── compiled-bundle.js
├── bundle.js
├── compiled-rollup-config.js
├── grayarrow.gif
├── index.html
├── logo.png
├── package.json
├── src
│ ├── App.compiled.js
│ ├── App.js
│ ├── compiled.js
│ └── react.js
├── style.css
└── yarn.lock
├── node-sandbox
├── App.compiled.js
└── App.js
├── package-lock.json
├── package.json
├── scripts
├── compiler-runtime
│ ├── dom-dispatcher.js
│ ├── index.js
│ ├── package.json
│ ├── render.js
│ ├── renderToString.js
│ ├── ssr-dispatcher.js
│ └── utils.js
├── test-externs.js
└── test.js
├── src
├── annotations.js
├── cli.js
├── compiler.js
├── deadcode.js
├── errors.js
├── invariant.js
├── nodes.js
├── react
│ ├── components.js
│ ├── context.js
│ ├── elements.js
│ ├── functions.js
│ ├── prop-information.js
│ └── style.js
├── references.js
├── templates.js
├── transforms.js
├── utils.js
└── validation.js
├── tests
├── benchmarks
│ ├── hacker-news.js
│ ├── hacker-news.json
│ ├── marko-benchmarks
│ │ ├── color-picker.js
│ │ └── color-picker.json
│ ├── pe-functional-components.js
│ └── pe-functional-components2.js
├── create-element
│ ├── array
│ │ ├── array.js
│ │ ├── array2.js
│ │ ├── array3.js
│ │ ├── array4.js
│ │ ├── array5.js
│ │ ├── array6.js
│ │ └── array7.js
│ ├── basic
│ │ ├── basic.js
│ │ ├── basic10.js
│ │ ├── basic11.js
│ │ ├── basic12.js
│ │ ├── basic13.js
│ │ ├── basic14.js
│ │ ├── basic15.js
│ │ ├── basic16.js
│ │ ├── basic17.js
│ │ ├── basic18.js
│ │ ├── basic19.js
│ │ ├── basic2.js
│ │ ├── basic20.js
│ │ ├── basic21.js
│ │ ├── basic22.js
│ │ ├── basic23.js
│ │ ├── basic24.js
│ │ ├── basic25.js
│ │ ├── basic26.js
│ │ ├── basic27.js
│ │ ├── basic28.js
│ │ ├── basic29.js
│ │ ├── basic3.js
│ │ ├── basic30.js
│ │ ├── basic31.js
│ │ ├── basic32.js
│ │ ├── basic33.js
│ │ ├── basic34.js
│ │ ├── basic35.js
│ │ ├── basic36.js
│ │ ├── basic37.js
│ │ ├── basic38.js
│ │ ├── basic39.js
│ │ ├── basic4.js
│ │ ├── basic40.js
│ │ ├── basic41.js
│ │ ├── basic42.js
│ │ ├── basic43.js
│ │ ├── basic44.js
│ │ ├── basic45.js
│ │ ├── basic46.js
│ │ ├── basic47.js
│ │ ├── basic47b.js
│ │ ├── basic48.js
│ │ ├── basic49.js
│ │ ├── basic5.js
│ │ ├── basic50.js
│ │ ├── basic51.js
│ │ ├── basic52.js
│ │ ├── basic53.js
│ │ ├── basic54.js
│ │ ├── basic55.js
│ │ ├── basic56.js
│ │ ├── basic57.js
│ │ ├── basic58.js
│ │ ├── basic59.js
│ │ ├── basic6.js
│ │ ├── basic60.js
│ │ ├── basic61.js
│ │ ├── basic62.js
│ │ ├── basic66.js
│ │ ├── basic67.js
│ │ ├── basic68.js
│ │ ├── basic69.js
│ │ ├── basic7.js
│ │ ├── basic70.js
│ │ ├── basic71.js
│ │ ├── basic72.js
│ │ ├── basic8.js
│ │ └── basic9.js
│ ├── context
│ │ ├── context.js
│ │ ├── context2.js
│ │ ├── context3.js
│ │ ├── context4.js
│ │ ├── context5.js
│ │ ├── context6.js
│ │ ├── context7.js
│ │ ├── context8.js
│ │ └── context9.js
│ ├── hooks
│ │ ├── hooks.js
│ │ ├── hooks2.js
│ │ ├── hooks3.js
│ │ ├── hooks4.js
│ │ └── hooks5.js
│ ├── large
│ │ └── large.js
│ └── style
│ │ ├── style.js
│ │ ├── style2.js
│ │ ├── style3.js
│ │ └── style4.js
├── jsx
│ ├── array
│ │ ├── array.js
│ │ ├── array2.js
│ │ ├── array3.js
│ │ ├── array4.js
│ │ ├── array5.js
│ │ ├── array6.js
│ │ └── array7.js
│ ├── basic
│ │ ├── basic.js
│ │ ├── basic10.js
│ │ ├── basic11.js
│ │ ├── basic12.js
│ │ ├── basic13.js
│ │ ├── basic14.js
│ │ ├── basic15.js
│ │ ├── basic16.js
│ │ ├── basic17.js
│ │ ├── basic18.js
│ │ ├── basic19.js
│ │ ├── basic2.js
│ │ ├── basic20.js
│ │ ├── basic21.js
│ │ ├── basic22.js
│ │ ├── basic23.js
│ │ ├── basic24.js
│ │ ├── basic25.js
│ │ ├── basic26.js
│ │ ├── basic27.js
│ │ ├── basic28.js
│ │ ├── basic29.js
│ │ ├── basic3.js
│ │ ├── basic30.js
│ │ ├── basic31.js
│ │ ├── basic32.js
│ │ ├── basic33.js
│ │ ├── basic34.js
│ │ ├── basic35.js
│ │ ├── basic36.js
│ │ ├── basic37.js
│ │ ├── basic38.js
│ │ ├── basic39.js
│ │ ├── basic4.js
│ │ ├── basic40.js
│ │ ├── basic41.js
│ │ ├── basic42.js
│ │ ├── basic43.js
│ │ ├── basic44.js
│ │ ├── basic45.js
│ │ ├── basic46.js
│ │ ├── basic47.js
│ │ ├── basic47b.js
│ │ ├── basic48.js
│ │ ├── basic49.js
│ │ ├── basic5.js
│ │ ├── basic50.js
│ │ ├── basic51.js
│ │ ├── basic52.js
│ │ ├── basic53.js
│ │ ├── basic54.js
│ │ ├── basic55.js
│ │ ├── basic56.js
│ │ ├── basic57.js
│ │ ├── basic58.js
│ │ ├── basic59.js
│ │ ├── basic6.js
│ │ ├── basic60.js
│ │ ├── basic61.js
│ │ ├── basic62.js
│ │ ├── basic63.js
│ │ ├── basic64.js
│ │ ├── basic64b.js
│ │ ├── basic65.js
│ │ ├── basic65b.js
│ │ ├── basic66.js
│ │ ├── basic67.js
│ │ ├── basic68.js
│ │ ├── basic69.js
│ │ ├── basic7.js
│ │ ├── basic70.js
│ │ ├── basic71.js
│ │ ├── basic72.js
│ │ ├── basic8.js
│ │ └── basic9.js
│ ├── context
│ │ ├── context.js
│ │ ├── context2.js
│ │ ├── context3.js
│ │ ├── context4.js
│ │ ├── context5.js
│ │ ├── context6.js
│ │ ├── context7.js
│ │ ├── context8.js
│ │ └── context9.js
│ ├── hooks
│ │ ├── hooks.js
│ │ ├── hooks2.js
│ │ ├── hooks3.js
│ │ ├── hooks4.js
│ │ └── hooks5.js
│ ├── large
│ │ └── large.js
│ └── style
│ │ ├── style.js
│ │ ├── style2.js
│ │ ├── style3.js
│ │ └── style4.js
└── modules
│ ├── simple-app
│ ├── Header.compiled.js
│ ├── Header.js
│ ├── index.compiled.js
│ └── index.js
│ ├── simple-app2
│ ├── Footer.compiled.js
│ ├── Footer.js
│ ├── Header.compiled.js
│ ├── Header.js
│ ├── index.compiled.js
│ ├── index.js
│ ├── type.js
│ └── utils.js
│ ├── simple-app3
│ ├── Foo.compiled.js
│ ├── Foo.js
│ ├── index.compiled.js
│ ├── index.js
│ ├── utils.compiled.js
│ └── utils.js
│ └── simple-app4
│ ├── Foo.compiled.js
│ ├── Foo.js
│ ├── index.compiled.js
│ ├── index.js
│ ├── utils.compiled.js
│ └── utils.js
└── yarn.lock
/.eslintignore:
--------------------------------------------------------------------------------
1 | lib
2 | flow-typed
3 | tests/**/*.js
4 | babel.config.js
5 | scripts/test-externs.js
6 | browser-sandbox
7 | node-sandbox
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | node_modules
3 | lib/
--------------------------------------------------------------------------------
/.prettierrc:
--------------------------------------------------------------------------------
1 | {
2 | "printWidth": 120,
3 | "trailingComma": "all"
4 | }
--------------------------------------------------------------------------------
/.vscode/launch.json:
--------------------------------------------------------------------------------
1 | {
2 | // Use IntelliSense to learn about possible attributes.
3 | // Hover to view descriptions of existing attributes.
4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5 | "version": "0.2.0",
6 | "configurations": [
7 | {
8 | "type": "node",
9 | "request": "launch",
10 | "name": "Run tests",
11 | "program": "${workspaceFolder}/scripts/test.js",
12 | "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/babel-node",
13 | "args": [
14 | "--mode=dom",
15 | "basic55"
16 | ]
17 | },
18 | {
19 | "type": "node",
20 | "request": "launch",
21 | "name": "Run sandbox",
22 | "program": "${workspaceFolder}/lib/cli.js",
23 | "args": [
24 | "--entry=node-sandbox/App.js",
25 | ]
26 | }
27 | ]
28 | }
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) Facebook, Inc. and its affiliates.
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # react-compiler
2 |
3 | Note: This project is unmaintained and is highly experimental.
4 |
5 | This was a personal side-project to investigate various different implementations of how you might ahead-of-time compile React applications (with contraints in place) in the pursuit of performance and code size wins. The primary reason for open sourcing this code is to share the ideas explored in this project, so others can make better use of them.
6 |
7 | ## Getting started
8 |
9 | Upon cloning the repository, run
10 |
11 | ```sh
12 | npm install && npm run build
13 | ```
14 |
15 | If you use VSCode, you'll find launch options available for compiling the `node-sandbox` directory.
16 |
17 | ### SSR
18 |
19 | You can see quick working examples in the `tests` directory. If you copy them into `node-sandbox/App.js` and then run:
20 |
21 | ```
22 | node lib/cli.js --entry=node-sandbox/App.js
23 | ```
24 |
25 | You should see the compiled output in `node-sandbox/App.compiled.js`.
26 |
27 | The compiler doesn't use the React/ReactDOM runtime. Instead it has its own runtime that only supports a handful of React
28 | APIs. The node runtime (which generates HTML) can be found in `scripts/compiler-runtime`.
29 |
30 | ### DOM
31 |
32 | There's a small DOM runtime that shows the basics working. This can be found `browser-sandbox`. If you want to make changes,
33 | you can modify the starting entry point in `browser-sandbox/compiled.js`. To build the changes, make sure you run `yarn build` from within the `browser-sandbox` directory.
34 |
35 | Lastly, open `browser-sandbox/index.html` in your browser to see the output.
36 |
37 | ## Tests
38 |
39 | Run tests:
40 |
41 | `yarn test`
42 |
43 | Run a test:
44 |
45 | `yarn test basic1.js`
46 |
47 | Run tests and see minified Google Closure Compiler size comparisons:
48 |
49 | `yarn test --size`
50 |
51 | Run benchmark tests and see the performance differences (10 warmups before metrics are captured):
52 |
53 | `yarn test --benchmark`
54 |
55 | Run benchmark tests and see the performance differences and minified size comparisons (10 warmups before metrics are captured):
56 |
57 | `yarn test --benchmark --size`
58 |
--------------------------------------------------------------------------------
/babel.config.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-present, Facebook, Inc.
3 | * All rights reserved.
4 | *
5 | * This source code is licensed under the BSD-style license found in the
6 | * LICENSE file in the root directory of this source tree. An additional grant
7 | * of patent rights can be found in the PATENTS file in the same directory.
8 | */
9 |
10 | module.exports = function(api) {
11 | api.cache(true);
12 |
13 | const plugins = [
14 | "@babel/plugin-syntax-flow",
15 | "@babel/plugin-syntax-jsx",
16 | "@babel/plugin-transform-flow-strip-types",
17 | ];
18 |
19 | // Default
20 | return {
21 | include: [/node_modules/, /scripts/, /src/],
22 | ignore: [],
23 | presets: [
24 | [
25 | "@babel/env",
26 | {
27 | targets: {
28 | node: "11.1",
29 | },
30 | },
31 | ],
32 | "@babel/preset-flow",
33 | ],
34 | plugins,
35 | };
36 | };
--------------------------------------------------------------------------------
/browser-sandbox/babel.config.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-present, Facebook, Inc.
3 | * All rights reserved.
4 | *
5 | * This source code is licensed under the BSD-style license found in the
6 | * LICENSE file in the root directory of this source tree. An additional grant
7 | * of patent rights can be found in the PATENTS file in the same directory.
8 | */
9 |
10 | module.exports = function(api) {
11 | api.cache(true);
12 |
13 | const plugins = [
14 | "@babel/plugin-syntax-flow",
15 | "@babel/plugin-syntax-jsx",
16 | "@babel/plugin-transform-flow-strip-types",
17 | ];
18 |
19 | // Default
20 | return {
21 | presets: [
22 | [
23 | "@babel/env",
24 | {
25 | targets: {
26 | node: "11.1",
27 | },
28 | },
29 | ],
30 | "@babel/preset-flow",
31 | ],
32 | plugins,
33 | };
34 | };
--------------------------------------------------------------------------------
/browser-sandbox/compiled-rollup-config.js:
--------------------------------------------------------------------------------
1 | import commonjs from "rollup-plugin-commonjs";
2 | import nodeResolve from "rollup-plugin-node-resolve";
3 | import replace from "rollup-plugin-replace";
4 | import babel from "rollup-plugin-babel";
5 | import closure from 'rollup-plugin-google-closure-compiler';
6 |
7 | export default {
8 | input: "src/compiled.js",
9 | output: {
10 | file: "build/compiled-bundle.js",
11 | format: "umd",
12 | name: "App",
13 | },
14 | plugins: [
15 | replace({
16 | "process.env.NODE_ENV": JSON.stringify("production"),
17 | }),
18 | babel({
19 | presets: [
20 | [
21 | "@babel/preset-env",
22 | {
23 | loose: true,
24 | modules: false,
25 | targets: {
26 | ie: "11",
27 | },
28 | },
29 | ],
30 | "@babel/preset-react",
31 | ],
32 | }),
33 | nodeResolve({
34 | jsnext: true,
35 | main: true,
36 | }),
37 | commonjs({
38 | ignore: ["react"],
39 | }),
40 | closure({
41 | language_in: 'ECMASCRIPT5_STRICT',
42 | language_out: 'ECMASCRIPT5_STRICT',
43 | }),
44 | ],
45 | };
--------------------------------------------------------------------------------
/browser-sandbox/grayarrow.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/trueadm/react-compiler/d2af62c097e154a5c5fbfddef545fe61bf629d88/browser-sandbox/grayarrow.gif
--------------------------------------------------------------------------------
/browser-sandbox/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 | Browser Sandbox (React Compiled)
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/browser-sandbox/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/trueadm/react-compiler/d2af62c097e154a5c5fbfddef545fe61bf629d88/browser-sandbox/logo.png
--------------------------------------------------------------------------------
/browser-sandbox/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "version": "0.0.1",
3 | "description": "Browser sandbox for the React compiler",
4 | "scripts": {
5 | "compile": "node ../lib/cli.js --entry=src/App.js",
6 | "build-compiled-bundle": "rollup --config compiled-rollup-config.js",
7 | "build": "yarn compile && yarn build-compiled-bundle"
8 | },
9 | "author": "Dominic Gannaway",
10 | "license": "MIT",
11 | "devDependencies": {
12 | "@babel/plugin-syntax-flow": "^7.2.0",
13 | "@babel/plugin-syntax-jsx": "^7.2.0",
14 | "@babel/plugin-transform-flow-strip-types": "^7.2.3",
15 | "@babel/preset-env": "^7.2.3",
16 | "@babel/preset-flow": "^7.0.0",
17 | "react": "^16.13.1",
18 | "react-compiler-runtime": "file:../scripts/compiler-runtime/",
19 | "react-dom": "^16.13.1",
20 | "rollup": "^1.0.1",
21 | "rollup-plugin-babel": "^4.3.0",
22 | "rollup-plugin-commonjs": "^9.2.0",
23 | "rollup-plugin-google-closure-compiler": "^0.1.0",
24 | "rollup-plugin-node-resolve": "^4.0.0",
25 | "rollup-plugin-replace": "^2.1.0",
26 | "rollup-plugin-svelte": "^5.0.1"
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/browser-sandbox/src/App.compiled.js:
--------------------------------------------------------------------------------
1 | import { createVNode } from "react-compiler-runtime";
2 | type StoryType = {
3 | by: string,
4 | descendants: number,
5 | id: number,
6 | kids: Array,
7 | score: number,
8 | time: number,
9 | title: string,
10 | type: string,
11 | url: string,
12 | };
13 |
14 | function timeAge(time): string {
15 | const now = new Date()["getTime"]() / 1000;
16 | const minutes = (now - time) / 60;
17 |
18 | if (minutes < 60) {
19 | return Math["round"](minutes) + " minutes ago";
20 | }
21 |
22 | return Math["round"](minutes / 60) + " hours ago";
23 | }
24 |
25 | function getHostUrl(url): string {
26 | return (url + "")["replace"]("https://", "")["replace"]("http://", "")["split"]("/")[0];
27 | }
28 |
29 | function Image_ComputeFunction(height, src, style, width) {
30 | return [src, width, height, style["border"]];
31 | }
32 |
33 | function Link_ComputeFunction(children, className, href) {
34 | return [href || "#", className, children];
35 | }
36 |
37 | function HeaderBar_ComputeFunction() {
38 | return [[createVNode(__vnode_tpl_0), void 0, void 0], ["new", void 0, void 0], ["comments", void 0, void 0], ["show", void 0, void 0], ["ask", void 0, void 0], ["jobs", void 0, void 0], ["submit", void 0, void 0]];
39 | }
40 |
41 | function Story_ComputeFunction(rank, story) {
42 | timeAge(story["time"]);
43 | getHostUrl(story["url"]);
44 | return [rank, [createVNode(__vnode_tpl_1), void 0, void 0], [story["title"], "storylink", void 0], story["url"], [getHostUrl(story["url"]), void 0, void 0], story["score"], [story["by"], "hnuser", void 0], [timeAge(story["time"]), void 0, void 0], ["hide", void 0, void 0], [[story["descendants"] || 0, " comments"], void 0, void 0]];
45 | }
46 |
47 | function StoryList_ComputeFunction(stories) {
48 | return [stories["map"]((story: Story, i: number): React.Node => createVNode([12, Story, 0], [[++i, story]], null))];
49 | }
50 |
51 | function Component_ComputeFunction(stories) {
52 | return [[stories]];
53 | }
54 |
55 | function Link() {
56 | return [1, Link_ComputeFunction, [1154, "a", ["href", 0, 0, "className", 0, 1], [13, 2]]];
57 | }
58 |
59 | const Image = [1, Image_ComputeFunction, [262786, "img", ["src", 0, 0, "width", 0, 1, "height", 0, 2], ["border", 3]]];
60 | const HeaderBar = [1, HeaderBar_ComputeFunction, [1282, "tr", ["background-color", "#222"], [1026, "td", [1346, "table", ["width", 0, "100%", "cellSpacing", 0, "0", "cellPadding", 0, "0"], ["padding", "4px"], [1026, "tbody", [2050, "tr", [[1282, "td", ["width", "18px", "padding-right", "4px"], [12, Link, 0]], [1346, "td", ["height", 0, "10"], ["line-height", "12pt"], [2114, "span", ["className", 0, "pagetop"], [[4162, "b", ["className", 0, "hnname"], "React HN Benchmark"], [12, Link, 1], [32771, " | "], [12, Link, 2], [32771, " | "], [12, Link, 3], [32771, " | "], [12, Link, 4], [32771, " | "], [12, Link, 5], [32771, " | "], [12, Link, 6]]]]]]]]]]];
61 | const Story = [1, Story_ComputeFunction, [5, [[2114, "tr", ["className", 0, "athing"], [[1346, "td", ["className", 0, "title"], ["vertical-align", "top", "text-align", "right"], [2114, "span", ["className", 0, "rank"], [[3, 0], [32771, "."]]]], [1346, "td", ["className", 0, "votelinks"], ["vertical-align", "top"], [1026, "center", [12, Link, 1]]], [2114, "td", ["className", 0, "title"], [[12, Link, 2], [6, 3, [2114, "span", ["className", 0, "sitebit comhead"], [[32771, " "], [32771, "("], [12, Link, 4], [32771, ")"]]], [32772, null]]]]]], [2050, "tr", [[66, "td", ["colSpan", 0, "2"]], [2114, "td", ["className", 0, "subtext"], [[2114, "span", ["className", 0, "score"], [[3, 5], [32771, " points"]]], [32771, " by "], [12, Link, 6], [32771, " "], [1090, "span", ["className", 0, "age"], [12, Link, 7]], [32771, " | "], [12, Link, 8], [32771, " | "], [12, Link, 9]]]]], [66, "tr", ["className", 0, "spacer", "height", 0, "5"]]]]];
62 | const StoryList = [1, StoryList_ComputeFunction, [1026, "tr", [1026, "td", [1090, "table", ["cellPadding", 0, "0", "cellSpacing", 0, "0", "classList", 0, "itemlist"], [1026, "tbody", [11, 0]]]]]];
63 | const __vnode_tpl_0 = [76, Image, ["16", "logo.png", {
64 | ["border"]: "1px solid #00d8ff"
65 | }, "16"]];
66 | const __vnode_tpl_1 = [66, "div", ["className", 0, "votearrow", "title", 0, "upvote"]];
67 | export const Component = [0, ["stories"], Component_ComputeFunction, [1026, "center", [1346, "table", ["id", 0, "hnmain", "border", 0, "0", "cellPadding", 0, "0", "cellSpacing", 0, "0", "width", 0, "85%"], ["background-color", "#f6f6ef"], [2050, "tbody", [[76, HeaderBar, []], [66, "tr", ["height", 0, "10"]], [12, StoryList, 0]]]]]];
--------------------------------------------------------------------------------
/browser-sandbox/src/App.js:
--------------------------------------------------------------------------------
1 | import React from "react";
2 |
3 | type StoryType = {
4 | by: string,
5 | descendants: number,
6 | id: number,
7 | kids: Array,
8 | score: number,
9 | time: number,
10 | title: string,
11 | type: string,
12 | url: string,
13 | };
14 |
15 | function timeAge(time): string {
16 | const now = new Date().getTime() / 1000;
17 | const minutes = (now - time) / 60;
18 |
19 | if (minutes < 60) {
20 | return Math.round(minutes) + " minutes ago";
21 | }
22 | return Math.round(minutes / 60) + " hours ago";
23 | }
24 |
25 | function getHostUrl(url): string {
26 | return (url + "")
27 | .replace("https://", "")
28 | .replace("http://", "")
29 | .split("/")[0];
30 | }
31 |
32 | function Image({
33 | src,
34 | style,
35 | width,
36 | height,
37 | }: {
38 | src: string,
39 | style: { border: string },
40 | width: string,
41 | height: string,
42 | }) {
43 | return
;
44 | }
45 |
46 | function Link({
47 | children,
48 | href,
49 | className,
50 | }: {
51 | children: string | React.Node,
52 | href: string | void,
53 | className: void | string,
54 | }) {
55 | return (
56 |
57 | {children}
58 |
59 | );
60 | }
61 |
62 | function HeaderBar() {
63 | return (
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 | |
74 |
75 |
76 | React HN Benchmark
77 | new
78 | {" | "}
79 | comments
80 | {" | "}
81 | show
82 | {" | "}
83 | ask
84 | {" | "}
85 | jobs
86 | {" | "}
87 | submit
88 |
89 | |
90 |
91 |
92 |
93 | |
94 |
95 | );
96 | }
97 |
98 | function Story({ story, rank }: { story: StoryType, rank: number }) {
99 | return (
100 | <>
101 |
102 |
103 | {rank}.
104 | |
105 |
106 |
107 |
108 |
109 |
110 |
111 | |
112 |
113 | {story.title}
114 | {story.url ? (
115 |
116 | {" "}
117 | ({getHostUrl(story.url)})
118 |
119 | ) : null}
120 | |
121 |
122 |
123 | |
124 |
125 | {story.score} points
126 | {" by "}
127 | {story.by}{" "}
128 |
129 | {timeAge(story.time)}
130 |
131 | {" | "}
132 | hide
133 | {" | "}
134 | {story.descendants || 0} comments
135 | |
136 |
137 |
138 | >
139 | );
140 | }
141 |
142 | function StoryList({ stories }: { stories: Array }) {
143 | return (
144 |
145 |
146 |
147 |
148 | {stories.map(
149 | (story: Story, i: number): React.Node => (
150 |
151 | ),
152 | )}
153 |
154 |
155 | |
156 |
157 | );
158 | }
159 |
160 | export function Component({ stories }: { stories: Array }) {
161 | return (
162 |
163 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 | );
179 | }
180 |
181 | Component.compileRootComponent = true;
182 |
--------------------------------------------------------------------------------
/browser-sandbox/style.css:
--------------------------------------------------------------------------------
1 | body {
2 | font-family: Verdana, Geneva, sans-serif
3 | }
4 |
5 | .pagetop {
6 | font-family: Verdana, Geneva, sans-serif;
7 | font-size: 10pt;
8 | color: #00d8ff;
9 | }
10 |
11 | .hnname {
12 | margin-right: 10px;
13 | color: #fff;
14 | }
15 |
16 | .pagetop a, .pagetop a:visited {
17 | color: #00d8ff;
18 | text-decoration: none;
19 | }
20 |
21 | .title {
22 | font-family: Verdana, Geneva, sans-serif;
23 | font-size: 10pt;
24 | color: #828282;
25 | }
26 |
27 | .subtext {
28 | font-family: Verdana, Geneva, sans-serif;
29 | font-size: 7pt;
30 | color: #828282;
31 | }
32 |
33 | .comhead a:link, .subtext a, .subtext a:visited {
34 | color: #828282;
35 | text-decoration: none;
36 | }
37 |
38 | .votearrow {
39 | width: 10px;
40 | height: 10px;
41 | border: 0px;
42 | margin: 3px 2px 6px;
43 | background: url(grayarrow.gif) no-repeat;
44 | }
45 |
46 | .title, .title a {
47 | font-family: Verdana, Geneva, sans-serif;
48 | font-size: 10pt;
49 | color: #000;
50 | text-decoration: none;
51 | }
52 |
53 | .comhead, .comhead a {
54 | font-family: Verdana, Geneva, sans-serif;
55 | font-size: 8pt;
56 | color: #828282;
57 | }
--------------------------------------------------------------------------------
/node-sandbox/App.compiled.js:
--------------------------------------------------------------------------------
1 | const Component = [98304, [102402, "div", "Hello world"]];
2 | module["exports"] = Component;
--------------------------------------------------------------------------------
/node-sandbox/App.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component() {
4 | return Hello world
5 | }
6 |
7 | Component.compileRootComponent = true;
8 |
9 | module.exports = Component;
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "react-compiler",
3 | "version": "1.0.0",
4 | "description": "React compiler project",
5 | "main": "index.js",
6 | "scripts": {
7 | "build": "babel src --out-dir lib --source-maps",
8 | "watch": "babel src scripts --out-dir lib --watch --source-maps --verbose",
9 | "test-ssr": "babel-node --expose-gc scripts/test.js --mode=ssr",
10 | "test-dom": "babel-node --expose-gc scripts/test.js --mode=dom",
11 | "test": "yarn test-ssr",
12 | "sandbox": "node lib/cli.js --entry=node-sandbox/App.js"
13 | },
14 | "author": "Dominic Gannaway",
15 | "license": "MIT",
16 | "dependencies": {
17 | "@babel/core": "^7.1.5",
18 | "@babel/generator": "^7.1.6",
19 | "@babel/parser": "^7.1.6",
20 | "@babel/preset-env": "^7.1.6",
21 | "@babel/traverse": "^7.1.6",
22 | "@babel/types": "^7.1.5",
23 | "chalk": "^2.4.1"
24 | },
25 | "devDependencies": {
26 | "@babel/cli": "^7.1.5",
27 | "@babel/node": "^7.2.2",
28 | "@babel/plugin-proposal-object-rest-spread": "^7.0.0",
29 | "@babel/plugin-transform-flow-strip-types": "^7.0.0",
30 | "@babel/plugin-transform-modules-commonjs": "^7.1.0",
31 | "@babel/plugin-transform-react-jsx": "^7.1.6",
32 | "@babel/preset-flow": "^7.0.0",
33 | "@babel/preset-react": "^7.0.0",
34 | "@babel/register": "^7.0.0",
35 | "babel-eslint": "^10.0.1",
36 | "eslint": "^5.9.0",
37 | "eslint-plugin-babel": "^5.2.1",
38 | "eslint-plugin-flow-header": "^0.2.0",
39 | "eslint-plugin-flowtype": "^3.2.0",
40 | "eslint-plugin-header": "^2.0.0",
41 | "eslint-plugin-prettier": "^3.0.0",
42 | "glob": "^7.1.3",
43 | "google-closure-compiler": "^20190121.0.0",
44 | "jsdom": "^13.0.0",
45 | "prettier": "^1.15.2",
46 | "react": "^16.8.0",
47 | "react-compiler-runtime": "file:scripts/compiler-runtime",
48 | "react-dom": "^16.8.0",
49 | "simple-html-dom-parser": "^1.1.2",
50 | "tmp": "^0.0.33"
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/scripts/compiler-runtime/index.js:
--------------------------------------------------------------------------------
1 | "use strict";
2 |
3 | const REACT_CONTEXT_TYPE = Symbol.for("react.context");
4 | const REACT_PROVIDER_TYPE = Symbol.for("react.context");
5 |
6 | export const currentDispatcher = {
7 | current: null,
8 | };
9 |
10 | function resolveDispatcher() {
11 | const dispatcher = currentDispatcher.current;
12 | if (dispatcher === null) {
13 | throw new Error("Hooks can only be called inside the body of a function component.");
14 | }
15 | return dispatcher;
16 | }
17 |
18 | export function useState(initialState) {
19 | const dispatcher = resolveDispatcher();
20 | return dispatcher.useState(initialState);
21 | }
22 |
23 | export function useReducer(reducer, initialState, initialAction) {
24 | const dispatcher = resolveDispatcher();
25 | return dispatcher.useReducer(reducer, initialState, initialAction);
26 | }
27 |
28 | export function createContext(defaultValue, calculateChangedBits) {
29 | if (calculateChangedBits === undefined) {
30 | calculateChangedBits = null;
31 | }
32 | const context = {
33 | $$typeof: REACT_CONTEXT_TYPE,
34 | _calculateChangedBits: calculateChangedBits,
35 | // As a workaround to support multiple concurrent renderers, we categorize
36 | // some renderers as primary and others as secondary. We only expect
37 | // there to be two concurrent renderers at most: React Native (primary) and
38 | // Fabric (secondary); React DOM (primary) and React ART (secondary).
39 | // Secondary renderers store their context values on separate fields.
40 | _currentValue: defaultValue,
41 | _currentValue2: defaultValue,
42 | // Used to track how many concurrent renderers this context currently
43 | // supports within in a single renderer. Such as parallel server rendering.
44 | _threadCount: 0,
45 | // These are circular
46 | Provider: null,
47 | Consumer: null,
48 | };
49 | context.Provider = {
50 | $$typeof: REACT_PROVIDER_TYPE,
51 | _context: context,
52 | };
53 | context.Consumer = context;
54 | return context;
55 | }
56 |
57 | export function createVNode(t, v, k) {
58 | return {
59 | f: null,
60 | k: k === undefined ? null : k,
61 | t,
62 | v: v || null,
63 | };
64 | }
65 |
--------------------------------------------------------------------------------
/scripts/compiler-runtime/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "react-compuler-runtime",
3 | "version": "1.0.0",
4 | "description": "",
5 | "main": "index.js",
6 | "author": "Dominic Gannaway",
7 | "license": "MIT",
8 | "dependencies": {
9 | "react": "^16.7.0-alpha.0"
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/scripts/compiler-runtime/ssr-dispatcher.js:
--------------------------------------------------------------------------------
1 | let firstWorkInProgressHook = null;
2 | let currentlyRenderingComputeFunction = null;
3 | let isReRender = false;
4 | let workInProgressHook = null;
5 |
6 | function resolveCurrentlyRenderingFiber() {
7 | if (currentlyRenderingComputeFunction === null) {
8 | throw new Error("Hooks can only be called inside the body of a function component.");
9 | }
10 | return currentlyRenderingComputeFunction;
11 | }
12 |
13 | function createHook() {
14 | return {
15 | memoizedState: null,
16 | queue: null,
17 | next: null,
18 | };
19 | }
20 |
21 | function createWorkInProgressHook() {
22 | if (workInProgressHook === null) {
23 | // This is the first hook in the list
24 | if (firstWorkInProgressHook === null) {
25 | isReRender = false;
26 | firstWorkInProgressHook = workInProgressHook = createHook();
27 | } else {
28 | // There's already a work-in-progress. Reuse it.
29 | isReRender = true;
30 | workInProgressHook = firstWorkInProgressHook;
31 | }
32 | } else {
33 | if (workInProgressHook.next === null) {
34 | isReRender = false;
35 | // Append to the end of the list
36 | workInProgressHook = workInProgressHook.next = createHook();
37 | } else {
38 | // There's already a work-in-progress. Reuse it.
39 | isReRender = true;
40 | workInProgressHook = workInProgressHook.next;
41 | }
42 | }
43 | return workInProgressHook;
44 | }
45 |
46 | function basicStateReducer(state, action) {
47 | return typeof action === "function" ? action(state) : action;
48 | }
49 |
50 | function useReducer(reducer, initialState, initialAction) {
51 | resolveCurrentlyRenderingFiber();
52 | workInProgressHook = createWorkInProgressHook();
53 | if (isReRender) {
54 | throw new Error("TODO");
55 | } else {
56 | if (reducer === basicStateReducer) {
57 | // Special case for `useState`.
58 | if (typeof initialState === "function") {
59 | initialState = initialState();
60 | }
61 | } else if (initialAction !== undefined && initialAction !== null) {
62 | initialState = reducer(initialState, initialAction);
63 | }
64 | workInProgressHook.memoizedState = initialState;
65 | const dispatch = () => {
66 | throw new Error("TODO");
67 | };
68 | return [workInProgressHook.memoizedState, dispatch];
69 | }
70 | }
71 |
72 | function useState(initialState) {
73 | return useReducer(basicStateReducer, initialState);
74 | }
75 |
76 | export function prepareToUseHooks(currentComputeFunction) {
77 | currentlyRenderingComputeFunction = currentComputeFunction;
78 | }
79 |
80 | export function finishHooks() {
81 | firstWorkInProgressHook = null;
82 | currentlyRenderingComputeFunction = null;
83 | isReRender = false;
84 | workInProgressHook = null;
85 | }
86 |
87 | export const Dispatcher = {
88 | useReducer,
89 | useState,
90 | };
91 |
--------------------------------------------------------------------------------
/scripts/test-externs.js:
--------------------------------------------------------------------------------
1 | // This is an externs file used for Google Closure Compiler advanced mode
2 | var module = {};
3 | var document = {};
4 | function require() {}
--------------------------------------------------------------------------------
/src/cli.js:
--------------------------------------------------------------------------------
1 | import { compileEntryModuleFileToDirectory } from "./compiler";
2 | import { resolve } from "path";
3 | import minimist from "minimist";
4 |
5 | const argv = minimist(process.argv.slice(2));
6 | const entry = argv.entry;
7 | const options = {};
8 |
9 | if (entry !== undefined) {
10 | try {
11 | compileEntryModuleFileToDirectory(resolve(entry), options);
12 | } catch (e) {
13 | console.error(e.stack);
14 | process.exit(1);
15 | }
16 | } else {
17 | console.error(
18 | `The compiler CLI requires an --entry argument.\n\n1: The entry must be the path to a root React component that you want to compile.\n\nFor example: "react-compiler --entry=src/MyApp.js"\n`,
19 | );
20 | process.exit(1);
21 | }
22 |
--------------------------------------------------------------------------------
/src/deadcode.js:
--------------------------------------------------------------------------------
1 | import {
2 | isNodeWithinReactElementTemplate,
3 | removePath,
4 | wasParentPathFlaggedWithDCE,
5 | wasParentPathRemoved,
6 | isReactObject,
7 | } from "./utils";
8 | import traverse from "@babel/traverse";
9 | import * as t from "@babel/types";
10 |
11 | function filterReferencePaths(path, state) {
12 | // If the node is used, then we need this path
13 | if (path.node.used) {
14 | return true;
15 | }
16 | if (path.node.canDCE) {
17 | return false;
18 | }
19 | const parentPath = path.parentPath;
20 | if (
21 | t.isMemberExpression(parentPath.node) &&
22 | (isReactObject(parentPath.get("object"), state) &&
23 | ((t.isIdentifier(parentPath.node.property) && parentPath.node.property.name === "createElement") ||
24 | (t.isStringLiteral(parentPath.node.property) && parentPath.node.property.value === "createElement")))
25 | ) {
26 | return false;
27 | }
28 | // Ignore React.Node types referencing React
29 | if (t.isQualifiedTypeIdentifier(path.container)) {
30 | return false;
31 | }
32 | if (isNodeWithinReactElementTemplate(path, state)) {
33 | return false;
34 | }
35 | if (wasParentPathRemoved(path)) {
36 | return false;
37 | }
38 | if (wasParentPathFlaggedWithDCE(path)) {
39 | return false;
40 | }
41 | return true;
42 | }
43 |
44 | // This isn't a thorough dead code removal tool. It's designed to complement
45 | // other tools such as Google Closure Compiler, so they can do a better job.
46 | export function applyDeadCodeElimination(moduleAst, moduleState) {
47 | const deadCodePasses = 3;
48 | for (let i = 0; i < deadCodePasses; i++) {
49 | // Second pass, remove unused identifiers and convert member expressions to computed
50 | traverse(moduleAst, {
51 | FunctionExpression(path) {
52 | // Remove dangling expressions that aren't referenced or used by anything.
53 | // This can occur because of the logic in createPropTemplateForFunctionExpression
54 | if (
55 | path.node.id === null &&
56 | t.isExpressionStatement(path.parentPath) &&
57 | t.isBlockStatement(path.parentPath.parentPath)
58 | ) {
59 | removePath(path);
60 | }
61 | if (path.node && path.node.id === null && path.node.canDCE) {
62 | removePath(path);
63 | }
64 | },
65 | CallExpression(path) {
66 | if (path.node.canDCE) {
67 | removePath(path);
68 | }
69 | },
70 | Identifier(path) {
71 | const node = path.node;
72 |
73 | // Flag we set to ensure we don't DCE this identifier
74 | if (node.used === true) {
75 | return;
76 | }
77 | if (node === undefined) {
78 | return;
79 | }
80 | if (moduleState.externalBindings.has(node.name)) {
81 | return;
82 | }
83 |
84 | const binding = path.scope.getBinding(node.name);
85 | if (binding === undefined) {
86 | return;
87 | }
88 | // Don't attempt to remove function params right now.
89 | if (binding.kind === "param") {
90 | return;
91 | }
92 | // If this is the id of a function, skip dead coding it.
93 | if (path.key === "id" && t.isFunctionDeclaration(path.parentPath.node)) {
94 | return;
95 | }
96 | const filteredReferencePaths = binding.referencePaths.filter(p => filterReferencePaths(p, moduleState));
97 | if (filteredReferencePaths.length === 0) {
98 | node.toBeRemoved = true;
99 | // Exclude object pattern for now
100 | if (!t.isObjectPattern(binding.path.node)) {
101 | traverse(moduleAst, {
102 | AssignmentExpression(assignPath) {
103 | const assignmentNode = assignPath.node;
104 | if (t.isIdentifier(assignmentNode.left) && assignmentNode.left.name === node.name) {
105 | removePath(assignPath);
106 | }
107 | },
108 | });
109 | moduleState.applyPostTransform(() => {
110 | removePath(path);
111 | });
112 | }
113 | }
114 | },
115 | });
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/src/errors.js:
--------------------------------------------------------------------------------
1 | // This error is thrown when a false invariant is encountered. This error should never be swallowed.
2 | export class InvariantError extends Error {
3 | constructor(message: string) {
4 | super(message);
5 | }
6 | }
7 |
--------------------------------------------------------------------------------
/src/invariant.js:
--------------------------------------------------------------------------------
1 | import { InvariantError } from "./errors";
2 |
3 | export default function invariant(condition: boolean, format: string = ""): void {
4 | if (condition) return;
5 | const message = `${format}
6 | This is likely a bug or missing functionality in the compiler, not your code.`;
7 | let error = new InvariantError(message);
8 | error.name = "Invariant Violation";
9 | throw error;
10 | }
11 |
--------------------------------------------------------------------------------
/src/react/context.js:
--------------------------------------------------------------------------------
1 | import { getReferenceFromExpression, isDestructuredRef, isIdentifierReferenceConstant } from "../references";
2 | import {
3 | getCodeLocation,
4 | getRuntimeValueIndex,
5 | handleWhiteSpace,
6 | isHostComponentType,
7 | isReactCreateElement,
8 | isReactObject,
9 | markNodeAsUsed,
10 | } from "../utils";
11 | import { compileReactComputeFunction } from "./functions";
12 | import * as t from "@babel/types";
13 | import { ContextConsumerTemplateNode } from "../templates";
14 |
15 | export function compileReactContextConsumer(namePath, childrenPath, state, componentPath) {
16 | const filteredChildrenPath = childrenPath.filter(childPath => {
17 | if (t.isJSXText(childPath.node) && handleWhiteSpace(childPath.node.value) === "") {
18 | return false;
19 | }
20 | return true;
21 | });
22 | if (filteredChildrenPath.length !== 1) {
23 | throw new Error(
24 | `Compiler failed to find a single child function for at ${getCodeLocation(childrenPath.node)}`,
25 | );
26 | }
27 | const contextObjectValueIndex = getContextObjectRuntimeValueIndex(namePath, state);
28 | const childrenPathRef = getReferenceFromExpression(filteredChildrenPath[0], state);
29 |
30 | if (t.isIdentifier(childrenPathRef.node) && !isIdentifierReferenceConstant(childrenPathRef, state)) {
31 | throw new Error(
32 | `The compiler is not yet able to evaluate conditional references at ${getCodeLocation(
33 | childrenPathRef.node,
34 | )}.`,
35 | );
36 | }
37 | if (t.isCallExpression(childrenPathRef.node)) {
38 | throw new Error(
39 | `The compiler is not yet able to evaluate references to call expressions at ${getCodeLocation(
40 | childrenPathRef.node,
41 | )}.`,
42 | );
43 | }
44 | const { isStatic, templateNode } = compileReactComputeFunction(childrenPathRef, state, false, false);
45 | if (!isStatic) {
46 | const parentNode = childrenPathRef.parentPath.node;
47 | let computeFunctionValueIndex;
48 |
49 | if (t.isJSXExpressionContainer(parentNode) || isReactCreateElement(childrenPathRef.parentPath, state)) {
50 | computeFunctionValueIndex = getRuntimeValueIndex(childrenPathRef.node, state);
51 | } else if (t.isVariableDeclarator(parentNode) && t.isIdentifier(parentNode.id)) {
52 | computeFunctionValueIndex = getRuntimeValueIndex(parentNode.id, state);
53 | markNodeAsUsed(parentNode.id);
54 | } else {
55 | throw new Error("TODO");
56 | }
57 | return new ContextConsumerTemplateNode(contextObjectValueIndex, templateNode, computeFunctionValueIndex);
58 | }
59 | return templateNode;
60 | }
61 |
62 | function isReferenceReactContext(path, state) {
63 | const pathRef = getReferenceFromExpression(path, state);
64 | const objectNode = pathRef.node;
65 |
66 | if (t.isCallExpression(objectNode)) {
67 | const calleePath = pathRef.get("callee");
68 | const calleePathNode = calleePath.node;
69 |
70 | if (t.isMemberExpression(calleePathNode)) {
71 | const memberObjectPath = calleePath.get("object");
72 |
73 | if (
74 | isReactObject(memberObjectPath, state) &&
75 | t.isIdentifier(calleePathNode.property) &&
76 | calleePathNode.property.name === "createContext"
77 | ) {
78 | return true;
79 | }
80 | }
81 | }
82 | return false;
83 | }
84 |
85 | function isReferenceReactContextConsumerOrProvider(path, name, state) {
86 | const node = path.node;
87 |
88 | if (
89 | (t.isJSXMemberExpression(node) || t.isMemberExpression(node)) &&
90 | (t.isJSXIdentifier(node.property) || t.isIdentifier(node.property)) &&
91 | node.property.name === name
92 | ) {
93 | const objectPath = path.get("object");
94 |
95 | return isReferenceReactContext(objectPath, state);
96 | } else if ((t.isJSXIdentifier(node) || t.isIdentifier(node)) && !isHostComponentType(path, state)) {
97 | let pathRef = getReferenceFromExpression(path, state);
98 |
99 | if (isDestructuredRef(pathRef)) {
100 | pathRef = pathRef.object;
101 | }
102 | const pathRefNode = pathRef.node;
103 |
104 | if (
105 | t.isMemberExpression(pathRefNode) &&
106 | t.isIdentifier(pathRefNode.property) &&
107 | pathRefNode.property.name === name
108 | ) {
109 | const objectPath = pathRef.get("object");
110 | return isReferenceReactContext(objectPath, state);
111 | } else if (t.isCallExpression(pathRefNode)) {
112 | return isReferenceReactContext(pathRef, state) && node.name === name;
113 | }
114 | }
115 | return false;
116 | }
117 |
118 | export function isReferenceReactContextProvider(path, state) {
119 | return isReferenceReactContextConsumerOrProvider(path, "Provider", state);
120 | }
121 |
122 | export function isReferenceReactContextConsumer(path, state) {
123 | return isReferenceReactContextConsumerOrProvider(path, "Consumer", state);
124 | }
125 |
126 | export function getContextObjectRuntimeValueIndex(path, state) {
127 | let contextObjectPath;
128 |
129 | if (t.isJSXMemberExpression(path.node) || t.isMemberExpression(path.node)) {
130 | contextObjectPath = getReferenceFromExpression(path.get("object"), state);
131 | } else if (t.isJSXIdentifier(path.node) || t.isIdentifier(path.node)) {
132 | contextObjectPath = getReferenceFromExpression(path, state);
133 | } else {
134 | throw new Error("TODO");
135 | }
136 | let contextObjectRefNode;
137 |
138 | if (isDestructuredRef(contextObjectPath)) {
139 | contextObjectPath = contextObjectPath.object;
140 | }
141 |
142 | if (t.isVariableDeclarator(contextObjectPath.parentPath.node)) {
143 | contextObjectRefNode = contextObjectPath.parentPath.node.id;
144 | } else {
145 | throw new Error("TODO");
146 | }
147 | return getRuntimeValueIndex(contextObjectRefNode, state);
148 | }
149 |
--------------------------------------------------------------------------------
/src/react/functions.js:
--------------------------------------------------------------------------------
1 | import { getReferenceFromExpression } from "../references";
2 | import { emptyObject, getComponentName, isPrimitive, isRootPathConditional } from "../utils";
3 | import { applyCachedRuntimeValues } from "../transforms";
4 | import { compileNode } from "../nodes";
5 | import { MultiReturnConditionalTemplateNode, StaticValueTemplateNode } from "../templates";
6 | import invariant from "../invariant";
7 | import * as t from "@babel/types";
8 |
9 | function replaceTemplateReturn(path, originalPathNode, replacement) {
10 | if (!path.removed) {
11 | if (path.node === originalPathNode || path.node === emptyObject) {
12 | path.replaceWith(replacement);
13 | } else if (t.isBlockStatement(path.node)) {
14 | for (let blockNode of path.node.body) {
15 | if (
16 | t.isReturnStatement(blockNode) &&
17 | (blockNode.argument === originalPathNode || blockNode.argument === emptyObject)
18 | ) {
19 | blockNode.argument = replacement;
20 | break;
21 | }
22 | }
23 | } else {
24 | throw new Error("TODO");
25 | }
26 | }
27 | }
28 |
29 | function compileTemplateBranch(templateBranch, templateBranchIndex, state, componentPath, compileToVNode) {
30 | const runtimeValues = new Map();
31 | if (templateBranchIndex !== null) {
32 | runtimeValues.set(t.numericLiteral(templateBranchIndex), {
33 | index: 0,
34 | references: 1,
35 | });
36 | }
37 | const childState = { ...state, ...{ runtimeValues } };
38 | // Get the argument from the return statement
39 | const isExplicitReturn = templateBranch.isExplicitReturn;
40 | const path = isExplicitReturn ? templateBranch.path.get("argument") : templateBranch.path;
41 | const refPath = getReferenceFromExpression(path, state);
42 | const originalPathNode = path.node;
43 | const templateNode = compileNode(path, refPath, childState, componentPath, true);
44 | const isStatic = runtimeValues.size === 0 && templateBranch.isConditional === false;
45 | templateNode.isStatic = isStatic;
46 | // replace branch with values array
47 | const runtimeValuesArray = [];
48 | for (let [runtimeValue, { index }] of runtimeValues) {
49 | runtimeValuesArray[index] = runtimeValue;
50 | }
51 | const runtimeValuesArrayAST = t.arrayExpression(runtimeValuesArray);
52 | if (compileToVNode) {
53 | state.helpers.add("createVNode");
54 | const vNodeAST = t.callExpression(t.identifier("createVNode"), [
55 | templateNode.toAST(),
56 | runtimeValuesArrayAST,
57 | templateNode.getKeyASTNode(),
58 | ]);
59 | replaceTemplateReturn(path, originalPathNode, vNodeAST);
60 | } else {
61 | replaceTemplateReturn(path, originalPathNode, runtimeValuesArrayAST);
62 | }
63 | return templateNode;
64 | }
65 |
66 | function compileTemplateBranches(templateBranches, computeFunction, state, functionPath, compileToVNode) {
67 | if (templateBranches.length === 1) {
68 | const templateBranch = templateBranches[0];
69 | return compileTemplateBranch(templateBranch, null, state, functionPath, compileToVNode);
70 | } else {
71 | // Check how many non primitive roots we have
72 | const nonPrimitiveRoots = templateBranches.filter(branch => !branch.isPrimitive);
73 |
74 | if (nonPrimitiveRoots.length === 1) {
75 | const templateBranch = nonPrimitiveRoots[0];
76 | return compileTemplateBranch(templateBranch, null, state, functionPath, compileToVNode);
77 | } else {
78 | const multiConditionalTemplateNode = new MultiReturnConditionalTemplateNode();
79 | let templateBranchIndex = 0;
80 | let isStatic = true;
81 |
82 | for (let templateBranch of templateBranches) {
83 | if (!templateBranch.isPrimitive) {
84 | const templateBranchTemplateNode = compileTemplateBranch(
85 | templateBranch,
86 | templateBranchIndex,
87 | state,
88 | functionPath,
89 | compileToVNode,
90 | );
91 | if (!templateBranch.isStatic) {
92 | isStatic = false;
93 | }
94 | multiConditionalTemplateNode.conditions.push([templateBranchIndex, templateBranchTemplateNode]);
95 | templateBranchIndex++;
96 | }
97 | }
98 | if (isStatic) {
99 | return new StaticValueTemplateNode(null);
100 | }
101 | return multiConditionalTemplateNode;
102 | }
103 | }
104 | }
105 |
106 | export function compileReactComputeFunction(functionPath, state, isComponentFunction, compileToVNode) {
107 | const computeFunction = functionPath.node;
108 | if (state.computeFunctionCache.has(computeFunction)) {
109 | return state.computeFunctionCache.get(computeFunction);
110 | }
111 | if (isComponentFunction) {
112 | updateComputeFunctionName(functionPath);
113 | }
114 |
115 | const templateBranches = [];
116 | const runtimeConditionals = new Map();
117 | const runtimeCachedValues = new Map();
118 | let previousRootWasConditional = false;
119 |
120 | // When the arrow function body is an expression
121 | if (t.isArrowFunctionExpression(computeFunction) && !t.isBlockStatement(computeFunction.body)) {
122 | templateBranches.push({
123 | path: functionPath.get("body"),
124 | isConditional: false,
125 | isExplicitReturn: false,
126 | isPrimitive: isPrimitive(computeFunction.body),
127 | });
128 | } else {
129 | functionPath.traverse({
130 | ReturnStatement(templatePath) {
131 | if (templatePath.scope.getFunctionParent() !== functionPath.scope) {
132 | return;
133 | }
134 | const isConditional = previousRootWasConditional || isRootPathConditional(functionPath, templatePath);
135 | if (isConditional) {
136 | previousRootWasConditional = true;
137 | }
138 | // Replace template note with value array
139 | const arg = templatePath.node.argument;
140 |
141 | templateBranches.push({
142 | path: templatePath,
143 | isConditional,
144 | isExplicitReturn: true,
145 | isPrimitive: isPrimitive(arg),
146 | });
147 | },
148 | });
149 | }
150 |
151 | const childState = {
152 | ...state,
153 | ...{
154 | runtimeCachedValues,
155 | runtimeConditionals,
156 | },
157 | };
158 | const templateNode = compileTemplateBranches(
159 | templateBranches,
160 | computeFunction,
161 | childState,
162 | functionPath,
163 | compileToVNode,
164 | );
165 | const isStatic = templateNode.isStatic;
166 |
167 | let computeFunctionRef = null;
168 | if (isComponentFunction) {
169 | if (!isStatic) {
170 | computeFunctionRef = getComponentName(functionPath);
171 | }
172 | }
173 |
174 | state.computeFunctionCache.set(computeFunction, {
175 | compileToVNode,
176 | computeFunctionRef,
177 | isStatic,
178 | templateNode,
179 | });
180 |
181 | applyCachedRuntimeValues(functionPath, runtimeCachedValues);
182 |
183 | return {
184 | compileToVNode,
185 | computeFunctionRef,
186 | isStatic,
187 | templateNode,
188 | };
189 | }
190 |
191 | function updateComputeFunctionName(functionPath) {
192 | const name = getComponentName(functionPath);
193 | // Change compute function name
194 | if (t.isFunctionDeclaration(functionPath.node) && t.isIdentifier(functionPath.node.id)) {
195 | functionPath.node.id.name = `${name}_ComputeFunction`;
196 | } else {
197 | const parentPath = functionPath.parentPath;
198 | if (t.isVariableDeclarator(parentPath.node) && t.isIdentifier(parentPath.node.id)) {
199 | parentPath.node.id.name = `${name}_ComputeFunction`;
200 | } else {
201 | invariant(false, "TODO");
202 | }
203 | }
204 | }
205 |
--------------------------------------------------------------------------------
/src/react/style.js:
--------------------------------------------------------------------------------
1 | const uppercasePattern = /([A-Z])/g;
2 | const msPattern = /^ms-/;
3 |
4 | const isUnitlessNumber = new Set([
5 | "animationIterationCount",
6 | "borderImageOutset",
7 | "borderImageSlice",
8 | "borderImageWidth",
9 | "boxFlex",
10 | "boxFlexGroup",
11 | "boxOrdinalGroup",
12 | "columnCount",
13 | "columns",
14 | "flex",
15 | "flexGrow",
16 | "flexPositive",
17 | "flexShrink",
18 | "flexNegative",
19 | "flexOrder",
20 | "gridArea",
21 | "gridRow",
22 | "gridRowEnd",
23 | "gridRowSpan",
24 | "gridRowStart",
25 | "gridColumn",
26 | "gridColumnEnd",
27 | "gridColumnSpan",
28 | "gridColumnStart",
29 | "fontWeight",
30 | "lineClamp",
31 | "lineHeight",
32 | "opacity",
33 | "order",
34 | "orphans",
35 | "tabSize",
36 | "widows",
37 | "zIndex",
38 | "zoom",
39 |
40 | // SVG-related properties
41 | "fillOpacity",
42 | "floodOpacity",
43 | "stopOpacity",
44 | "strokeDasharray",
45 | "strokeDashoffset",
46 | "strokeMiterlimit",
47 | "strokeOpacity",
48 | "strokeWidth",
49 | ]);
50 |
51 | /**
52 | * @param {string} prefix vendor-specific prefix, eg: Webkit
53 | * @param {string} key style name, eg: transitionDuration
54 | * @return {string} style name prefixed with `prefix`, properly camelCased, eg:
55 | * WebkitTransitionDuration
56 | */
57 | function prefixKey(prefix, key) {
58 | return prefix + key.charAt(0).toUpperCase() + key.substring(1);
59 | }
60 |
61 | /**
62 | * Support style names that may come passed in prefixed by adding permutations
63 | * of vendor prefixes.
64 | */
65 | const prefixes = ["Webkit", "ms", "Moz", "O"];
66 |
67 | // Using Object.keys here, or else the vanilla for-in loop makes IE8 go into an
68 | // infinite loop, because it iterates over the newly added props too.
69 | Object.keys(isUnitlessNumber).forEach(prop => {
70 | prefixes.forEach(prefix => {
71 | isUnitlessNumber.add(prefixKey(prefix, prop));
72 | });
73 | });
74 |
75 | export function hyphenateStyleName(name) {
76 | return name
77 | .replace(uppercasePattern, "-$1")
78 | .toLowerCase()
79 | .replace(msPattern, "-ms-");
80 | }
81 |
82 | export function dangerousStyleValue(name, value, isCustomProperty) {
83 | // Note that we've removed escapeTextForBrowser() calls here since the
84 | // whole string will be escaped when the attribute is injected into
85 | // the markup. If you provide unsafe user data here they can inject
86 | // arbitrary CSS which may be problematic (I couldn't repro this):
87 | // https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet
88 | // http://www.thespanner.co.uk/2007/11/26/ultimate-xss-css-injection/
89 | // This is not an XSS hole but instead a potential CSS injection issue
90 | // which has lead to a greater discussion about how we're going to
91 | // trust URLs moving forward. See #2115901
92 |
93 | if (typeof value === "boolean" || value === "") {
94 | return "";
95 | }
96 |
97 | if (!isCustomProperty && typeof value === "number" && value !== 0 && !isUnitlessNumber.has(name)) {
98 | return value + "px"; // Presumes implicit 'px' suffix for unitless numbers
99 | }
100 |
101 | return ("" + value).trim();
102 | }
103 |
--------------------------------------------------------------------------------
/src/validation.js:
--------------------------------------------------------------------------------
1 | import * as t from "@babel/types";
2 | import traverse from "@babel/traverse";
3 | import { getCodeLocation, getComponentName, getProgramPath, isReactCreateElement } from "./utils";
4 | import { assertType, getTypeAnnotationForExpression } from "./annotations";
5 | import { getBindingPathRef, getReferenceFromExpression } from "./references";
6 |
7 | function arrayMethodMutates(arrayMethod) {
8 | return (
9 | arrayMethod === "push" ||
10 | arrayMethod === "splice" ||
11 | arrayMethod === "unshift" ||
12 | arrayMethod === "pop" ||
13 | arrayMethod === "shift" ||
14 | arrayMethod === "sort" ||
15 | arrayMethod === "reverse" ||
16 | arrayMethod === "fill"
17 | );
18 | }
19 |
20 | export function validateArrayHasNotBeenMutated(path, bindingName, state) {
21 | const binding = getBindingPathRef(path, bindingName, state);
22 | // Check if any array mutation occurs, then throw an error if found.
23 | // TODO check if the array is passed to the outside or to another
24 | // function, where it might be mutated.
25 | for (let referencePath of binding.referencePaths) {
26 | const parentPath = referencePath.parentPath;
27 | const parentNode = parentPath.node;
28 |
29 | if (t.isMemberExpression(parentNode) && t.isIdentifier(parentNode.property)) {
30 | const arrayMethod = parentNode.property.name;
31 | if (arrayMethodMutates(arrayMethod)) {
32 | throw new Error(
33 | `The compiler does not support mutated arrays that are used as part of a component's render. Found mutating array method "${bindingName}.${arrayMethod}(...)" at ${getCodeLocation(
34 | path.node,
35 | )}`,
36 | );
37 | }
38 | }
39 | }
40 | // Check if the array has been updated via square bracket notation
41 | const programPath = getProgramPath(path);
42 | programPath.traverse({
43 | AssignmentExpression(assignPath) {
44 | const node = assignPath.node;
45 |
46 | if (
47 | t.isMemberExpression(node.left) &&
48 | t.isIdentifier(node.left.object) &&
49 | node.left.object.name === bindingName
50 | ) {
51 | throw new Error(
52 | `The compiler does not support mutated arrays that are used as part of a component's render. Found direct array mutation at ${getCodeLocation(
53 | assignPath.node,
54 | )}`,
55 | );
56 | }
57 | },
58 | });
59 | }
60 |
61 | export function validateArgumentsDoNotContainTemplateNodes(path, refPath, state) {
62 | const argumentsPath = refPath.get("arguments");
63 | for (let argPath of argumentsPath) {
64 | const argPathRef = getReferenceFromExpression(argPath, state);
65 | const typeAnnotation = getTypeAnnotationForExpression(argPath, state);
66 |
67 | if (
68 | t.isJSXElement(argPathRef.node) ||
69 | (assertType(path, typeAnnotation, true, state, "REACT_NODE") &&
70 | !t.isArrowFunctionExpression(argPathRef.node) &&
71 | !t.isFunctionExpression(argPathRef.node) &&
72 | !t.isFunctionDeclaration(argPathRef.node))
73 | ) {
74 | throw new Error(
75 | `The compiler does not currently support passing of React elements to function calls at ${getCodeLocation(
76 | refPath.node,
77 | )}`,
78 | );
79 | }
80 | }
81 | }
82 |
83 | export function validateReactElementsHaveAllBeenCompiled(moduleAst, state) {
84 | traverse(moduleAst, {
85 | JSXElement(path) {
86 | throw new Error(
87 | `The compiler failed to reach JSX node and compile it away at ${getCodeLocation(
88 | path.node,
89 | )}. This is likely due to an unsupported code style or due to the complexity of the component code.`,
90 | );
91 | },
92 | CallExpression(path) {
93 | if (t.isIdentifier(path.node.callee) && path.node.callee.name === "createVNode") {
94 | return;
95 | }
96 | if (isReactCreateElement(path, state)) {
97 | throw new Error(
98 | `The compiler failed to reach React.createElement and compile it away at ${getCodeLocation(
99 | path.node,
100 | )}. This is likely due to an unsupported code style or due to the complexity of the component code.`,
101 | );
102 | }
103 | },
104 | });
105 | }
106 |
107 | export function validateFunctionComponentUsesDestructuredProps(path) {
108 | const node = path.node;
109 | const name = getComponentName(path);
110 |
111 | if (node.params.length > 0 && !t.isObjectPattern(node.params[0])) {
112 | throw new Error(`Compilation failed on component "${name}". "props" argument must be a destructured object.`);
113 | }
114 | }
115 |
116 | export function validateParamsDoNotConflictOuterScope(params, componentPath, path, state) {
117 | for (let param of params) {
118 | if (t.isIdentifier(param)) {
119 | const paramName = param.name;
120 | const binding = componentPath.scope.getBinding(paramName);
121 | if (binding !== undefined && binding.path !== path) {
122 | throw new Error(
123 | `Compilation failed due to a function paramater of the same name being created in an outer scope at ${getCodeLocation(
124 | path.node,
125 | )}`,
126 | );
127 | }
128 | }
129 | }
130 | }
131 |
--------------------------------------------------------------------------------
/tests/benchmarks/hacker-news.js:
--------------------------------------------------------------------------------
1 | // props_from_file:tests/benchmarks/hacker-news.json
2 |
3 | const React = require("react");
4 | const e = React.createElement;
5 |
6 | type StoryType = {
7 | by: string,
8 | descendants: number,
9 | id: number,
10 | kids: Array,
11 | score: number,
12 | time: number,
13 | title: string,
14 | type: string,
15 | url: string,
16 | };
17 |
18 | function timeAge(time): string {
19 | const now = new Date().getTime() / 1000;
20 | const minutes = (now - time) / 60;
21 |
22 | if (minutes < 60) {
23 | return Math.round(minutes) + " minutes ago";
24 | }
25 | return Math.round(minutes / 60) + " hours ago";
26 | }
27 |
28 | function getHostUrl(url): string {
29 | return (url + "")
30 | .replace("https://", "")
31 | .replace("http://", "")
32 | .split("/")[0];
33 | }
34 |
35 | function HeaderBar() {
36 | return e(
37 | "tr",
38 | {
39 | style: {
40 | backgroundColor: "#222",
41 | },
42 | },
43 | e(
44 | "table",
45 | {
46 | style: {
47 | padding: 4,
48 | },
49 | width: "100%",
50 | cellSpacing: 0,
51 | cellPadding: 0,
52 | },
53 | e(
54 | "tbody",
55 | null,
56 | e(
57 | "tr",
58 | null,
59 | e(
60 | "td",
61 | {
62 | style: {
63 | width: 18,
64 | paddingRight: 4,
65 | },
66 | },
67 | e(
68 | "a",
69 | {
70 | href: "#",
71 | },
72 | e("img", {
73 | src: "logo.png",
74 | width: 16,
75 | height: 16,
76 | style: {
77 | border: "1px solid #00d8ff",
78 | },
79 | }),
80 | ),
81 | ),
82 | e(
83 | "td",
84 | {
85 | style: {
86 | lineHeight: "12pt",
87 | },
88 | height: 10,
89 | },
90 | e(
91 | "span",
92 | {
93 | className: "pagetop",
94 | },
95 | e("b", { className: "hnname" }, "React HN Benchmark"),
96 | e("a", { href: "#" }, "new"),
97 | " | ",
98 | e("a", { href: "#" }, "comments"),
99 | " | ",
100 | e("a", { href: "#" }, "show"),
101 | " | ",
102 | e("a", { href: "#" }, "ask"),
103 | " | ",
104 | e("a", { href: "#" }, "jobs"),
105 | " | ",
106 | e("a", { href: "#" }, "submit"),
107 | ),
108 | ),
109 | ),
110 | ),
111 | ),
112 | );
113 | }
114 |
115 | function Story({ story, rank }: { story: StoryType, rank: number }) {
116 | return e(React.Fragment, null,
117 | e(
118 | "tr",
119 | {
120 | className: "athing",
121 | },
122 | e(
123 | "td",
124 | {
125 | style: {
126 | verticalAlign: "top",
127 | textAlign: "right",
128 | },
129 | className: "title",
130 | },
131 | e(
132 | "span",
133 | {
134 | className: "rank",
135 | },
136 | `${rank}.`,
137 | ),
138 | ),
139 | e(
140 | "td",
141 | {
142 | className: "votelinks",
143 | style: {
144 | verticalAlign: "top",
145 | },
146 | },
147 | e(
148 | "center",
149 | null,
150 | e(
151 | "a",
152 | {
153 | href: "#",
154 | },
155 | e("div", {
156 | className: "votearrow",
157 | title: "upvote",
158 | }),
159 | ),
160 | ),
161 | ),
162 | e(
163 | "td",
164 | {
165 | className: "title",
166 | },
167 | e(
168 | "a",
169 | {
170 | href: "#",
171 | className: "storylink",
172 | },
173 | story.title,
174 | ),
175 | story.url
176 | ? e(
177 | "span",
178 | {
179 | className: "sitebit comhead",
180 | },
181 | " (",
182 | e(
183 | "a",
184 | {
185 | href: "#",
186 | },
187 | getHostUrl(story.url),
188 | ),
189 | ")",
190 | )
191 | : null,
192 | ),
193 | ),
194 | e(
195 | "tr",
196 | null,
197 | e("td", {
198 | colSpan: 2,
199 | }),
200 | e(
201 | "td",
202 | {
203 | className: "subtext",
204 | },
205 | e(
206 | "span",
207 | {
208 | className: "score",
209 | },
210 | `${story.score} points`,
211 | ),
212 | " by ",
213 | e(
214 | "a",
215 | {
216 | href: "#",
217 | className: "hnuser",
218 | },
219 | story.by,
220 | ),
221 | " ",
222 | e(
223 | "span",
224 | {
225 | className: "age",
226 | },
227 | e(
228 | "a",
229 | {
230 | href: "#",
231 | },
232 | timeAge(story.time),
233 | ),
234 | ),
235 | " | ",
236 | e(
237 | "a",
238 | {
239 | href: "#",
240 | },
241 | "hide",
242 | ),
243 | " | ",
244 | e(
245 | "a",
246 | {
247 | href: "#",
248 | },
249 | `${story.descendants || 0} comments`,
250 | ),
251 | ),
252 | ),
253 | e("tr", {
254 | style: {
255 | height: 5,
256 | },
257 | className: "spacer",
258 | }),
259 | );
260 | }
261 |
262 | function StoryList({ stories }: { stories: Array }) {
263 | return e(
264 | "tr",
265 | null,
266 | e(
267 | "td",
268 | null,
269 | e(
270 | "table",
271 | {
272 | cellPadding: 0,
273 | cellSpacing: 0,
274 | classList: "itemlist",
275 | },
276 | e(
277 | "tbody",
278 | null,
279 | stories.map((story: Story, i: number): React.Node => e(Story, { story, rank: ++i, key: story.id })),
280 | ),
281 | ),
282 | ),
283 | );
284 | }
285 |
286 | function App({ stories }: { stories: Array }) {
287 | return e(
288 | "center",
289 | null,
290 | e(
291 | "table",
292 | {
293 | id: "hnmain",
294 | border: 0,
295 | cellPadding: 0,
296 | cellSpacing: 0,
297 | width: "85%",
298 | style: {
299 | "background-color": "#f6f6ef",
300 | },
301 | },
302 | e(
303 | "tbody",
304 | null,
305 | e(HeaderBar, null),
306 | e("tr", { height: 10 }),
307 | e(StoryList, {
308 | stories,
309 | }),
310 | ),
311 | ),
312 | );
313 | }
314 |
315 | App.compileRootComponent = true;
316 |
317 | module.exports = App;
318 |
--------------------------------------------------------------------------------
/tests/benchmarks/marko-benchmarks/color-picker.js:
--------------------------------------------------------------------------------
1 | // props_from_file:tests/benchmarks/marko-benchmarks/color-picker.json
2 |
3 | const React = require("react");
4 |
5 | type ColorType = {
6 | hex: string,
7 | name: string,
8 | rgb: string,
9 | };
10 |
11 | function App({ colors }: { colors: Array }) {
12 | var colors = colors;
13 | var selectedColorIndex = 0;
14 | var selectedColor = colors[selectedColorIndex];
15 |
16 | function renderColor(color: ColorType, i: number): React.Node {
17 | var style = {
18 | backgroundColor: color.hex,
19 | };
20 |
21 | var className = "color";
22 | if (selectedColorIndex === i) {
23 | className += " selected";
24 | }
25 |
26 | return (
27 |
28 | {color.name}
29 |
30 | );
31 | }
32 |
33 | function renderColors(colors: Array): React.Node {
34 | if (colors.length) {
35 | return {colors.map(renderColor)}
;
36 | } else {
37 | return No colors!
;
38 | }
39 | }
40 |
41 | return (
42 |
43 |
Choose your favorite color:
44 |
{renderColors(colors)}
45 |
46 | You chose:
47 |
{selectedColor.name}
48 |
49 |
50 | );
51 | }
52 |
53 | App.compileRootComponent = true;
54 |
55 | module.exports = App;
56 |
--------------------------------------------------------------------------------
/tests/create-element/array/array.js:
--------------------------------------------------------------------------------
1 | // props:{items: [1,2,3,4,5]}
2 | var React = require("react");
3 |
4 | function Component({ items }: { items: Array }) {
5 | return React.createElement(
6 | "ul",
7 | null,
8 | items.map((item, i: number): React.Node =>
9 | React.createElement(
10 | "li",
11 | {
12 | key: i,
13 | },
14 | "Static item!",
15 | ),
16 | ),
17 | );
18 | }
19 |
20 | Component.compileRootComponent = true;
21 |
22 | module.exports = Component;
23 |
--------------------------------------------------------------------------------
/tests/create-element/array/array2.js:
--------------------------------------------------------------------------------
1 | // props:{items: [1,2,3,4,5]}
2 | var React = require("react");
3 |
4 | function Component({ items }: { items: Array }) {
5 | return (
6 |
7 | {items.map(
8 | (item: number): React.Node =>
9 | React.createElement(
10 | "li",
11 | {
12 | key: item,
13 | },
14 | "The item is ",
15 | item,
16 | ),
17 | )}
18 |
19 | );
20 | }
21 |
22 | Component.compileRootComponent = true;
23 |
24 | module.exports = Component;
25 |
--------------------------------------------------------------------------------
/tests/create-element/array/array3.js:
--------------------------------------------------------------------------------
1 | // props:{items: [{id: 0, text: "Item 1"}, {id: 1, text: "Item 2"}, {id: 2, text: "Item 3"}, {id: 3, text: "Item 4"}, {id: 4, text: "Item 5"}]}
2 | var React = require("react");
3 |
4 | function Component({ items }: { items: Array }) {
5 | const children = Array.from(items).map(
6 | (item: { id: number, text: string }): React.Node =>
7 | React.createElement(
8 | "li",
9 | {
10 | key: item.id,
11 | },
12 | React.createElement("div", null, "The text is ", item.text, "!"),
13 | )
14 | );
15 | return React.createElement("ul", null, children);
16 | }
17 |
18 | Component.compileRootComponent = true;
19 |
20 | module.exports = Component;
21 |
--------------------------------------------------------------------------------
/tests/create-element/array/array4.js:
--------------------------------------------------------------------------------
1 | // props:{items: [1,2,3,4,5]}
2 | var React = require("react");
3 |
4 | function Component({ items }: { items: Array }) {
5 | const children = items
6 | .filter((item: number) => item % 2)
7 | .map(
8 | (item, i: number): React.Node =>
9 | React.createElement(
10 | "li",
11 | {
12 | key: i,
13 | },
14 | "Static item!",
15 | ),
16 | );
17 | return React.createElement("ul", null, children);
18 | }
19 |
20 | Component.compileRootComponent = true;
21 |
22 | module.exports = Component;
23 |
--------------------------------------------------------------------------------
/tests/create-element/array/array5.js:
--------------------------------------------------------------------------------
1 | // props:{items: [1,2,3,4,5]}
2 | var React = require("react");
3 |
4 | type ReactElementType = React.Node;
5 | type ItemsType = Array;
6 |
7 | function Component({ items }: { items: ItemsType }) {
8 | const children = items
9 | .filter((item: number) => item % 2)
10 | .map(
11 | (item, i: number): ReactElementType =>
12 | React.createElement(
13 | "li",
14 | {
15 | key: i,
16 | },
17 | "Static item!",
18 | ),
19 | );
20 | return React.createElement("ul", null, children);
21 | }
22 |
23 | Component.compileRootComponent = true;
24 |
25 | module.exports = Component;
26 |
--------------------------------------------------------------------------------
/tests/create-element/array/array6.js:
--------------------------------------------------------------------------------
1 | // props:{items: [1,2,3,4,5]}
2 | var React = require("react");
3 |
4 | function Component2({ children }: { children: Array }) {
5 | return React.createElement("li", null, children);
6 | }
7 |
8 | function Component({ items }: { items: Array }) {
9 | return React.createElement(
10 | "ul",
11 | null,
12 | items.map((item: number): React.Node =>
13 | React.createElement(
14 | Component2,
15 | {
16 | key: item,
17 | },
18 | "The item is ",
19 | item,
20 | ),
21 | ),
22 | );
23 | }
24 |
25 | Component.compileRootComponent = true;
26 |
27 | module.exports = Component;
28 |
--------------------------------------------------------------------------------
/tests/create-element/array/array7.js:
--------------------------------------------------------------------------------
1 | // props:{items: [1,2,3,4,5]}
2 | var React = require("react");
3 |
4 | function Component2({ children }: { children: Array }) {
5 | return React.createElement("li", null, children);
6 | }
7 |
8 | function Component({ items }: { items: Array }) {
9 | return React.createElement(
10 | "ul",
11 | null,
12 | items.map((item: number): React.Node =>
13 | React.createElement(
14 | React.Fragment,
15 | {
16 | key: item,
17 | },
18 | React.createElement(Component2, null, "The item is ", item),
19 | React.createElement(Component2, null, "The item is ", item),
20 | ),
21 | ),
22 | );
23 | }
24 |
25 | Component.compileRootComponent = true;
26 |
27 | module.exports = Component;
28 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component() {
4 | return React.createElement("div", null, "Hello world");
5 | }
6 |
7 | Component.compileRootComponent = true;
8 |
9 | module.exports = Component;
10 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic10.js:
--------------------------------------------------------------------------------
1 | // props:{var: "This should show up!", bar: "123", cond: true, cond2: true, defaultClassName: "default-item"}
2 |
3 | var React = require("react");
4 |
5 | function foo(bar): React.Node {
6 | return [
7 | React.createElement(
8 | "span",
9 | {
10 | key: "1",
11 | },
12 | "Hello world",
13 | ),
14 | React.createElement(
15 | "div",
16 | {
17 | key: "2",
18 | },
19 | bar + "",
20 | ),
21 | ];
22 | }
23 |
24 | function Component({
25 | cond,
26 | cond2,
27 | defaultClassName,
28 | bar,
29 | }: {
30 | cond: boolean,
31 | cond2: boolean,
32 | defaultClassName: string,
33 | bar: string,
34 | }) {
35 | let children = [
36 | React.createElement(
37 | "li",
38 | {
39 | key: "default",
40 | className: defaultClassName,
41 | },
42 | "Default item",
43 | ),
44 | ];
45 | let elements;
46 |
47 | if (typeof cond2 !== undefined) {
48 | elements = foo(bar);
49 | }
50 |
51 | if (cond) {
52 | children = [
53 | React.createElement(
54 | "li",
55 | {
56 | key: "generic",
57 | className: "generic-item",
58 | },
59 | elements,
60 | ),
61 | ];
62 | }
63 |
64 | return React.createElement(
65 | "ul",
66 | {
67 | id: "list-view",
68 | className: "list",
69 | },
70 | children,
71 | );
72 | }
73 |
74 | Component.compileRootComponent = true;
75 |
76 | module.exports = Component;
77 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic11.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component() {
4 | return React.createElement(
5 | React.Fragment,
6 | null,
7 | React.createElement("div", null, "Hello world"),
8 | React.createElement(
9 | React.Fragment,
10 | null,
11 | React.createElement("span", null, "123"),
12 | "456",
13 | React.createElement("input", {
14 | type: "text",
15 | }),
16 | React.createElement(React.Fragment, null, React.createElement(React.Fragment, null, "789")),
17 | ),
18 | );
19 | }
20 |
21 | Component.compileRootComponent = true;
22 |
23 | module.exports = Component;
24 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic12.js:
--------------------------------------------------------------------------------
1 | // props:{bar: "123", cond: true, cond2: true, defaultClassName: "default-item"}
2 |
3 | var React = require("react");
4 |
5 | function foo(bar): Array {
6 | return [123, 456, 789, null, false, true, undefined];
7 | }
8 |
9 | function Component({
10 | cond,
11 | cond2,
12 | defaultClassName,
13 | bar,
14 | }: {
15 | cond: boolean,
16 | cond2: boolean,
17 | defaultClassName: string,
18 | bar: string,
19 | }) {
20 | let children = [
21 | React.createElement(
22 | "li",
23 | {
24 | key: "default",
25 | className: undefined,
26 | },
27 | null,
28 | ),
29 | ];
30 | let elements;
31 |
32 | if (typeof cond2 !== undefined) {
33 | elements = foo(bar);
34 | }
35 |
36 | if (cond) {
37 | children = [
38 | React.createElement(
39 | "li",
40 | {
41 | key: "generic",
42 | className: "",
43 | },
44 | elements,
45 | ),
46 | ];
47 | }
48 |
49 | return React.createElement(
50 | "ul",
51 | {
52 | id: "list-view",
53 | className: null,
54 | },
55 | children,
56 | );
57 | }
58 |
59 | Component.compileRootComponent = true;
60 |
61 | module.exports = Component;
62 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic13.js:
--------------------------------------------------------------------------------
1 | // props:{cond: false, cond2: false, txt: '', number: 123}
2 |
3 | var React = require("react");
4 |
5 | function Component({ cond, cond2, txt, num }: { cond: boolean, cond2: boolean, txt: string, num: number }) {
6 | var numStr = num + "";
7 | var txt2 = "100".toString();
8 | var txt3 = "Hello" + "world";
9 | return React.createElement(
10 | "div",
11 | null,
12 | React.createElement("span", null, !cond && cond2 === false ? txt : numStr),
13 | React.createElement(
14 | "span",
15 | {
16 | className: txt,
17 | id: ``,
18 | },
19 | txt2,
20 | txt3,
21 | "Static text!",
22 | txt,
23 | ),
24 | );
25 | }
26 |
27 | Component.compileRootComponent = true;
28 |
29 | module.exports = Component;
30 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic14.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component() {
4 | function handleClick(e): void {
5 | e.target.textContent = "Worked!";
6 | }
7 |
8 | return React.createElement(
9 | "div",
10 | {
11 | onClick: handleClick,
12 | },
13 | "Hello world",
14 | );
15 | }
16 |
17 | Component.compileRootComponent = true;
18 |
19 | module.exports = Component;
20 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic15.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component() {
4 | function getHandler(): e => void {
5 | return e => {
6 | e.target.textContent = "Worked!";
7 | };
8 | }
9 |
10 | return React.createElement(
11 | "div",
12 | {
13 | onClick: getHandler(),
14 | },
15 | "Hello world",
16 | );
17 | }
18 |
19 | Component.compileRootComponent = true;
20 |
21 | module.exports = Component;
22 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic16.js:
--------------------------------------------------------------------------------
1 | // props:{cond: false}
2 | var React = require("react");
3 |
4 | function Component({ cond }: { cond: boolean }) {
5 | const elements = [
6 | React.createElement(
7 | "span",
8 | {
9 | key: "a",
10 | },
11 | "123",
12 | ),
13 | React.createElement(
14 | "span",
15 | {
16 | key: "b",
17 | },
18 | "456",
19 | ),
20 | ];
21 |
22 | let elements2 = [7, 8, 9];
23 |
24 | if (!cond) {
25 | elements2 = [10, 11, 12];
26 | }
27 |
28 | function getElements(): React.Node {
29 | return React.createElement("div", null, elements, elements2);
30 | }
31 |
32 | return React.createElement("div", null, getElements());
33 | }
34 |
35 | Component.compileRootComponent = true;
36 |
37 | module.exports = Component;
38 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic17.js:
--------------------------------------------------------------------------------
1 | // props:{cond1: true, cond2: false}
2 | var React = require("react");
3 |
4 | function Component({ cond1, cond2 }: { cond1: boolean, cond2: boolean }) {
5 | let className = "class1";
6 |
7 | if (cond1) {
8 | className = "class2";
9 | }
10 | if (cond2) {
11 | className = "class3";
12 | }
13 |
14 | return React.createElement(
15 | "div",
16 | {
17 | className: className,
18 | },
19 | "Hello world",
20 | );
21 | }
22 |
23 | Component.compileRootComponent = true;
24 |
25 | module.exports = Component;
26 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic18.js:
--------------------------------------------------------------------------------
1 | // props:{cond1: true, cond2: true}
2 | var React = require("react");
3 |
4 | function Component({ cond1, cond2 }: { cond1: boolean, cond2: boolean }) {
5 | let className = cond1 ? "class2" : cond2 ? "class3" : "class1";
6 |
7 | return React.createElement("div", {
8 | className: className
9 | }, "Hello world");
10 | }
11 |
12 | Component.compileRootComponent = true;
13 |
14 | module.exports = Component;
15 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic19.js:
--------------------------------------------------------------------------------
1 | // props:{cond1: true, cond2: true, val1: "works"}
2 | var React = require("react");
3 |
4 | function getVal2(): string {
5 | return "works2";
6 | }
7 |
8 | function Component({ cond1, cond2, val1 }: { cond1: boolean, cond2: boolean, val1: string }) {
9 | let className = cond1 ? val1 : cond2 ? getVal2() : "class1";
10 |
11 | return React.createElement(
12 | "div",
13 | {
14 | className: className,
15 | },
16 | "Hello world",
17 | );
18 | }
19 |
20 | Component.compileRootComponent = true;
21 |
22 | module.exports = Component;
23 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic2.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component() {
4 | return React.createElement(
5 | "div",
6 | null,
7 | React.createElement("span", null, "Hello"),
8 | React.createElement("span", null, "world"),
9 | );
10 | }
11 |
12 | Component.compileRootComponent = true;
13 |
14 | module.exports = Component;
15 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic20.js:
--------------------------------------------------------------------------------
1 | // props:{cond: true, cond2: false, txt: '', number: 123}
2 |
3 | var React = require("react");
4 |
5 | function Component({ cond, cond2, txt, num }: { cond: boolean, cond2: boolean, txt: string, num: number }) {
6 | var numStr = num + "";
7 | var txt2 = "100".toString();
8 | var txt3 = "Hello" + "world";
9 | return React.createElement(
10 | "div",
11 | null,
12 | React.createElement("span", null, !cond && cond2 === false ? txt : numStr),
13 | React.createElement(
14 | "span",
15 | {
16 | className: txt,
17 | id: ``,
18 | },
19 | txt2,
20 | txt3,
21 | "Static text!",
22 | txt,
23 | ),
24 | );
25 | }
26 |
27 | Component.compileRootComponent = true;
28 |
29 | module.exports = Component;
30 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic21.js:
--------------------------------------------------------------------------------
1 | // props:{cond: true, cond2: false, txt: '', number: 123}
2 |
3 | var React = require("react");
4 |
5 | type PropsType = {
6 | cond: boolean,
7 | cond2: boolean,
8 | txt: string,
9 | num: number,
10 | };
11 |
12 | function Component({ cond, cond2, txt, num }: PropsType) {
13 | var numStr = num + "";
14 | var txt2 = "100".toString();
15 | var txt3 = "Hello" + "world";
16 | return React.createElement(
17 | "div",
18 | null,
19 | React.createElement("span", null, !cond && cond2 === false ? txt : numStr),
20 | React.createElement(
21 | "span",
22 | {
23 | className: txt,
24 | id: ``,
25 | },
26 | txt2,
27 | txt3,
28 | "Static text!",
29 | txt,
30 | ),
31 | );
32 | }
33 |
34 | Component.compileRootComponent = true;
35 |
36 | module.exports = Component;
37 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic22.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component2() {
4 | return React.createElement("span", null, "Hello world!");
5 | }
6 |
7 | function Component() {
8 | return React.createElement("div", null, React.createElement(Component2, null));
9 | }
10 |
11 | Component.compileRootComponent = true;
12 |
13 | module.exports = Component;
14 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic23.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component2({ x }: { x: number }) {
4 | return React.createElement("span", null, "The number is ", x);
5 | }
6 |
7 | function Component() {
8 | return React.createElement(
9 | "div",
10 | null,
11 | React.createElement(Component2, {
12 | x: 5,
13 | }),
14 | );
15 | }
16 |
17 | Component.compileRootComponent = true;
18 |
19 | module.exports = Component;
20 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic24.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component2({ children }: { children: string }) {
4 | return React.createElement("span", null, "The child is ", children);
5 | }
6 |
7 | function Component() {
8 | return React.createElement("div", null, React.createElement(Component2, null, "Hello world"));
9 | }
10 |
11 | Component.compileRootComponent = true;
12 |
13 | module.exports = Component;
14 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic25.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component2({ children }: { children: Array }) {
4 | return React.createElement("span", null, "The child is ", children);
5 | }
6 |
7 | function Component() {
8 | return React.createElement("div", null, React.createElement(Component2, null, [1, 2, 3]));
9 | }
10 |
11 | Component.compileRootComponent = true;
12 |
13 | module.exports = Component;
14 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic26.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component2({ children }: { children: { text: string } }) {
4 | return React.createElement("span", null, "The child is ", children.text);
5 | }
6 |
7 | function Component() {
8 | return React.createElement(
9 | "div",
10 | null,
11 | React.createElement(Component2, null, {
12 | text: "Hello world!",
13 | }),
14 | );
15 | }
16 |
17 | Component.compileRootComponent = true;
18 |
19 | module.exports = Component;
20 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic27.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component2({ children }: { children: React.Node }) {
4 | return React.createElement("span", null, "The child is ", children);
5 | }
6 |
7 | function Component() {
8 | return React.createElement(
9 | "div",
10 | null,
11 | React.createElement(Component2, null, React.createElement("span", null, "Hello world")),
12 | );
13 | }
14 |
15 | Component.compileRootComponent = true;
16 |
17 | module.exports = Component;
18 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic28.js:
--------------------------------------------------------------------------------
1 | // props:{val: "hello world"}
2 | var React = require("react");
3 |
4 | function Component2({ children }: { children: React.Node }) {
5 | return React.createElement("span", null, "The child is ", children);
6 | }
7 |
8 | function Component({ val }: { val: string }) {
9 | return React.createElement(
10 | "div",
11 | null,
12 | React.createElement(
13 | Component2,
14 | null,
15 | React.createElement("span", null, "Hello world"),
16 | React.createElement("div", null, val),
17 | ),
18 | );
19 | }
20 |
21 | Component.compileRootComponent = true;
22 |
23 | module.exports = Component;
24 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic29.js:
--------------------------------------------------------------------------------
1 | // props:{val: "hello world"}
2 | var React = require("react");
3 |
4 | function Component2({ children }: { children: Array }) {
5 | return React.createElement("span", null, "The child is ", children);
6 | }
7 |
8 | function Component({ val }: { val: string }) {
9 | return React.createElement(
10 | "div",
11 | null,
12 | React.createElement(
13 | Component2,
14 | null,
15 | React.createElement("span", null, "Hello world"),
16 | React.createElement("div", null, val),
17 | null,
18 | undefined,
19 | "Hello world",
20 | false,
21 | ),
22 | );
23 | }
24 |
25 | Component.compileRootComponent = true;
26 |
27 | module.exports = Component;
28 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic3.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component() {
4 | return React.createElement(
5 | "div",
6 | null,
7 | React.createElement("div", null, "Hello"),
8 | React.createElement("br", null),
9 | React.createElement("span", null, "world"),
10 | "Random text!",
11 | "More text!",
12 | 123,
13 | React.createElement("custom-element", null, "123"),
14 | );
15 | }
16 |
17 | Component.compileRootComponent = true;
18 |
19 | module.exports = Component;
20 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic30.js:
--------------------------------------------------------------------------------
1 | // props:{val: "hello world"}
2 | var React = require("react");
3 |
4 | function Component2({ children }: { children: Array }) {
5 | return React.createElement("span", null, "The child is ", children);
6 | }
7 |
8 | function Component3() {
9 | return "It works!";
10 | }
11 |
12 | function Component({ val }: { val: string }) {
13 | return React.createElement(
14 | "div",
15 | null,
16 | React.createElement(
17 | Component2,
18 | null,
19 | React.createElement("span", null, "Hello world"),
20 | React.createElement(Component3, null),
21 | ),
22 | );
23 | }
24 |
25 | Component.compileRootComponent = true;
26 |
27 | module.exports = Component;
28 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic31.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component() {
4 | return "A string!";
5 | }
6 |
7 | Component.compileRootComponent = true;
8 |
9 | module.exports = Component;
10 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic32.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function foo(): string {
4 | return "A string!";
5 | }
6 |
7 | function Component() {
8 | return foo();
9 | }
10 |
11 | Component.compileRootComponent = true;
12 |
13 | module.exports = Component;
14 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic33.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component2({ renderText }: { renderText: () => string }) {
4 | return React.createElement("span", null, "The child is ", renderText());
5 | }
6 |
7 | function Component() {
8 | const renderText = function(): string {
9 | return "Hello world!";
10 | };
11 |
12 | return React.createElement("div", null, React.createElement(Component2, {
13 | renderText: renderText
14 | }));
15 | }
16 |
17 | Component.compileRootComponent = true;
18 |
19 | module.exports = Component;
20 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic34.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component2({ node }: { node: React.Node }) {
4 | return React.createElement("span", null, "The child is ", node);
5 | }
6 |
7 | function Component() {
8 | const node = React.createElement("div", null, "Hello world");
9 | return React.createElement(
10 | "div",
11 | null,
12 | React.createElement(Component2, {
13 | node: node,
14 | }),
15 | );
16 | }
17 |
18 | Component.compileRootComponent = true;
19 |
20 | module.exports = Component;
21 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic35.js:
--------------------------------------------------------------------------------
1 | // props:{cond: true}
2 | var React = require("react");
3 |
4 | function Component2({ node }: { node: React.Node }) {
5 | return React.createElement("span", null, "The child is ", node);
6 | }
7 |
8 | function Component({ cond }: { cond: boolean }) {
9 | let node = React.createElement("div", null, "Hello world");
10 |
11 | if (cond) {
12 | node = React.createElement("span", null, "This should show!");
13 | }
14 |
15 | return React.createElement("div", null, React.createElement(Component2, {
16 | node: node
17 | }));
18 | }
19 |
20 | Component.compileRootComponent = true;
21 |
22 | module.exports = Component;
23 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic36.js:
--------------------------------------------------------------------------------
1 | // props:{cond: true}
2 | var React = require("react");
3 |
4 | function Component2({ node }: { node: string }) {
5 | return React.createElement("span", null, "The child is ", node);
6 | }
7 |
8 | function Component({ cond }: { cond: boolean }) {
9 | let node = 123
10 |
11 | if (cond) {
12 | node = 456
13 | }
14 |
15 | return React.createElement("div", null, React.createElement(Component2, {
16 | node: node
17 | }));
18 | }
19 |
20 | Component.compileRootComponent = true;
21 |
22 | module.exports = Component;
23 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic37.js:
--------------------------------------------------------------------------------
1 | // props:{cond: true}
2 | var React = require("react");
3 |
4 | function Component2({ node }: { node: string }) {
5 | return React.createElement("span", null, "The child is ", node);
6 | }
7 |
8 | function Component({ cond }: { cond: boolean }) {
9 | return React.createElement(
10 | "div",
11 | null,
12 | React.createElement(Component2, {
13 | node: cond ? 123 : 456,
14 | }),
15 | );
16 | }
17 |
18 | Component.compileRootComponent = true;
19 |
20 | module.exports = Component;
21 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic38.js:
--------------------------------------------------------------------------------
1 | // props:{cond: true}
2 | var React = require("react");
3 |
4 | function Component2({ node }: { node: React.Node }) {
5 | return React.createElement("span", null, "The child is ", node);
6 | }
7 |
8 | function Component({ cond }: { cond: boolean }) {
9 | return React.createElement(
10 | "div",
11 | null,
12 | React.createElement(Component2, {
13 | node: cond ? React.createElement("span", null, "Hello world") : React.createElement("div", null, "Hello world"),
14 | }),
15 | );
16 | }
17 |
18 | Component.compileRootComponent = true;
19 |
20 | module.exports = Component;
21 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic39.js:
--------------------------------------------------------------------------------
1 | // props:{x: "foobar"}
2 | var React = require("react");
3 |
4 | function Component2({ children }: { children: string }) {
5 | return React.createElement("span", null, "The child is ", children);
6 | }
7 |
8 | function getText(x): string {
9 | return x.toString();
10 | }
11 |
12 | function Component({ x }: { x: string }) {
13 | return React.createElement("div", null, React.createElement(Component2, null, getText(x)));
14 | }
15 |
16 | Component.compileRootComponent = true;
17 |
18 | module.exports = Component;
19 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic4.js:
--------------------------------------------------------------------------------
1 | // props:{cond: false}
2 |
3 | var React = require("react");
4 |
5 | function Component({ cond }: { cond: boolean }) {
6 | return React.createElement(
7 | "div",
8 | null,
9 | React.createElement("span", null, cond ? "Hello" : "Goodbye"),
10 | React.createElement("span", null, ``),
11 | );
12 | }
13 |
14 | Component.compileRootComponent = true;
15 |
16 | module.exports = Component;
17 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic40.js:
--------------------------------------------------------------------------------
1 | // props:{x: "foobar"}
2 | var React = require("react");
3 |
4 | function Component2({ foo, bar }: { foo: string, bar: Array }) {
5 | return React.createElement("span", null, "The child is ", foo, bar);
6 | }
7 |
8 | function getText(x): string {
9 | return x.toString();
10 | }
11 |
12 | function Component({ x }: { x: string }) {
13 | const bar = [
14 | React.createElement(
15 | "span",
16 | {
17 | key: "a",
18 | },
19 | "123",
20 | ),
21 | React.createElement(
22 | "span",
23 | {
24 | key: "b",
25 | },
26 | "456",
27 | ),
28 | ];
29 | return React.createElement(
30 | "div",
31 | null,
32 | React.createElement(Component2, {
33 | foo: getText(x),
34 | bar: bar,
35 | }),
36 | );
37 | }
38 |
39 | Component.compileRootComponent = true;
40 |
41 | module.exports = Component;
42 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic41.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component2({ x, y, z }: { x: string, y: string, z: number }) {
4 | return React.createElement(
5 | "header",
6 | null,
7 | React.createElement(
8 | "div",
9 | {
10 | className: z,
11 | },
12 | x,
13 | ),
14 | React.createElement(
15 | "span",
16 | {
17 | className: z,
18 | },
19 | y,
20 | ),
21 | );
22 | }
23 |
24 | Component2.defaultProps = {
25 | x: "default-x",
26 | y: "default-y",
27 | z: "default-class",
28 | };
29 |
30 | function Component() {
31 | return React.createElement(
32 | "div",
33 | null,
34 | React.createElement(Component2, {
35 | x: "over-ride x",
36 | y: "over-ride y",
37 | }),
38 | );
39 | }
40 |
41 | Component.compileRootComponent = true;
42 |
43 | module.exports = Component;
44 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic42.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component2() {
4 | return React.createElement("span", null, "Hello world!");
5 | }
6 |
7 | function Component() {
8 | return React.createElement(
9 | "div",
10 | null,
11 | React.createElement(Component2, null),
12 | React.createElement(Component2, null),
13 | React.createElement(Component2, null),
14 | );
15 | }
16 |
17 | Component.compileRootComponent = true;
18 |
19 | module.exports = Component;
20 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic43.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component() {
4 | let val = "foo";
5 | let count = 0;
6 | let count2 = 5;
7 |
8 | val = val + "bar";
9 | val += " test";
10 | count = count + 1;
11 | count = ++count;
12 | count2 -= 1;
13 |
14 | return React.createElement("span", null, val, ++count, --count2, typeof count);
15 | }
16 |
17 | Component.compileRootComponent = true;
18 |
19 | module.exports = Component;
20 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic44.js:
--------------------------------------------------------------------------------
1 | // props:{cond: true}
2 | var React = require("react");
3 |
4 | function Component({ cond }: { cond: boolean }) {
5 | let val = "foo";
6 | let count = 0;
7 | let count2 = 5;
8 |
9 | if (cond) {
10 | val += " test";
11 | count++;
12 | count2 -= 1;
13 | count--;
14 | }
15 |
16 | return React.createElement("span", null, val, ++count, --count2, typeof count);
17 | }
18 |
19 | Component.compileRootComponent = true;
20 |
21 | module.exports = Component;
22 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic45.js:
--------------------------------------------------------------------------------
1 | // props:{cond: true}
2 | var React = require("react");
3 |
4 | function Component({ cond }: { cond: boolean }) {
5 | let val = "foo";
6 | let count = 0;
7 | let count2 = 5;
8 |
9 | if (cond) {
10 | val += " test";
11 | }
12 |
13 | return React.createElement("span", null, val, ++count, --count2, typeof count);
14 | }
15 |
16 | Component.compileRootComponent = true;
17 |
18 | module.exports = Component;
19 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic46.js:
--------------------------------------------------------------------------------
1 | // props:{x: 10}
2 | var React = require("react");
3 |
4 | function Component2({ children }: { children: React.Node }) {
5 | return React.createElement("span", null, children);
6 | }
7 |
8 | function Component({ x }: { x: number }) {
9 | return React.createElement(
10 | "div",
11 | null,
12 | React.createElement(Component2, null, React.createElement("div", null, x)),
13 | React.createElement(Component2, null, React.createElement("div", null, x)),
14 | React.createElement(Component2, null, React.createElement("div", null, x)),
15 | );
16 | }
17 |
18 | Component.compileRootComponent = true;
19 |
20 | module.exports = Component;
21 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic47.js:
--------------------------------------------------------------------------------
1 | // props:{cond: false, x: 123}
2 | var React = require("react");
3 |
4 | function Component({x, cond}: {x: string, cond: boolean}) {
5 | var otherVal;
6 |
7 | if (cond) {
8 | otherVal = React.createElement(
9 | "span",
10 | null,
11 | "456"
12 | );
13 | }
14 |
15 | return React.createElement(
16 | "div",
17 | null,
18 | otherVal || x,
19 | "123"
20 | );
21 | }
22 |
23 | Component.compileRootComponent = true;
24 |
25 | module.exports = Component;
--------------------------------------------------------------------------------
/tests/create-element/basic/basic47b.js:
--------------------------------------------------------------------------------
1 | // props:{cond: false, x: 123}
2 | var React = require("react");
3 |
4 | function Component({x, cond}: {x: string, cond: boolean}) {
5 | var otherVal;
6 |
7 | if (cond) {
8 | otherVal = React.createElement(
9 | "span",
10 | null,
11 | "456"
12 | );
13 | }
14 |
15 | return React.createElement(
16 | "div",
17 | null,
18 | otherVal && x,
19 | "123"
20 | );
21 | }
22 |
23 | Component.compileRootComponent = true;
24 |
25 | module.exports = Component;
--------------------------------------------------------------------------------
/tests/create-element/basic/basic48.js:
--------------------------------------------------------------------------------
1 | // props:{val: "hello world"}
2 | var React = require("react");
3 |
4 | function Component2({ children }: { children: Array }) {
5 | return React.createElement("span", null, "The child is ", children);
6 | }
7 |
8 | function getChildren(val: string): string {
9 | return val;
10 | }
11 |
12 | function getChildren2(): React.Node {
13 | return React.createElement("span", null, "Hello world");
14 | }
15 |
16 | function Component({ val }: { val: string }) {
17 | return React.createElement("div", null, React.createElement(Component2, null, getChildren(val), getChildren2()));
18 | }
19 |
20 | Component.compileRootComponent = true;
21 |
22 | module.exports = Component;
23 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic49.js:
--------------------------------------------------------------------------------
1 | // props:{val: "hello world"}
2 | var React = require("react");
3 |
4 | function Component2({ children }: { children: Array }) {
5 | return React.createElement("span", null, "The child is ", children);
6 | }
7 |
8 | function getChildren(val: string): string {
9 | return val;
10 | }
11 |
12 | function getChildren2(x: string): React.Node {
13 | return React.createElement("span", null, x);
14 | }
15 |
16 | function Component({ val }: { val: string }) {
17 | return React.createElement(
18 | "div",
19 | null,
20 | React.createElement(Component2, null, getChildren(val), getChildren2("Hello world")),
21 | );
22 | }
23 |
24 | Component.compileRootComponent = true;
25 |
26 | module.exports = Component;
27 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic5.js:
--------------------------------------------------------------------------------
1 | // props:{cond: false, cond2: false, txt: '', number: 123}
2 |
3 | var React = require("react");
4 |
5 | function Component({ cond, cond2, txt, num }: { cond: boolean, cond2: boolean, txt: string, num: number }) {
6 | var numStr = num + "";
7 | var txt2 = "100".toString();
8 | var txt3 = "Hello" + "world";
9 | return React.createElement(
10 | "div",
11 | null,
12 | React.createElement("span", null, !cond && cond2 === false ? txt : numStr),
13 | React.createElement("span", null, txt2, txt3, txt),
14 | );
15 | }
16 |
17 | Component.compileRootComponent = true;
18 |
19 | module.exports = Component;
20 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic50.js:
--------------------------------------------------------------------------------
1 | // props:{cond: true, cond2: true, x: 123}
2 | var React = require("react");
3 |
4 | function Component({ x, cond, cond2 }: { x: string, cond: boolean, cond2: boolean }) {
5 | var otherVal;
6 |
7 | if (cond) {
8 | otherVal = React.createElement("span", null, x);
9 | }
10 |
11 | if (cond2) {
12 | otherVal = [React.createElement("div", null, x)];
13 | }
14 |
15 | return React.createElement("div", null, otherVal, x);
16 | }
17 |
18 | Component.compileRootComponent = true;
19 |
20 | module.exports = Component;
21 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic51.js:
--------------------------------------------------------------------------------
1 | // props:{cond: true, cond2: true, x: 123, padding: 10}
2 | var React = require("react");
3 |
4 | function getValue(x): string {
5 | return x;
6 | }
7 |
8 | function Component({ x, cond, cond2, padding }: { x: string, cond: boolean, cond2: boolean, padding: number }) {
9 | var otherVal;
10 |
11 | if (cond) {
12 | otherVal = React.createElement("span", null, cond2 ? getValue(x) : getValue("456"));
13 | }
14 |
15 | if (cond2) {
16 | otherVal = [cond ? React.createElement("div", null, x) : React.createElement("span", null, x)];
17 | }
18 |
19 | var span = React.createElement("span", null, "Span!");
20 | var span2 = React.createElement("span", null, "Span 2!");
21 | return React.createElement(
22 | "div",
23 | {
24 | style: {
25 | padding,
26 | },
27 | },
28 | otherVal,
29 | x,
30 | span && span2,
31 | );
32 | }
33 |
34 | Component.compileRootComponent = true;
35 |
36 | module.exports = Component;
37 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic52.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component2({ obj }: { obj: { val: string, el: React.Node, func: () => string, func2: () => React.Node } }) {
4 | return React.createElement("span", null, obj.val, obj.el, obj.func(), obj.func2());
5 | }
6 |
7 | function Component() {
8 | const obj = {
9 | val: "Hello world",
10 | el: React.createElement("span", null, "123"),
11 | func: (): string => "foo bar!",
12 | func2: (): React.Node => React.createElement("span", null, "456"),
13 | };
14 |
15 | return React.createElement(
16 | "div",
17 | null,
18 | React.createElement(Component2, {
19 | obj: obj,
20 | }),
21 | );
22 | }
23 |
24 | Component.compileRootComponent = true;
25 |
26 | module.exports = Component;
27 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic53.js:
--------------------------------------------------------------------------------
1 | // props:{a: "this is a", b: "this is b", c: "this is c", className: "this is className", id: "this is id"}
2 | var React = require("react");
3 |
4 | function Component({ a, b, c, ...others }: { a: string, b: string, c: string, className: string, id: string }) {
5 | return React.createElement("div", others, a, b, c);
6 | }
7 |
8 | Component.compileRootComponent = true;
9 |
10 | module.exports = Component;
11 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic54.js:
--------------------------------------------------------------------------------
1 | // props:{a: "this is a", b: "this is b", c: "this is c", id: "this is id", className: "this is className"}
2 | var React = require("react");
3 |
4 | function Component({ a, b, c, ...others }: { a: string, b: string, c: string, className: string, id: string }) {
5 | return React.createElement("div", null, a, b, c, others.id.toString(), others.className.toString());
6 | }
7 |
8 | Component.compileRootComponent = true;
9 |
10 | module.exports = Component;
11 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic55.js:
--------------------------------------------------------------------------------
1 | // props:{id: "this is id", className: "this is className"}
2 | var React = require("react");
3 |
4 | function Component({ a, b, c, ...others }: { a?: string, b?: string, c?: string, className: string, id: string }) {
5 | return React.createElement("div", null, a, b, c, others.id.toString(), others.className.toString());
6 | }
7 |
8 | Component.compileRootComponent = true;
9 |
10 | module.exports = Component;
11 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic56.js:
--------------------------------------------------------------------------------
1 | // props:{a: "this is a", b: "this is b", c: "this is c", id: "this is id", className: "this is className"}
2 | var React = require("react");
3 |
4 | function Component2({ others }: { others: { className: string, id: string } }) {
5 | return React.createElement("span", null, " ", others.id.toString(), others.className.toString());
6 | }
7 |
8 | function Component({ a, b, c, ...others }: { a: string, b: string, c: string, className: string, id: string }) {
9 | return React.createElement(
10 | "div",
11 | null,
12 | a,
13 | b,
14 | c,
15 | React.createElement(Component2, {
16 | others: others,
17 | }),
18 | );
19 | }
20 |
21 | Component.compileRootComponent = true;
22 |
23 | module.exports = Component;
24 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic57.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component() {
4 | const props = {
5 | className: "big-div",
6 | id: "big",
7 | };
8 | return React.createElement("div", props, "Hello world");
9 | }
10 |
11 | Component.compileRootComponent = true;
12 |
13 | module.exports = Component;
14 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic58.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component() {
4 | const props = {
5 | className: "big-div",
6 | id: "big",
7 | };
8 | return React.createElement(
9 | "div",
10 | Object.assign({}, props, {
11 | id: "override1",
12 | className: "override2",
13 | }),
14 | "Hello world",
15 | );
16 | }
17 |
18 | Component.compileRootComponent = true;
19 |
20 | module.exports = Component;
21 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic59.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component2({
4 | children,
5 | className,
6 | id,
7 | foo,
8 | }: {
9 | children: string,
10 | className: string,
11 | id: string,
12 | foo: string,
13 | }) {
14 | return React.createElement(
15 | "div",
16 | {
17 | className: className,
18 | id: id,
19 | },
20 | children,
21 | foo,
22 | );
23 | }
24 |
25 | function Component() {
26 | const props = {
27 | className: "big-div",
28 | id: "big",
29 | foo: "bar",
30 | };
31 | return React.createElement(
32 | Component2,
33 | Object.assign({}, props, {
34 | id: "override1",
35 | className: "override2",
36 | }),
37 | "Hello world",
38 | );
39 | }
40 |
41 | Component.compileRootComponent = true;
42 |
43 | module.exports = Component;
44 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic6.js:
--------------------------------------------------------------------------------
1 | // props:{cond: false, defaultClassName: "default-item"}
2 |
3 | var React = require("react");
4 |
5 | function Component({ cond, defaultClassName }: { cond: boolean, defaultClassName: string }) {
6 | let children = [
7 | React.createElement(
8 | "li",
9 | {
10 | key: "default",
11 | className: defaultClassName,
12 | },
13 | "Default item",
14 | ),
15 | ];
16 |
17 | if (cond) {
18 | children = [
19 | React.createElement(
20 | "li",
21 | {
22 | key: "generic",
23 | className: "generic-item",
24 | },
25 | "Generic item",
26 | ),
27 | ];
28 | }
29 |
30 | return React.createElement(
31 | "ul",
32 | {
33 | id: "list-view",
34 | className: "list",
35 | },
36 | children,
37 | );
38 | }
39 |
40 | Component.compileRootComponent = true;
41 |
42 | module.exports = Component;
43 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic60.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component2({
4 | children,
5 | className,
6 | id,
7 | foo,
8 | }: {
9 | children: string,
10 | className: string,
11 | id: string,
12 | foo: string,
13 | }) {
14 | return React.createElement(
15 | "div",
16 | {
17 | className: className,
18 | id: id,
19 | },
20 | children,
21 | foo,
22 | );
23 | }
24 |
25 | function Component() {
26 | const props = {
27 | className: "big-div",
28 | id: "big",
29 | foo: "bar",
30 | };
31 | return React.createElement(
32 | Component2,
33 | Object.assign(
34 | {
35 | id: "override1",
36 | className: "override2",
37 | },
38 | props,
39 | ),
40 | "Hello world",
41 | );
42 | }
43 |
44 | Component.compileRootComponent = true;
45 |
46 | module.exports = Component;
47 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic61.js:
--------------------------------------------------------------------------------
1 | // props:{a: "this is a", b: "this is b", c: "this is c", className: "this is className", id: "this is id"}
2 | var React = require("react");
3 |
4 | function Component2({ className, id }: { className: string, id: string }) {
5 | return React.createElement(
6 | "span",
7 | {
8 | className: className,
9 | id: id,
10 | },
11 | "Hello world",
12 | );
13 | }
14 |
15 | function Component({ a, b, c, ...others }: { a: string, b: string, c: string, className: string, id: string }) {
16 | return React.createElement(
17 | "div",
18 | Object.assign({}, others, {
19 | className: "override",
20 | children: null,
21 | }),
22 | a,
23 | b,
24 | c,
25 | React.createElement(Component2, others),
26 | );
27 | }
28 |
29 | Component.compileRootComponent = true;
30 |
31 | module.exports = Component;
32 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic62.js:
--------------------------------------------------------------------------------
1 | // props:{a: "this is a", b: "this is b", c: "this is c", className: "this is className", id: "this is id"}
2 | var React = require("react");
3 |
4 | function Component2({ className, id }: { className: string, id: string }) {
5 | return React.createElement(
6 | "span",
7 | {
8 | className: className,
9 | id: id,
10 | },
11 | "Hello world",
12 | );
13 | }
14 |
15 | function Component({ a, b, c, ...others }: { a: string, b: string, c: string, className: string, id: string }) {
16 | return React.createElement(
17 | "div",
18 | Object.assign({}, others, {
19 | className: "override",
20 | children: null,
21 | }),
22 | a,
23 | b,
24 | c,
25 | React.createElement(
26 | Component2,
27 | Object.assign(
28 | {
29 | className: "123",
30 | },
31 | others,
32 | ),
33 | ),
34 | );
35 | }
36 |
37 | Component.compileRootComponent = true;
38 |
39 | module.exports = Component;
40 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic66.js:
--------------------------------------------------------------------------------
1 | // props:{cond: false, text: "hello world 2"}
2 | var React = require("react");
3 |
4 | function Component4({ children }: { children: string }) {
5 | return React.createElement("span", null, children);
6 | }
7 |
8 | function Component3({ children }: { children: React.Node }) {
9 | return React.createElement("span", null, children);
10 | }
11 |
12 | function Component2({ children }: { children: (text: string) => React.Node }) {
13 | return React.createElement("div", null, children("Hello world"));
14 | }
15 |
16 | function Component({ cond, text }: { cond: boolean, text: string }) {
17 | var icon;
18 |
19 | if (!cond) {
20 | icon = null;
21 | } else {
22 | icon = React.createElement(Component4, null, text);
23 | }
24 |
25 | return React.createElement(
26 | "div",
27 | null,
28 | React.createElement(Component2, null, function(childText: string): React.Node {
29 | return React.createElement(Component3, null, icon, childText);
30 | }),
31 | );
32 | }
33 |
34 | Component.compileRootComponent = true;
35 |
36 | module.exports = Component;
37 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic67.js:
--------------------------------------------------------------------------------
1 | // props:{cond: false, text: "hello world 2"}
2 | var React = require("react");
3 |
4 | function Component4({ children }: { children: string }) {
5 | return React.createElement("span", null, children);
6 | }
7 |
8 | function Component3({ children }: { children: React.Node }) {
9 | return React.createElement("span", null, children);
10 | }
11 |
12 | function Component2({ children }: { children: (text: string) => React.Node }) {
13 | return React.createElement("div", null, children("Hello world"));
14 | }
15 |
16 | function Component({ cond, text }: { cond: boolean, text: string }) {
17 | var icon;
18 |
19 | if (!cond) {
20 | icon = null;
21 | } else {
22 | icon = React.createElement(Component4, null, text);
23 | }
24 |
25 | return icon
26 | ? React.createElement(
27 | "div",
28 | null,
29 | React.createElement(Component2, null, function(childText: string): React.Node {
30 | return React.createElement(Component3, null, icon, childText);
31 | }),
32 | )
33 | : null;
34 | }
35 |
36 | Component.compileRootComponent = true;
37 |
38 | module.exports = Component;
39 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic68.js:
--------------------------------------------------------------------------------
1 | // props:{cond: true}
2 | var React = require("react");
3 |
4 | function Component({ cond }: { cond: boolean }) {
5 | var Tag = cond ? "a" : "span";
6 |
7 | return React.createElement(Tag, null, "Hello world");
8 | }
9 |
10 | Component.compileRootComponent = true;
11 |
12 | module.exports = Component;
13 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic69.js:
--------------------------------------------------------------------------------
1 | // props:{cond: true}
2 | var React = require("react");
3 |
4 | function Component2() {
5 | return React.createElement("div", null, "Hello world");
6 | }
7 |
8 | function Component({ cond }: { cond: boolean }) {
9 | var Tag = cond ? Component2 : "span";
10 |
11 | return React.createElement(Tag, null, "Hello world");
12 | }
13 |
14 | Component.compileRootComponent = true;
15 |
16 | module.exports = Component;
--------------------------------------------------------------------------------
/tests/create-element/basic/basic7.js:
--------------------------------------------------------------------------------
1 | // props:{cond: false, defaultClassName: "default-item", id: "dynamic-id"}
2 |
3 | var React = require("react");
4 |
5 | function getLiBody(bodyText: string): React.Node {
6 | return React.createElement("span", null, bodyText);
7 | }
8 |
9 | function Component({ cond, defaultClassName, id }: { cond: boolean, defaultClassName: string, id: string }) {
10 | let children = [
11 | React.createElement(
12 | "li",
13 | {
14 | key: "default",
15 | className: defaultClassName,
16 | },
17 | getLiBody("Default item"),
18 | ),
19 | ];
20 |
21 | if (cond) {
22 | children = [
23 | React.createElement(
24 | "li",
25 | {
26 | key: "generic",
27 | className: "generic-item",
28 | },
29 | getLiBody("Generic item"),
30 | ),
31 | ];
32 | }
33 |
34 | return React.createElement(
35 | "ul",
36 | {
37 | id: id + "-conntected",
38 | className: "list",
39 | },
40 | children,
41 | );
42 | }
43 |
44 | Component.compileRootComponent = true;
45 |
46 | module.exports = Component;
47 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic70.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | var Component = function() {
4 | return React.createElement("span", null, "123");
5 | };
6 |
7 | Component.compileRootComponent = true;
8 |
9 | module.exports = Component;
10 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic71.js:
--------------------------------------------------------------------------------
1 | // props:{val: "hello world"}
2 | var React = require("react");
3 |
4 | var Component = function({ val }: { val: string }) {
5 | return React.createElement("span", null, val);
6 | };
7 |
8 | Component.compileRootComponent = true;
9 |
10 | module.exports = Component;
11 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic72.js:
--------------------------------------------------------------------------------
1 | // props:{val: "hello world"}
2 | var React = require("react");
3 |
4 | var Component2 = function({ children }: { children: string }) {
5 | return React.createElement("span", null, children);
6 | };
7 |
8 | var Component = function({ val }: { val: string }) {
9 | return React.createElement(Component2, null, val);
10 | };
11 |
12 | Component.compileRootComponent = true;
13 |
14 | module.exports = Component;
15 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic8.js:
--------------------------------------------------------------------------------
1 | // props:{cond: true, cond2: true, defaultClassName: "default-item", id: "dynamic-id"}
2 |
3 | var React = require("react");
4 |
5 | function getLiBody(bodyText: string): React.Node {
6 | return React.createElement("span", null, bodyText);
7 | }
8 |
9 | function getClassName(className: string, cond2: boolean): string | null {
10 | return cond2 ? className : null;
11 | }
12 |
13 | function Component({
14 | cond,
15 | cond2,
16 | defaultClassName,
17 | id,
18 | }: {
19 | cond: boolean,
20 | cond2: boolean,
21 | defaultClassName: string,
22 | id: string,
23 | }) {
24 | let children = [
25 | React.createElement(
26 | "li",
27 | {
28 | key: "default",
29 | className: defaultClassName,
30 | },
31 | getLiBody("Default item"),
32 | ),
33 | ];
34 |
35 | if (cond) {
36 | children = [
37 | React.createElement(
38 | "li",
39 | {
40 | key: "generic",
41 | className: getClassName("generic-item", cond2),
42 | },
43 | getLiBody("Generic item"),
44 | ),
45 | ];
46 | }
47 |
48 | return React.createElement(
49 | "ul",
50 | {
51 | id: id + "-conntected",
52 | className: "list",
53 | },
54 | children,
55 | );
56 | }
57 |
58 | Component.compileRootComponent = true;
59 |
60 | module.exports = Component;
61 |
--------------------------------------------------------------------------------
/tests/create-element/basic/basic9.js:
--------------------------------------------------------------------------------
1 | // props:{cond: true, cond2: true, cond3: true, cond4: false, defaultClassName: "default-item", id: "dynamic-id"}
2 |
3 | var React = require("react");
4 |
5 | function getLiBody(bodyText: string): React.Node {
6 | return React.createElement("foo", null, bodyText);
7 | }
8 |
9 | function getLiBody2(bodyText: string, cond: boolean): string | React.Node {
10 | return cond ? React.createElement("span", null, bodyText) : "No wrapping element!";
11 | }
12 |
13 | function getClassName(className: string, cond2: boolean): string | null {
14 | return cond2 ? className : null;
15 | }
16 |
17 | function Component({
18 | cond,
19 | cond2,
20 | cond3,
21 | cond4,
22 | defaultClassName,
23 | id,
24 | }: {
25 | cond: boolean,
26 | cond2: boolean,
27 | cond3: boolean,
28 | cond4: boolean,
29 | defaultClassName: string,
30 | id: string,
31 | }) {
32 | let children = [
33 | React.createElement(
34 | "li",
35 | {
36 | key: "default",
37 | className: defaultClassName,
38 | },
39 | getLiBody("Default item"),
40 | ),
41 | ];
42 |
43 | if (cond) {
44 | children = [
45 | React.createElement(
46 | "li",
47 | {
48 | key: "generic",
49 | className: cond3 ? getClassName("generic-item", cond2) : getClassName("generic-item2", cond2),
50 | },
51 | getLiBody("Generic item"),
52 | getLiBody2("Generic item", cond4),
53 | getLiBody2("Generic item", true),
54 | ),
55 | ];
56 | }
57 |
58 | return React.createElement(
59 | "ul",
60 | {
61 | id: id + "-conntected",
62 | className: "list",
63 | },
64 | children,
65 | );
66 | }
67 |
68 | Component.compileRootComponent = true;
69 |
70 | module.exports = Component;
71 |
--------------------------------------------------------------------------------
/tests/create-element/context/context.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | const MyContext = React.createContext(10);
4 |
5 | function Component() {
6 | return React.createElement(MyContext.Provider, null, React.createElement("div", null, "Hello world"));
7 | }
8 |
9 | Component.compileRootComponent = true;
10 |
11 | module.exports = Component;
12 |
--------------------------------------------------------------------------------
/tests/create-element/context/context2.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | const MyContext = React.createContext(10);
4 |
5 | function Component() {
6 | return React.createElement(
7 | MyContext.Provider,
8 | {
9 | value: 20,
10 | },
11 | React.createElement("div", null, "Hello world"),
12 | );
13 | }
14 |
15 | Component.compileRootComponent = true;
16 |
17 | module.exports = Component;
18 |
--------------------------------------------------------------------------------
/tests/create-element/context/context3.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | const MyContext = React.createContext(10);
4 |
5 | function Component() {
6 | var Provider = MyContext.Provider;
7 | return React.createElement(
8 | Provider,
9 | null,
10 | React.createElement("div", null, "Hello world"),
11 | React.createElement(React.Fragment, null, "Static text"),
12 | );
13 | }
14 |
15 | Component.compileRootComponent = true;
16 |
17 | module.exports = Component;
18 |
--------------------------------------------------------------------------------
/tests/create-element/context/context4.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | const MyContext = React.createContext(10);
4 |
5 | function Component() {
6 | const { Consumer, Provider } = MyContext;
7 | return React.createElement(
8 | Provider,
9 | null,
10 | React.createElement("div", null, "Hello world"),
11 | React.createElement(React.Fragment, null, "Static text"),
12 | React.createElement(Consumer, null, (value: number) => React.createElement("span", null, value)),
13 | );
14 | }
15 |
16 | Component.compileRootComponent = true;
17 |
18 | module.exports = Component;
19 |
--------------------------------------------------------------------------------
/tests/create-element/context/context5.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | const MyContext = React.createContext(10);
4 |
5 | function Component() {
6 | const { Consumer, Provider } = MyContext;
7 | return React.createElement(
8 | Provider,
9 | {
10 | value: 200,
11 | },
12 | React.createElement("div", null, "Hello world"),
13 | React.createElement(React.Fragment, null, "Static text"),
14 | React.createElement(Consumer, null, (value: number) => React.createElement("span", null, value)),
15 | );
16 | }
17 |
18 | Component.compileRootComponent = true;
19 |
20 | module.exports = Component;
21 |
--------------------------------------------------------------------------------
/tests/create-element/context/context6.js:
--------------------------------------------------------------------------------
1 | // props:{number2: 101}
2 |
3 | var React = require("react");
4 |
5 | const MyContext = React.createContext(10);
6 |
7 | function Component({ number2 }: { number2: 10 }) {
8 | const { Consumer, Provider } = MyContext;
9 | const number = 10;
10 | return React.createElement(
11 | Provider,
12 | {
13 | value: 200,
14 | },
15 | React.createElement("div", null, "Hello world"),
16 | React.createElement(React.Fragment, null, "Static text"),
17 | React.createElement(Consumer, null, (value: number) => React.createElement("span", null, value, number, number2)),
18 | );
19 | }
20 |
21 | Component.compileRootComponent = true;
22 |
23 | module.exports = Component;
24 |
--------------------------------------------------------------------------------
/tests/create-element/context/context7.js:
--------------------------------------------------------------------------------
1 | // props:{number2: 101}
2 |
3 | var React = require("react");
4 |
5 | const MyContext = React.createContext(10);
6 |
7 | function formatValue(val): string {
8 | return `The value is ${val}!`;
9 | }
10 |
11 | function Component({ number2 }: { number2: 10 }) {
12 | const { Consumer, Provider } = MyContext;
13 | const number = 10;
14 | return React.createElement(
15 | React.Fragment,
16 | null,
17 | React.createElement(Consumer, null, (value: number) => React.createElement("span", null, formatValue(value))),
18 | React.createElement(
19 | Provider,
20 | {
21 | value: 200,
22 | },
23 | React.createElement("div", null, "Hello world"),
24 | React.createElement(React.Fragment, null, "Static text"),
25 | React.createElement(Consumer, null, (value: number) => React.createElement("span", null, formatValue(value))),
26 | ),
27 | );
28 | }
29 |
30 | Component.compileRootComponent = true;
31 |
32 | module.exports = Component;
33 |
--------------------------------------------------------------------------------
/tests/create-element/context/context8.js:
--------------------------------------------------------------------------------
1 | // props:{number2: 101}
2 |
3 | var React = require("react");
4 |
5 | const MyContext = React.createContext(10);
6 |
7 | function formatValue(val): string {
8 | return `The value is ${val}!`;
9 | }
10 |
11 | function Component({ number2 }: { number2: 10 }) {
12 | const { Consumer, Provider } = MyContext;
13 | const number = 10;
14 | return React.createElement(
15 | React.Fragment,
16 | null,
17 | React.createElement(Consumer, null, (value: number) => {
18 | return React.createElement("span", null, formatValue(value));
19 | }),
20 | React.createElement(
21 | Provider,
22 | {
23 | value: 200,
24 | },
25 | React.createElement("div", null, "Hello world"),
26 | React.createElement(React.Fragment, null, "Static text"),
27 | React.createElement(Consumer, null, (value: number) => {
28 | return React.createElement("span", null, formatValue(value));
29 | }),
30 | ),
31 | );
32 | }
33 |
34 | Component.compileRootComponent = true;
35 |
36 | module.exports = Component;
37 |
--------------------------------------------------------------------------------
/tests/create-element/context/context9.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | const MyContext = React.createContext(10);
4 |
5 | function Component() {
6 | const { Consumer, Provider } = MyContext;
7 | const func = (value: number) => React.createElement("span", null, value);
8 |
9 | return React.createElement(
10 | Provider,
11 | null,
12 | React.createElement("div", null, "Hello world"),
13 | React.createElement(React.Fragment, null, "Static text"),
14 | React.createElement(Consumer, null, func),
15 | );
16 | }
17 |
18 | Component.compileRootComponent = true;
19 |
20 | module.exports = Component;
21 |
--------------------------------------------------------------------------------
/tests/create-element/hooks/hooks.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component() {
4 | const [value, updateValue] = React.useState("Hello world");
5 | return React.createElement("div", null, value);
6 | }
7 |
8 | Component.compileRootComponent = true;
9 |
10 | module.exports = Component;
11 |
--------------------------------------------------------------------------------
/tests/create-element/hooks/hooks2.js:
--------------------------------------------------------------------------------
1 | // Due to a Closure Compiler Bug, this code can't compile in ADVANCED
2 | // https://github.com/google/closure-compiler/issues/3145
3 | // gcc_SIMPLE
4 |
5 | var React = require("react");
6 | var { useState } = React;
7 |
8 | function Component() {
9 | const [value, updateValue] = useState("Hello world 2");
10 | return React.createElement("div", null, value, value + " - again");
11 | }
12 |
13 | Component.compileRootComponent = true;
14 |
15 | module.exports = Component;
16 |
--------------------------------------------------------------------------------
/tests/create-element/hooks/hooks3.js:
--------------------------------------------------------------------------------
1 | // Due to a Closure Compiler Bug, this code can't compile in ADVANCED
2 | // https://github.com/google/closure-compiler/issues/3145
3 | // gcc_SIMPLE
4 | // props:{cond: true}
5 |
6 | var React = require("react");
7 | var { useState } = React;
8 |
9 | function Component({ cond }: { cond: boolean }) {
10 | const [value, updateValue] = useState("Hello world 2");
11 |
12 | if (cond) {
13 | return null;
14 | }
15 | return React.createElement("div", null, value, value + " - again");
16 | }
17 |
18 | Component.compileRootComponent = true;
19 |
20 | module.exports = Component;
21 |
--------------------------------------------------------------------------------
/tests/create-element/hooks/hooks4.js:
--------------------------------------------------------------------------------
1 | // Due to a Closure Compiler Bug, this code can't compile in ADVANCED
2 | // https://github.com/google/closure-compiler/issues/3145
3 | // gcc_SIMPLE
4 | // props:{cond: false, cond2: false}
5 |
6 | var React = require("react");
7 | var { useState } = React;
8 |
9 | function Component({ cond, cond2 }: { cond: boolean, cond2: boolean }) {
10 | const [value, updateValue] = useState("Hello world 2");
11 |
12 | if (cond) {
13 | return React.createElement("span", null, value);
14 | }
15 |
16 | if (cond2) {
17 | return React.createElement("div", null, value);
18 | }
19 |
20 | return React.createElement("div", null, value, value + " - again");
21 | }
22 |
23 | Component.compileRootComponent = true;
24 |
25 | module.exports = Component;
26 |
--------------------------------------------------------------------------------
/tests/create-element/hooks/hooks5.js:
--------------------------------------------------------------------------------
1 | // Due to a Closure Compiler Bug, this code can't compile in ADVANCED
2 | // https://github.com/google/closure-compiler/issues/3145
3 | // gcc_SIMPLE
4 | // props:{cond: false}
5 |
6 | var React = require("react");
7 | var { useState } = React;
8 |
9 | function Component({ cond }: { cond: boolean }) {
10 | const [value, updateValue] = useState("Hello world 2");
11 |
12 | if (cond) {
13 | return null;
14 | }
15 | return React.createElement("div", null, value, value + " - again");
16 | }
17 |
18 | Component.compileRootComponent = true;
19 |
20 | module.exports = Component;
21 |
--------------------------------------------------------------------------------
/tests/create-element/large/large.js:
--------------------------------------------------------------------------------
1 | // props:{val1: "val1", val2: "val2", val3: "val3", val4: "val4", val5: "val5", val6: "val6", val7: "val7"}
2 | var React = require("react");
3 |
4 | function Component({
5 | val1,
6 | val2,
7 | val3,
8 | val4,
9 | val5,
10 | val6,
11 | val7,
12 | }: {
13 | val1: string,
14 | val2: string,
15 | val3: string,
16 | val4: string,
17 | val5: string,
18 | val6: string,
19 | val7: string,
20 | }) {
21 | return React.createElement(
22 | "div",
23 | {
24 | className: "container",
25 | },
26 | React.createElement(
27 | "header",
28 | {
29 | className: "header-bar",
30 | },
31 | React.createElement("h1", null, val1),
32 | ),
33 | React.createElement(
34 | "section",
35 | {
36 | className: "section-divider",
37 | },
38 | React.createElement(
39 | "div",
40 | null,
41 | React.createElement("h2", null, "Sub-section #1"),
42 | React.createElement(
43 | "div",
44 | {
45 | className: "details",
46 | },
47 | React.createElement("p", null, val2),
48 | React.createElement(
49 | "span",
50 | {
51 | className: val3,
52 | },
53 | val4,
54 | ),
55 | React.createElement(
56 | "button",
57 | {
58 | className: "button",
59 | id: "button1",
60 | },
61 | "Click here!",
62 | ),
63 | React.createElement(
64 | "button",
65 | {
66 | className: "button",
67 | id: "button1",
68 | },
69 | "Click here!",
70 | ),
71 | ),
72 | ),
73 | React.createElement(
74 | "div",
75 | null,
76 | React.createElement("h2", null, "Sub-section #2"),
77 | React.createElement(
78 | "div",
79 | {
80 | className: "details",
81 | },
82 | React.createElement("p", null, val5),
83 | React.createElement(
84 | "span",
85 | {
86 | className: val6,
87 | },
88 | val7,
89 | ),
90 | React.createElement(
91 | "button",
92 | {
93 | className: "button",
94 | id: "button1",
95 | },
96 | "Click here!",
97 | ),
98 | React.createElement(
99 | "button",
100 | {
101 | className: "button",
102 | id: "button1",
103 | },
104 | "Click here!",
105 | ),
106 | ),
107 | ),
108 | ),
109 | );
110 | }
111 |
112 | Component.compileRootComponent = true;
113 |
114 | module.exports = Component;
115 |
--------------------------------------------------------------------------------
/tests/create-element/style/style.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component() {
4 | return React.createElement(
5 | "div",
6 | {
7 | style: null,
8 | },
9 | React.createElement(
10 | "div",
11 | {
12 | style: {
13 | color: "red",
14 | backgroundColor: "black",
15 | },
16 | },
17 | "Hello world",
18 | ),
19 | );
20 | }
21 |
22 | Component.compileRootComponent = true;
23 |
24 | module.exports = Component;
25 |
--------------------------------------------------------------------------------
/tests/create-element/style/style2.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component() {
4 | return React.createElement(
5 | "div",
6 | {
7 | style: null,
8 | },
9 | React.createElement(
10 | "div",
11 | {
12 | style: {
13 | fontSize: 100,
14 | textAlign: "left",
15 | },
16 | },
17 | "Hello world",
18 | ),
19 | );
20 | }
21 |
22 | Component.compileRootComponent = true;
23 |
24 | module.exports = Component;
25 |
--------------------------------------------------------------------------------
/tests/create-element/style/style3.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component() {
4 | return React.createElement(
5 | "div",
6 | {
7 | style: null,
8 | },
9 | React.createElement(
10 | "div",
11 | {
12 | style: {
13 | fontSize: false,
14 | textAlign: null,
15 | color: undefined,
16 | padding: "",
17 | },
18 | },
19 | "Hello world",
20 | ),
21 | );
22 | }
23 |
24 | Component.compileRootComponent = true;
25 |
26 | module.exports = Component;
27 |
--------------------------------------------------------------------------------
/tests/create-element/style/style4.js:
--------------------------------------------------------------------------------
1 | // props:{styles: {color: "red", backgroundColor: "black", margin: 10}}
2 |
3 | var React = require("react");
4 |
5 | function Component({ styles }: { styles: { color: string, backgroundColor: string, margin: number } }) {
6 | return React.createElement(
7 | "div",
8 | {
9 | style: null,
10 | },
11 | React.createElement(
12 | "div",
13 | {
14 | style: styles,
15 | },
16 | "Hello world",
17 | ),
18 | );
19 | }
20 |
21 | Component.compileRootComponent = true;
22 |
23 | module.exports = Component;
24 |
--------------------------------------------------------------------------------
/tests/jsx/array/array.js:
--------------------------------------------------------------------------------
1 | // props:{items: [1,2,3,4,5]}
2 | var React = require("react");
3 |
4 | function Component({ items }: { items: Array }) {
5 | return (
6 |
7 | {items.map(
8 | (item, i: number): React.Node => (
9 | - Static item!
10 | ),
11 | )}
12 |
13 | );
14 | }
15 |
16 | Component.compileRootComponent = true;
17 |
18 | module.exports = Component;
19 |
--------------------------------------------------------------------------------
/tests/jsx/array/array2.js:
--------------------------------------------------------------------------------
1 | // props:{items: [1,2,3,4,5]}
2 | var React = require("react");
3 |
4 | function Component({ items }: { items: Array }) {
5 | return (
6 |
7 | {items.map(
8 | (item: number): React.Node => (
9 | - The item is {item}
10 | ),
11 | )}
12 |
13 | );
14 | }
15 |
16 | Component.compileRootComponent = true;
17 |
18 | module.exports = Component;
19 |
--------------------------------------------------------------------------------
/tests/jsx/array/array3.js:
--------------------------------------------------------------------------------
1 | // props:{items: [{id: 0, text: "Item 1"}, {id: 1, text: "Item 2"}, {id: 2, text: "Item 3"}, {id: 3, text: "Item 4"}, {id: 4, text: "Item 5"}]}
2 | var React = require("react");
3 |
4 | function Component({ items }: { items: Array }) {
5 | const children = Array.from(items).map(
6 | (item: { id: number, text: string }): React.Node => (
7 |
8 | The text is {item.text}!
9 |
10 | ),
11 | );
12 | return ;
13 | }
14 |
15 | Component.compileRootComponent = true;
16 |
17 | module.exports = Component;
18 |
--------------------------------------------------------------------------------
/tests/jsx/array/array4.js:
--------------------------------------------------------------------------------
1 | // props:{items: [1,2,3,4,5]}
2 | var React = require("react");
3 |
4 | function Component({ items }: { items: Array }) {
5 | const children = items
6 | .filter((item: number) => item % 2)
7 | .map((item, i: number): React.Node => Static item!);
8 | return ;
9 | }
10 |
11 | Component.compileRootComponent = true;
12 |
13 | module.exports = Component;
14 |
--------------------------------------------------------------------------------
/tests/jsx/array/array5.js:
--------------------------------------------------------------------------------
1 | // props:{items: [1,2,3,4,5]}
2 | var React = require("react");
3 |
4 | type ReactElementType = React.Node;
5 | type ItemsType = Array;
6 |
7 | function Component({ items }: { items: ItemsType }) {
8 | const children = items
9 | .filter((item: number) => item % 2)
10 | .map((item, i: number): ReactElementType => Static item!);
11 | return ;
12 | }
13 |
14 | Component.compileRootComponent = true;
15 |
16 | module.exports = Component;
17 |
--------------------------------------------------------------------------------
/tests/jsx/array/array6.js:
--------------------------------------------------------------------------------
1 | // props:{items: [1,2,3,4,5]}
2 | var React = require("react");
3 |
4 | function Component2({children}: {children: Array}) {
5 | return {children};
6 | }
7 |
8 | function Component({ items }: { items: Array }) {
9 | return (
10 |
11 | {items.map(
12 | (item: number): React.Node => (
13 | The item is {item}
14 | ),
15 | )}
16 |
17 | );
18 | }
19 |
20 | Component.compileRootComponent = true;
21 |
22 | module.exports = Component;
23 |
--------------------------------------------------------------------------------
/tests/jsx/array/array7.js:
--------------------------------------------------------------------------------
1 | // props:{items: [1,2,3,4,5]}
2 | var React = require("react");
3 |
4 | function Component2({children}: {children: Array}) {
5 | return {children};
6 | }
7 |
8 | function Component({ items }: { items: Array }) {
9 | return (
10 |
11 | {items.map(
12 | (item: number): React.Node => (
13 |
14 | The item is {item}
15 | The item is {item}
16 |
17 | ),
18 | )}
19 |
20 | );
21 | }
22 |
23 | Component.compileRootComponent = true;
24 |
25 | module.exports = Component;
26 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component() {
4 | return Hello world
5 | }
6 |
7 | Component.compileRootComponent = true;
8 |
9 | module.exports = Component;
--------------------------------------------------------------------------------
/tests/jsx/basic/basic10.js:
--------------------------------------------------------------------------------
1 | // props:{var: "This should show up!", bar: "123", cond: true, cond2: true, defaultClassName: "default-item"}
2 |
3 | var React = require("react");
4 |
5 | function foo(bar): React.Node {
6 | return [Hello world, {bar + ""}
];
7 | }
8 |
9 | function Component({
10 | cond,
11 | cond2,
12 | defaultClassName,
13 | bar
14 | }: {
15 | cond: boolean,
16 | cond2: boolean,
17 | defaultClassName: string,
18 | bar: string
19 | }) {
20 | let children = [
21 |
22 | Default item
23 |
24 | ];
25 |
26 | let elements;
27 |
28 | if (typeof cond2 !== undefined) {
29 | elements = foo(bar);
30 | }
31 |
32 | if (cond) {
33 | children = [
34 |
35 | {elements}
36 |
37 | ];
38 | }
39 |
40 | return (
41 |
44 | );
45 | }
46 |
47 | Component.compileRootComponent = true;
48 |
49 | module.exports = Component;
50 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic11.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component() {
4 | return (
5 | <>
6 | Hello world
7 | <>
8 | 123
9 | 456
10 |
11 | <>
12 | <>
13 | 789
14 | >
15 | >
16 | >
17 | >
18 | );
19 | }
20 |
21 | Component.compileRootComponent = true;
22 |
23 | module.exports = Component;
--------------------------------------------------------------------------------
/tests/jsx/basic/basic12.js:
--------------------------------------------------------------------------------
1 | // props:{bar: "123", cond: true, cond2: true, defaultClassName: "default-item"}
2 |
3 | var React = require("react");
4 |
5 | function foo(bar): Array {
6 | return [123, 456, 789, null, false, true, undefined];
7 | }
8 |
9 | function Component({
10 | cond,
11 | cond2,
12 | defaultClassName,
13 | bar
14 | }: {
15 | cond: boolean,
16 | cond2: boolean,
17 | defaultClassName: string,
18 | bar: string
19 | }) {
20 | let children = [
21 |
22 | {null}
23 |
24 | ];
25 |
26 | let elements;
27 |
28 | if (typeof cond2 !== undefined) {
29 | elements = foo(bar);
30 | }
31 |
32 | if (cond) {
33 | children = [
34 |
35 | {elements}
36 |
37 | ];
38 | }
39 |
40 | return (
41 |
44 | );
45 | }
46 |
47 | Component.compileRootComponent = true;
48 |
49 | module.exports = Component;
50 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic13.js:
--------------------------------------------------------------------------------
1 | // props:{cond: false, cond2: false, txt: '', number: 123}
2 |
3 | var React = require("react");
4 |
5 | function Component({
6 | cond,
7 | cond2,
8 | txt,
9 | num
10 | }: {
11 | cond: boolean,
12 | cond2: boolean,
13 | txt: string,
14 | num: number
15 | }) {
16 | var numStr = num + "";
17 | var txt2 = "100".toString();
18 | var txt3 = "Hello" + "world";
19 | return (
20 |
21 | {!cond && cond2 === false ? txt : numStr}
22 | alert("Haxor!")`}>
23 | {txt2}
24 | {txt3}
25 | Static text!
26 | {txt}
27 |
28 |
29 | );
30 | }
31 |
32 | Component.compileRootComponent = true;
33 |
34 | module.exports = Component;
35 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic14.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component() {
4 | function handleClick(e): void {
5 | e.target.textContent = "Worked!";
6 | }
7 |
8 | return Hello world
;
9 | }
10 |
11 | Component.compileRootComponent = true;
12 |
13 | module.exports = Component;
14 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic15.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component() {
4 | function getHandler(): (e) => void {
5 | return e => {
6 | e.target.textContent = "Worked!";
7 | };
8 | }
9 |
10 | return Hello world
11 | }
12 |
13 | Component.compileRootComponent = true;
14 |
15 | module.exports = Component;
--------------------------------------------------------------------------------
/tests/jsx/basic/basic16.js:
--------------------------------------------------------------------------------
1 | // props:{cond: false}
2 | var React = require("react");
3 |
4 | function Component({ cond }: { cond: boolean }) {
5 | const elements = [123, 456];
6 |
7 | let elements2 = [7, 8, 9];
8 |
9 | if (!cond) {
10 | elements2 = [10, 11, 12];
11 | }
12 |
13 | function getElements(): React.Node {
14 | return (
15 |
16 | {elements}
17 | {elements2}
18 |
19 | );
20 | }
21 |
22 | return {getElements()}
;
23 | }
24 |
25 | Component.compileRootComponent = true;
26 |
27 | module.exports = Component;
28 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic17.js:
--------------------------------------------------------------------------------
1 | // props:{cond1: true, cond2: false}
2 | var React = require("react");
3 |
4 | function Component({ cond1, cond2 }: { cond1: boolean, cond2: boolean }) {
5 | let className = "class1";
6 |
7 | if (cond1) {
8 | className = "class2";
9 | }
10 | if (cond2) {
11 | className = "class3";
12 | }
13 |
14 | return Hello world
;
15 | }
16 |
17 | Component.compileRootComponent = true;
18 |
19 | module.exports = Component;
20 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic18.js:
--------------------------------------------------------------------------------
1 | // props:{cond1: true, cond2: true}
2 | var React = require("react");
3 |
4 | function Component({ cond1, cond2 }: { cond1: boolean, cond2: boolean }) {
5 | let className = cond1 ? "class2" : cond2 ? "class3" : "class1";
6 |
7 | return Hello world
;
8 | }
9 |
10 | Component.compileRootComponent = true;
11 |
12 | module.exports = Component;
13 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic19.js:
--------------------------------------------------------------------------------
1 | // props:{cond1: true, cond2: true, val1: "works"}
2 | var React = require("react");
3 |
4 | function getVal2(): string {
5 | return "works2";
6 | }
7 |
8 | function Component({
9 | cond1,
10 | cond2,
11 | val1
12 | }: {
13 | cond1: boolean,
14 | cond2: boolean,
15 | val1: string
16 | }) {
17 | let className = cond1 ? val1 : cond2 ? getVal2() : "class1";
18 |
19 | return Hello world
;
20 | }
21 |
22 | Component.compileRootComponent = true;
23 |
24 | module.exports = Component;
25 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic2.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component() {
4 | return (
5 |
6 | Hello
7 | world
8 |
9 | );
10 | }
11 |
12 | Component.compileRootComponent = true;
13 |
14 | module.exports = Component;
15 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic20.js:
--------------------------------------------------------------------------------
1 | // props:{cond: true, cond2: false, txt: '', number: 123}
2 |
3 | var React = require("react");
4 |
5 | function Component({
6 | cond,
7 | cond2,
8 | txt,
9 | num
10 | }: {
11 | cond: boolean,
12 | cond2: boolean,
13 | txt: string,
14 | num: number
15 | }) {
16 | var numStr = num + "";
17 | var txt2 = "100".toString();
18 | var txt3 = "Hello" + "world";
19 | return (
20 |
21 | {!cond && cond2 === false ? txt : numStr}
22 | alert("Haxor!")`}>
23 | {txt2}
24 | {txt3}
25 | Static text!
26 | {txt}
27 |
28 |
29 | );
30 | }
31 |
32 | Component.compileRootComponent = true;
33 |
34 | module.exports = Component;
35 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic21.js:
--------------------------------------------------------------------------------
1 | // props:{cond: true, cond2: false, txt: '', number: 123}
2 |
3 | var React = require("react");
4 |
5 | type PropsType = {
6 | cond: boolean,
7 | cond2: boolean,
8 | txt: string,
9 | num: number,
10 | };
11 |
12 | function Component({ cond, cond2, txt, num }: PropsType) {
13 | var numStr = num + "";
14 | var txt2 = "100".toString();
15 | var txt3 = "Hello" + "world";
16 | return (
17 |
18 | {!cond && cond2 === false ? txt : numStr}
19 | alert("Haxor!")`}>
20 | {txt2}
21 | {txt3}
22 | Static text!
23 | {txt}
24 |
25 |
26 | );
27 | }
28 |
29 | Component.compileRootComponent = true;
30 |
31 | module.exports = Component;
32 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic22.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component2() {
4 | return Hello world!;
5 | }
6 |
7 | function Component() {
8 | return (
9 |
10 |
11 |
12 | );
13 | }
14 |
15 | Component.compileRootComponent = true;
16 |
17 | module.exports = Component;
18 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic23.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component2({ x }: { x: number }) {
4 | return The number is {x};
5 | }
6 |
7 | function Component() {
8 | return (
9 |
10 |
11 |
12 | );
13 | }
14 |
15 | Component.compileRootComponent = true;
16 |
17 | module.exports = Component;
18 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic24.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component2({ children }: { children: string }) {
4 | return The child is {children};
5 | }
6 |
7 | function Component() {
8 | return (
9 |
10 | Hello world
11 |
12 | );
13 | }
14 |
15 | Component.compileRootComponent = true;
16 |
17 | module.exports = Component;
18 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic25.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component2({ children }: { children: Array }) {
4 | return The child is {children};
5 | }
6 |
7 | function Component() {
8 | return (
9 |
10 | {[1, 2, 3]}
11 |
12 | );
13 | }
14 |
15 | Component.compileRootComponent = true;
16 |
17 | module.exports = Component;
18 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic26.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component2({ children }: { children: {text: string} }) {
4 | return The child is {children.text};
5 | }
6 |
7 | function Component() {
8 | return (
9 |
10 | {{text: "Hello world!"}}
11 |
12 | );
13 | }
14 |
15 | Component.compileRootComponent = true;
16 |
17 | module.exports = Component;
18 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic27.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component2({ children }: { children: React.Node }) {
4 | return The child is {children};
5 | }
6 |
7 | function Component() {
8 | return (
9 |
10 |
11 | Hello world
12 |
13 |
14 | );
15 | }
16 |
17 | Component.compileRootComponent = true;
18 |
19 | module.exports = Component;
20 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic28.js:
--------------------------------------------------------------------------------
1 | // props:{val: "hello world"}
2 | var React = require("react");
3 |
4 | function Component2({ children }: { children: React.Node }) {
5 | return The child is {children};
6 | }
7 |
8 | function Component({ val }: { val: string }) {
9 | return (
10 |
11 |
12 | Hello world
13 | {val}
14 |
15 |
16 | );
17 | }
18 |
19 | Component.compileRootComponent = true;
20 |
21 | module.exports = Component;
22 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic29.js:
--------------------------------------------------------------------------------
1 | // props:{val: "hello world"}
2 | var React = require("react");
3 |
4 | function Component2({ children }: { children: Array }) {
5 | return The child is {children};
6 | }
7 |
8 | function Component({ val }: { val: string }) {
9 | return (
10 |
11 |
12 | Hello world
13 | {val}
14 | {null}
15 | {undefined}
16 | {"Hello world"}
17 | {false}
18 |
19 |
20 | );
21 | }
22 |
23 | Component.compileRootComponent = true;
24 |
25 | module.exports = Component;
26 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic3.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component() {
4 | return (
5 |
6 |
Hello
7 |
8 |
world
9 | Random text!
10 | {"More text!"}
11 | {123}
12 |
123
13 |
14 | );
15 | }
16 |
17 | Component.compileRootComponent = true;
18 |
19 | module.exports = Component;
20 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic30.js:
--------------------------------------------------------------------------------
1 | // props:{val: "hello world"}
2 | var React = require("react");
3 |
4 | function Component2({ children }: { children: Array }) {
5 | return The child is {children};
6 | }
7 |
8 | function Component3() {
9 | return "It works!";
10 | }
11 |
12 | function Component({ val }: { val: string }) {
13 | return (
14 |
15 |
16 | Hello world
17 |
18 |
19 |
20 | );
21 | }
22 |
23 | Component.compileRootComponent = true;
24 |
25 | module.exports = Component;
26 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic31.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component() {
4 | return "A string!";
5 | }
6 |
7 | Component.compileRootComponent = true;
8 |
9 | module.exports = Component;
10 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic32.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function foo(): string {
4 | return "A string!";
5 | }
6 |
7 | function Component() {
8 | return foo();
9 | }
10 |
11 | Component.compileRootComponent = true;
12 |
13 | module.exports = Component;
14 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic33.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component2({ renderText }: { renderText: () => string }) {
4 | return The child is {renderText()};
5 | }
6 |
7 | function Component() {
8 | const renderText = function(): string {
9 | return "Hello world!";
10 | };
11 |
12 | return (
13 |
14 |
15 |
16 | );
17 | }
18 |
19 | Component.compileRootComponent = true;
20 |
21 | module.exports = Component;
22 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic34.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component2({ node }: { node: React.Node }) {
4 | return The child is {node};
5 | }
6 |
7 | function Component() {
8 | const node = Hello world
;
9 |
10 | return (
11 |
12 |
13 |
14 | );
15 | }
16 |
17 | Component.compileRootComponent = true;
18 |
19 | module.exports = Component;
20 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic35.js:
--------------------------------------------------------------------------------
1 | // props:{cond: true}
2 | var React = require("react");
3 |
4 | function Component2({ node }: { node: React.Node }) {
5 | return The child is {node};
6 | }
7 |
8 | function Component({ cond }: { cond: boolean }) {
9 | let node = Hello world
;
10 |
11 | if (cond) {
12 | node = This should show!;
13 | }
14 |
15 | return (
16 |
17 |
18 |
19 | );
20 | }
21 |
22 | Component.compileRootComponent = true;
23 |
24 | module.exports = Component;
25 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic36.js:
--------------------------------------------------------------------------------
1 | // props:{cond: true}
2 | var React = require("react");
3 |
4 | function Component2({ node }: { node: string }) {
5 | return The child is {node};
6 | }
7 |
8 | function Component({ cond }: { cond: boolean }) {
9 | let node = 123
10 |
11 | if (cond) {
12 | node = 456
13 | }
14 |
15 | return (
16 |
17 |
18 |
19 | );
20 | }
21 |
22 | Component.compileRootComponent = true;
23 |
24 | module.exports = Component;
25 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic37.js:
--------------------------------------------------------------------------------
1 | // props:{cond: true}
2 | var React = require("react");
3 |
4 | function Component2({ node }: { node: string }) {
5 | return The child is {node};
6 | }
7 |
8 | function Component({ cond }: { cond: boolean }) {
9 | return (
10 |
11 |
12 |
13 | );
14 | }
15 |
16 | Component.compileRootComponent = true;
17 |
18 | module.exports = Component;
19 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic38.js:
--------------------------------------------------------------------------------
1 | // props:{cond: true}
2 | var React = require("react");
3 |
4 | function Component2({ node }: { node: React.Node }) {
5 | return The child is {node};
6 | }
7 |
8 | function Component({ cond }: { cond: boolean }) {
9 | return (
10 |
11 |
Hello world : Hello world
} />
12 |
13 | );
14 | }
15 |
16 | Component.compileRootComponent = true;
17 |
18 | module.exports = Component;
19 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic39.js:
--------------------------------------------------------------------------------
1 | // props:{x: "foobar"}
2 | var React = require("react");
3 |
4 | function Component2({ children }: { children: string }) {
5 | return The child is {children};
6 | }
7 |
8 | function getText(x): string {
9 | return x.toString();
10 | }
11 |
12 | function Component({ x }: { x: string }) {
13 | return (
14 |
15 | {getText(x)}
16 |
17 | );
18 | }
19 |
20 | Component.compileRootComponent = true;
21 |
22 | module.exports = Component;
23 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic4.js:
--------------------------------------------------------------------------------
1 | // props:{cond: false}
2 |
3 | var React = require("react");
4 |
5 | function Component({ cond }: { cond: boolean }) {
6 | return (
7 |
8 | {cond ? "Hello" : "Goodbye"}
9 | {``}
10 |
11 | );
12 | }
13 |
14 | Component.compileRootComponent = true;
15 |
16 | module.exports = Component;
17 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic40.js:
--------------------------------------------------------------------------------
1 | // props:{x: "foobar"}
2 | var React = require("react");
3 |
4 | function Component2({ foo, bar }: { foo: string, bar: Array }) {
5 | return (
6 |
7 | The child is {foo}
8 | {bar}
9 |
10 | );
11 | }
12 |
13 | function getText(x): string {
14 | return x.toString();
15 | }
16 |
17 | function Component({ x }: { x: string }) {
18 | const bar = [123, 456];
19 | return (
20 |
21 |
22 |
23 | );
24 | }
25 |
26 | Component.compileRootComponent = true;
27 |
28 | module.exports = Component;
29 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic41.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component2({ x, y, z }: { x: string, y: string, z: number }) {
4 | return (
5 |
9 | );
10 | }
11 |
12 | Component2.defaultProps = {
13 | x: "default-x",
14 | y: "default-y",
15 | z: "default-class",
16 | };
17 |
18 | function Component() {
19 | return (
20 |
21 |
22 |
23 | );
24 | }
25 |
26 | Component.compileRootComponent = true;
27 |
28 | module.exports = Component;
29 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic42.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component2() {
4 | return Hello world!;
5 | }
6 |
7 | function Component() {
8 | return (
9 |
10 |
11 |
12 |
13 |
14 | );
15 | }
16 |
17 | Component.compileRootComponent = true;
18 |
19 | module.exports = Component;
20 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic43.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component() {
4 | let val = "foo";
5 | let count = 0;
6 | let count2 = 5;
7 |
8 | val = val + "bar";
9 | val += " test";
10 | count = count + 1;
11 | count = ++count;
12 | count2 -= 1;
13 |
14 | return {val}{++count}{--count2}{typeof count}
15 | }
16 |
17 | Component.compileRootComponent = true;
18 |
19 | module.exports = Component;
20 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic44.js:
--------------------------------------------------------------------------------
1 | // props:{cond: true}
2 | var React = require("react");
3 |
4 | function Component({ cond }: { cond: boolean }) {
5 | let val = "foo";
6 | let count = 0;
7 | let count2 = 5;
8 |
9 | if (cond) {
10 | val += " test";
11 | count++;
12 | count2 -= 1;
13 | count--;
14 | }
15 |
16 | return (
17 |
18 | {val}
19 | {++count}
20 | {--count2}
21 | {typeof count}
22 |
23 | );
24 | }
25 |
26 | Component.compileRootComponent = true;
27 |
28 | module.exports = Component;
29 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic45.js:
--------------------------------------------------------------------------------
1 | // props:{cond: true}
2 | var React = require("react");
3 |
4 | function Component({ cond }: { cond: boolean }) {
5 | let val = "foo";
6 | let count = 0;
7 | let count2 = 5;
8 |
9 | if (cond) {
10 | val += " test";
11 | }
12 |
13 | return (
14 |
15 | {val}
16 | {++count}
17 | {--count2}
18 | {typeof count}
19 |
20 | );
21 | }
22 |
23 | Component.compileRootComponent = true;
24 |
25 | module.exports = Component;
26 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic46.js:
--------------------------------------------------------------------------------
1 | // props:{x: 10}
2 | var React = require("react");
3 |
4 | function Component2({ children }: { children: React.Node }) {
5 | return {children};
6 | }
7 |
8 | function Component({ x }: { x: number }) {
9 | return (
10 |
11 |
12 | {x}
13 |
14 |
15 | {x}
16 |
17 |
18 | {x}
19 |
20 |
21 | );
22 | }
23 |
24 | Component.compileRootComponent = true;
25 |
26 | module.exports = Component;
27 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic47.js:
--------------------------------------------------------------------------------
1 | // props:{cond: true, x: 123}
2 | var React = require("react");
3 |
4 | function Component({x, cond}: {x: string, cond: boolean}) {
5 | var otherVal;
6 |
7 | if (cond) {
8 | otherVal = 456;
9 | }
10 |
11 | return {otherVal || x}123
12 | }
13 |
14 | Component.compileRootComponent = true;
15 |
16 | module.exports = Component;
--------------------------------------------------------------------------------
/tests/jsx/basic/basic47b.js:
--------------------------------------------------------------------------------
1 | // props:{cond: true, x: 123}
2 | var React = require("react");
3 |
4 | function Component({x, cond}: {x: string, cond: boolean}) {
5 | var otherVal;
6 |
7 | if (cond) {
8 | otherVal = 456;
9 | }
10 |
11 | return {otherVal && x}123
12 | }
13 |
14 | Component.compileRootComponent = true;
15 |
16 | module.exports = Component;
--------------------------------------------------------------------------------
/tests/jsx/basic/basic48.js:
--------------------------------------------------------------------------------
1 | // props:{val: "hello world"}
2 | var React = require("react");
3 |
4 | function Component2({ children }: { children: Array }) {
5 | return The child is {children};
6 | }
7 |
8 | function getChildren(val: string): string {
9 | return val;
10 | }
11 |
12 | function getChildren2(): React.Node {
13 | return Hello world;
14 | }
15 |
16 | function Component({ val }: { val: string }) {
17 | return (
18 |
19 |
20 | {getChildren(val)}
21 | {getChildren2()}
22 |
23 |
24 | );
25 | }
26 |
27 | Component.compileRootComponent = true;
28 |
29 | module.exports = Component;
30 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic49.js:
--------------------------------------------------------------------------------
1 | // props:{val: "hello world"}
2 | var React = require("react");
3 |
4 | function Component2({ children }: { children: Array }) {
5 | return The child is {children};
6 | }
7 |
8 | function getChildren(val: string): string {
9 | return val;
10 | }
11 |
12 | function getChildren2(x: string): React.Node {
13 | return {x};
14 | }
15 |
16 | function Component({ val }: { val: string }) {
17 | return (
18 |
19 |
20 | {getChildren(val)}
21 | {getChildren2("Hello world")}
22 |
23 |
24 | );
25 | }
26 |
27 | Component.compileRootComponent = true;
28 |
29 | module.exports = Component;
30 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic5.js:
--------------------------------------------------------------------------------
1 | // props:{cond: false, cond2: false, txt: '', number: 123}
2 |
3 | var React = require("react");
4 |
5 | function Component({
6 | cond,
7 | cond2,
8 | txt,
9 | num
10 | }: {
11 | cond: boolean,
12 | cond2: boolean,
13 | txt: string,
14 | num: number
15 | }) {
16 | var numStr = num + "";
17 | var txt2 = "100".toString();
18 | var txt3 = "Hello" + "world";
19 | return (
20 |
21 | {!cond && cond2 === false ? txt : numStr}
22 |
23 | {txt2}
24 | {txt3}
25 | {txt}
26 |
27 |
28 | );
29 | }
30 |
31 | Component.compileRootComponent = true;
32 |
33 | module.exports = Component;
34 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic50.js:
--------------------------------------------------------------------------------
1 | // props:{cond: true, cond2: true, x: 123}
2 | var React = require("react");
3 |
4 | function Component({ x, cond, cond2 }: { x: string, cond: boolean, cond2: boolean }) {
5 | var otherVal;
6 |
7 | if (cond) {
8 | otherVal = {x};
9 | }
10 |
11 | if (cond2) {
12 | otherVal = [{x}
];
13 | }
14 |
15 | return (
16 |
17 | {otherVal}
18 | {x}
19 |
20 | );
21 | }
22 |
23 | Component.compileRootComponent = true;
24 |
25 | module.exports = Component;
26 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic51.js:
--------------------------------------------------------------------------------
1 | // props:{cond: true, cond2: true, x: 123, padding: 10}
2 | var React = require("react");
3 |
4 | function getValue(x): string {
5 | return x;
6 | }
7 |
8 | function Component({ x, cond, cond2, padding }: { x: string, cond: boolean, cond2: boolean, padding: number }) {
9 | var otherVal;
10 |
11 | if (cond) {
12 | otherVal = {cond2 ? getValue(x) : getValue("456")};
13 | }
14 |
15 | if (cond2) {
16 | otherVal = [cond ? {x}
: {x}];
17 | }
18 |
19 | var span = Span!;
20 | var span2 = Span 2!;
21 |
22 | return (
23 |
24 | {otherVal}
25 | {x}
26 | {span && span2}
27 |
28 | );
29 | }
30 |
31 | Component.compileRootComponent = true;
32 |
33 | module.exports = Component;
34 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic52.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component2({ obj }: { obj: { val: string, el: React.Node, func: () => string, func2: () => React.Node } }) {
4 | return (
5 |
6 | {obj.val}
7 | {obj.el}
8 | {obj.func()}
9 | {obj.func2()}
10 |
11 | );
12 | }
13 |
14 | function Component() {
15 | const obj = {
16 | val: "Hello world",
17 | el: 123,
18 | func: (): string => "foo bar!",
19 | func2: (): React.Node => 456,
20 | };
21 |
22 | return (
23 |
24 |
25 |
26 | );
27 | }
28 |
29 | Component.compileRootComponent = true;
30 |
31 | module.exports = Component;
32 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic53.js:
--------------------------------------------------------------------------------
1 | // props:{a: "this is a", b: "this is b", c: "this is c", className: "this is className", id: "this is id"}
2 | var React = require("react");
3 |
4 | function Component({ a, b, c, ...others }: { a: string, b: string, c: string, className: string, id: string }) {
5 | return (
6 |
7 | {a}
8 | {b}
9 | {c}
10 |
11 | );
12 | }
13 |
14 | Component.compileRootComponent = true;
15 |
16 | module.exports = Component;
17 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic54.js:
--------------------------------------------------------------------------------
1 | // props:{a: "this is a", b: "this is b", c: "this is c", id: "this is id", className: "this is className"}
2 | var React = require("react");
3 |
4 | function Component({ a, b, c, ...others }: { a: string, b: string, c: string, className: string, id: string }) {
5 | return (
6 |
7 | {a}
8 | {b}
9 | {c}
10 | {others.id.toString()}
11 | {others.className.toString()}
12 |
13 | );
14 | }
15 |
16 | Component.compileRootComponent = true;
17 |
18 | module.exports = Component;
19 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic55.js:
--------------------------------------------------------------------------------
1 | // props:{id: "this is id", className: "this is className"}
2 | var React = require("react");
3 |
4 | function Component({ a, b, c, ...others }: { a?: string, b?: string, c?: string, className: string, id: string }) {
5 | return (
6 |
7 | {a}
8 | {b}
9 | {c}
10 | {others.id.toString()}
11 | {others.className.toString()}
12 |
13 | );
14 | }
15 |
16 | Component.compileRootComponent = true;
17 |
18 | module.exports = Component;
19 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic56.js:
--------------------------------------------------------------------------------
1 | // props:{a: "this is a", b: "this is b", c: "this is c", id: "this is id", className: "this is className"}
2 | var React = require("react");
3 |
4 | function Component2({ others }: { others: { className: string, id: string } }) {
5 | return (
6 |
7 | {" "}
8 | {others.id.toString()}
9 | {others.className.toString()}
10 |
11 | );
12 | }
13 |
14 | function Component({ a, b, c, ...others }: { a: string, b: string, c: string, className: string, id: string }) {
15 | return (
16 |
17 | {a}
18 | {b}
19 | {c}
20 |
21 |
22 | );
23 | }
24 |
25 | Component.compileRootComponent = true;
26 |
27 | module.exports = Component;
28 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic57.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component() {
4 | const props = {
5 | className: "big-div",
6 | id: "big",
7 | };
8 | return Hello world
9 | }
10 |
11 | Component.compileRootComponent = true;
12 |
13 | module.exports = Component;
--------------------------------------------------------------------------------
/tests/jsx/basic/basic58.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component() {
4 | const props = {
5 | className: "big-div",
6 | id: "big",
7 | };
8 | return Hello world
9 | }
10 |
11 | Component.compileRootComponent = true;
12 |
13 | module.exports = Component;
--------------------------------------------------------------------------------
/tests/jsx/basic/basic59.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component2({ children, className, id, foo }: { children: string, className: string, id: string, foo: string }) {
4 | return (
5 |
6 | {children}{foo}
7 |
8 | );
9 | }
10 |
11 | function Component() {
12 | const props = {
13 | className: "big-div",
14 | id: "big",
15 | foo: "bar",
16 | };
17 | return (
18 |
19 | Hello world
20 |
21 | );
22 | }
23 |
24 | Component.compileRootComponent = true;
25 |
26 | module.exports = Component;
27 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic6.js:
--------------------------------------------------------------------------------
1 | // props:{cond: false, defaultClassName: "default-item"}
2 |
3 | var React = require("react");
4 |
5 | function Component({ cond, defaultClassName }: { cond: boolean, defaultClassName: string }) {
6 | let children = [
7 |
8 | Default item
9 |
10 | ];
11 |
12 | if (cond) {
13 | children = [
14 |
15 | Generic item
16 |
17 | ];
18 | }
19 |
20 | return (
21 |
24 | );
25 | }
26 |
27 | Component.compileRootComponent = true;
28 |
29 | module.exports = Component;
30 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic60.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component2({ children, className, id, foo }: { children: string, className: string, id: string, foo: string }) {
4 | return (
5 |
6 | {children}{foo}
7 |
8 | );
9 | }
10 |
11 | function Component() {
12 | const props = {
13 | className: "big-div",
14 | id: "big",
15 | foo: "bar",
16 | };
17 | return (
18 |
19 | Hello world
20 |
21 | );
22 | }
23 |
24 | Component.compileRootComponent = true;
25 |
26 | module.exports = Component;
27 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic61.js:
--------------------------------------------------------------------------------
1 | // props:{a: "this is a", b: "this is b", c: "this is c", className: "this is className", id: "this is id"}
2 | var React = require("react");
3 |
4 | function Component2({ className, id }: { className: string, id: string }) {
5 | return (
6 |
7 | Hello world
8 |
9 | );
10 | }
11 |
12 | function Component({ a, b, c, ...others }: { a: string, b: string, c: string, className: string, id: string }) {
13 | return (
14 |
15 | {a}
16 | {b}
17 | {c}
18 |
19 |
20 | );
21 | }
22 |
23 | Component.compileRootComponent = true;
24 |
25 | module.exports = Component;
26 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic62.js:
--------------------------------------------------------------------------------
1 | // props:{a: "this is a", b: "this is b", c: "this is c", className: "this is className", id: "this is id"}
2 | var React = require("react");
3 |
4 | function Component2({ className, id }: { className: string, id: string }) {
5 | return (
6 |
7 | Hello world
8 |
9 | );
10 | }
11 |
12 | function Component({ a, b, c, ...others }: { a: string, b: string, c: string, className: string, id: string }) {
13 | return (
14 |
15 | {a}
16 | {b}
17 | {c}
18 |
19 |
20 | );
21 | }
22 |
23 | Component.compileRootComponent = true;
24 |
25 | module.exports = Component;
26 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic63.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component2({ children }: { children: (text: string) => React.Node }) {
4 | return {children("Hello world")}
;
5 | }
6 |
7 | function Component() {
8 | return (
9 |
10 |
11 | {function(text: string): React.Node {
12 | return {text};
13 | }}
14 |
15 |
16 | );
17 | }
18 |
19 | Component.compileRootComponent = true;
20 |
21 | module.exports = Component;
22 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic64.js:
--------------------------------------------------------------------------------
1 | // props:{cond: false, text: "hello world"}
2 | var React = require("react");
3 |
4 | function Component2({ children }: { children: (text: string) => React.Node }) {
5 | return {children("Hello world")}
;
6 | }
7 |
8 | function Component({ cond, text }: { cond: boolean, text: string }) {
9 | var icon;
10 |
11 | if (!cond) {
12 | icon = null;
13 | } else {
14 | icon = {text}
;
15 | }
16 |
17 | return (
18 |
19 |
20 | {function(childText: string): React.Node {
21 | return (
22 |
23 | {icon}
24 | {childText}
25 |
26 | );
27 | }}
28 |
29 |
30 | );
31 | }
32 |
33 | Component.compileRootComponent = true;
34 |
35 | module.exports = Component;
36 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic64b.js:
--------------------------------------------------------------------------------
1 | // props:{cond: true, text: "hello world ##"}
2 | var React = require("react");
3 |
4 | function Component2({ children }: { children: (text: string) => React.Node }) {
5 | return {children("Hello world")}
;
6 | }
7 |
8 | function Component({ cond, text }: { cond: boolean, text: string }) {
9 | var icon;
10 |
11 | if (!cond) {
12 | icon = null;
13 | } else {
14 | icon = {text}
;
15 | }
16 |
17 | return (
18 |
19 |
20 | {function(childText: string): React.Node {
21 | return (
22 |
23 | {icon}
24 | {childText}
25 |
26 | );
27 | }}
28 |
29 |
30 | );
31 | }
32 |
33 | Component.compileRootComponent = true;
34 |
35 | module.exports = Component;
36 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic65.js:
--------------------------------------------------------------------------------
1 | // props:{cond: false, text: "hello world 2"}
2 | var React = require("react");
3 |
4 | function Component3({ children }: { children: React.Node }) {
5 | return {children};
6 | }
7 |
8 | function Component2({ children }: { children: (text: string) => React.Node }) {
9 | return {children("Hello world")}
;
10 | }
11 |
12 | function Component({ cond, text }: { cond: boolean, text: string }) {
13 | var icon;
14 |
15 | if (!cond) {
16 | icon = null;
17 | } else {
18 | icon = {text}
;
19 | }
20 |
21 | return (
22 |
23 |
24 | {function(childText: string): React.Node {
25 | return (
26 |
27 | {icon}
28 | {childText}
29 |
30 | );
31 | }}
32 |
33 |
34 | );
35 | }
36 |
37 | Component.compileRootComponent = true;
38 |
39 | module.exports = Component;
40 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic65b.js:
--------------------------------------------------------------------------------
1 | // props:{cond: true, text: "hello world 2"}
2 | var React = require("react");
3 |
4 | function Component3({ children }: { children: React.Node }) {
5 | return {children};
6 | }
7 |
8 | function Component2({ children }: { children: (text: string) => React.Node }) {
9 | return {children("Hello world")}
;
10 | }
11 |
12 | function Component({ cond, text }: { cond: boolean, text: string }) {
13 | var icon;
14 |
15 | if (!cond) {
16 | icon = null;
17 | } else {
18 | icon = {text}
;
19 | }
20 |
21 | return (
22 |
23 |
24 | {function(childText: string): React.Node {
25 | return (
26 |
27 | {icon}
28 | {childText}
29 |
30 | );
31 | }}
32 |
33 |
34 | );
35 | }
36 |
37 | Component.compileRootComponent = true;
38 |
39 | module.exports = Component;
40 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic66.js:
--------------------------------------------------------------------------------
1 | // props:{cond: false, text: "hello world 2"}
2 | var React = require("react");
3 |
4 | function Component4({ children }: { children: string }) {
5 | return {children};
6 | }
7 |
8 | function Component3({ children }: { children: React.Node }) {
9 | return {children};
10 | }
11 |
12 | function Component2({ children }: { children: (text: string) => React.Node }) {
13 | return {children("Hello world")}
;
14 | }
15 |
16 | function Component({ cond, text }: { cond: boolean, text: string }) {
17 | var icon;
18 |
19 | if (!cond) {
20 | icon = null;
21 | } else {
22 | icon = {text};
23 | }
24 |
25 | return (
26 |
27 |
28 | {function(childText: string): React.Node {
29 | return (
30 |
31 | {icon}
32 | {childText}
33 |
34 | );
35 | }}
36 |
37 |
38 | );
39 | }
40 |
41 | Component.compileRootComponent = true;
42 |
43 | module.exports = Component;
44 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic67.js:
--------------------------------------------------------------------------------
1 | // props:{cond: false, text: "hello world 2"}
2 | var React = require("react");
3 |
4 | function Component4({ children }: { children: string }) {
5 | return {children};
6 | }
7 |
8 | function Component3({ children }: { children: React.Node }) {
9 | return {children};
10 | }
11 |
12 | function Component2({ children }: { children: (text: string) => React.Node }) {
13 | return {children("Hello world")}
;
14 | }
15 |
16 | function Component({ cond, text }: { cond: boolean, text: string }) {
17 | var icon;
18 |
19 | if (!cond) {
20 | icon = null;
21 | } else {
22 | icon = {text};
23 | }
24 |
25 | return icon ? (
26 |
27 |
28 | {function(childText: string): React.Node {
29 | return (
30 |
31 | {icon}
32 | {childText}
33 |
34 | );
35 | }}
36 |
37 |
38 | ) : null;
39 | }
40 |
41 | Component.compileRootComponent = true;
42 |
43 | module.exports = Component;
44 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic68.js:
--------------------------------------------------------------------------------
1 | // props:{cond: true}
2 | var React = require("react");
3 |
4 | function Component({ cond }: { cond: boolean }) {
5 | const Tag = cond ? "a" : "span";
6 |
7 | return Hello world;
8 | }
9 |
10 | Component.compileRootComponent = true;
11 |
12 | module.exports = Component;
--------------------------------------------------------------------------------
/tests/jsx/basic/basic69.js:
--------------------------------------------------------------------------------
1 | // props:{cond: true}
2 | var React = require("react");
3 |
4 | function Component2() {
5 | return Hello world;
6 | }
7 |
8 | function Component({ cond }: { cond: boolean }) {
9 | const Tag = cond ? Component2 : "span";
10 |
11 | return Hello world;
12 | }
13 |
14 | Component.compileRootComponent = true;
15 |
16 | module.exports = Component;
--------------------------------------------------------------------------------
/tests/jsx/basic/basic7.js:
--------------------------------------------------------------------------------
1 | // props:{cond: false, defaultClassName: "default-item", id: "dynamic-id"}
2 |
3 | var React = require("react");
4 |
5 | function getLiBody(bodyText: string): React.Node {
6 | return {bodyText};
7 | }
8 |
9 | function Component({ cond, defaultClassName, id }: { cond: boolean, defaultClassName: string, id: string }) {
10 | let children = [
11 |
12 | {getLiBody("Default item")}
13 |
14 | ];
15 |
16 | if (cond) {
17 | children = [
18 |
19 | {getLiBody("Generic item")}
20 |
21 | ];
22 | }
23 |
24 | return (
25 |
28 | );
29 | }
30 |
31 | Component.compileRootComponent = true;
32 |
33 | module.exports = Component;
34 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic70.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | var Component = function() {
4 | return 123
5 | };
6 |
7 | Component.compileRootComponent = true;
8 |
9 | module.exports = Component;
10 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic71.js:
--------------------------------------------------------------------------------
1 | // props:{val: "hello world"}
2 | var React = require("react");
3 |
4 | var Component = function({ val }: { val: string }) {
5 | return {val}
6 | };
7 |
8 | Component.compileRootComponent = true;
9 |
10 | module.exports = Component;
11 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic72.js:
--------------------------------------------------------------------------------
1 | // props:{val: "hello world"}
2 | var React = require("react");
3 |
4 | var Component2 = function({ children }: { children: string }) {
5 | return {children}
6 | };
7 |
8 | var Component = function({ val }: { val: string }) {
9 | return {val}
10 | };
11 |
12 | Component.compileRootComponent = true;
13 |
14 | module.exports = Component;
15 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic8.js:
--------------------------------------------------------------------------------
1 | // props:{cond: true, cond2: true, defaultClassName: "default-item", id: "dynamic-id"}
2 |
3 | var React = require("react");
4 |
5 | function getLiBody(bodyText: string): React.Node {
6 | return {bodyText};
7 | }
8 |
9 | function getClassName(className: string, cond2: boolean): string | null {
10 | return cond2 ? className : null;
11 | }
12 |
13 | function Component({
14 | cond,
15 | cond2,
16 | defaultClassName,
17 | id
18 | }: {
19 | cond: boolean,
20 | cond2: boolean,
21 | defaultClassName: string,
22 | id: string
23 | }) {
24 | let children = [
25 |
26 | {getLiBody("Default item")}
27 |
28 | ];
29 |
30 | if (cond) {
31 | children = [
32 |
33 | {getLiBody("Generic item")}
34 |
35 | ];
36 | }
37 |
38 | return (
39 |
42 | );
43 | }
44 |
45 | Component.compileRootComponent = true;
46 |
47 | module.exports = Component;
48 |
--------------------------------------------------------------------------------
/tests/jsx/basic/basic9.js:
--------------------------------------------------------------------------------
1 | // props:{cond: true, cond2: true, cond3: true, cond4: false, defaultClassName: "default-item", id: "dynamic-id"}
2 |
3 | var React = require("react");
4 |
5 | function getLiBody(bodyText: string): React.Node {
6 | return {bodyText};
7 | }
8 |
9 | function getLiBody2(bodyText: string, cond: boolean): string | React.Node {
10 | return cond ? {bodyText} : "No wrapping element!";
11 | }
12 |
13 | function getClassName(className: string, cond2: boolean): string | null {
14 | return cond2 ? className : null;
15 | }
16 |
17 | function Component({
18 | cond,
19 | cond2,
20 | cond3,
21 | cond4,
22 | defaultClassName,
23 | id
24 | }: {
25 | cond: boolean,
26 | cond2: boolean,
27 | cond3: boolean,
28 | cond4: boolean,
29 | defaultClassName: string,
30 | id: string
31 | }) {
32 | let children = [
33 |
34 | {getLiBody("Default item")}
35 |
36 | ];
37 |
38 | if (cond) {
39 | children = [
40 |
48 | {getLiBody("Generic item")}
49 | {getLiBody2("Generic item", cond4)}
50 | {getLiBody2("Generic item", true)}
51 |
52 | ];
53 | }
54 |
55 | return (
56 |
59 | );
60 | }
61 |
62 | Component.compileRootComponent = true;
63 |
64 | module.exports = Component;
65 |
--------------------------------------------------------------------------------
/tests/jsx/context/context.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | const MyContext = React.createContext(10);
4 |
5 | function Component() {
6 | return (
7 |
8 | Hello world
9 |
10 | );
11 | }
12 |
13 | Component.compileRootComponent = true;
14 |
15 | module.exports = Component;
16 |
--------------------------------------------------------------------------------
/tests/jsx/context/context2.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | const MyContext = React.createContext(10);
4 |
5 | function Component() {
6 | return (
7 |
8 | Hello world
9 |
10 | );
11 | }
12 |
13 | Component.compileRootComponent = true;
14 |
15 | module.exports = Component;
16 |
--------------------------------------------------------------------------------
/tests/jsx/context/context3.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | const MyContext = React.createContext(10);
4 |
5 | function Component() {
6 | var Provider = MyContext.Provider;
7 | return (
8 |
9 | Hello world
10 | <>
11 | Static text
12 | >
13 |
14 | );
15 | }
16 |
17 | Component.compileRootComponent = true;
18 |
19 | module.exports = Component;
20 |
--------------------------------------------------------------------------------
/tests/jsx/context/context4.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | const MyContext = React.createContext(10);
4 |
5 | function Component() {
6 | const { Consumer, Provider } = MyContext;
7 | return (
8 |
9 | Hello world
10 | <>
11 | Static text
12 | >
13 | {(value: number) => {value}}
14 |
15 | );
16 | }
17 |
18 | Component.compileRootComponent = true;
19 |
20 | module.exports = Component;
21 |
--------------------------------------------------------------------------------
/tests/jsx/context/context5.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | const MyContext = React.createContext(10);
4 |
5 | function Component() {
6 | const { Consumer, Provider } = MyContext;
7 | return (
8 |
9 | Hello world
10 | <>
11 | Static text
12 | >
13 | {(value: number) => {value}}
14 |
15 | );
16 | }
17 |
18 | Component.compileRootComponent = true;
19 |
20 | module.exports = Component;
21 |
--------------------------------------------------------------------------------
/tests/jsx/context/context6.js:
--------------------------------------------------------------------------------
1 | // props:{number2: 101}
2 |
3 | var React = require("react");
4 |
5 | const MyContext = React.createContext(10);
6 |
7 | function Component({number2}: {number2: 10}) {
8 | const { Consumer, Provider } = MyContext;
9 | const number = 10;
10 | return (
11 |
12 | Hello world
13 | <>
14 | Static text
15 | >
16 | {(value: number) => {value}{number}{number2}}
17 |
18 | );
19 | }
20 |
21 | Component.compileRootComponent = true;
22 |
23 | module.exports = Component;
24 |
--------------------------------------------------------------------------------
/tests/jsx/context/context7.js:
--------------------------------------------------------------------------------
1 | // props:{number2: 101}
2 |
3 | var React = require("react");
4 |
5 | const MyContext = React.createContext(10);
6 |
7 | function formatValue(val): string {
8 | return `The value is ${val}!`;
9 | }
10 |
11 | function Component({ number2 }: { number2: 10 }) {
12 | const { Consumer, Provider } = MyContext;
13 | const number = 10;
14 | return (
15 | <>
16 |
17 | {(value: number) => {formatValue(value)}}
18 |
19 |
20 | Hello world
21 | <>Static text>
22 |
23 | {(value: number) => {formatValue(value)}}
24 |
25 |
26 | >
27 | );
28 | }
29 |
30 | Component.compileRootComponent = true;
31 |
32 | module.exports = Component;
--------------------------------------------------------------------------------
/tests/jsx/context/context8.js:
--------------------------------------------------------------------------------
1 | // props:{number2: 101}
2 |
3 | var React = require("react");
4 |
5 | const MyContext = React.createContext(10);
6 |
7 | function formatValue(val): string {
8 | return `The value is ${val}!`;
9 | }
10 |
11 | function Component({ number2 }: { number2: 10 }) {
12 | const { Consumer, Provider } = MyContext;
13 | const number = 10;
14 | return (
15 | <>
16 |
17 | {(value: number) => {
18 | return {formatValue(value)};
19 | }}
20 |
21 |
22 | Hello world
23 | <>Static text>
24 |
25 | {(value: number) => {
26 | return {formatValue(value)};
27 | }}
28 |
29 |
30 | >
31 | );
32 | }
33 |
34 | Component.compileRootComponent = true;
35 |
36 | module.exports = Component;
37 |
--------------------------------------------------------------------------------
/tests/jsx/context/context9.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | const MyContext = React.createContext(10);
4 |
5 | function Component() {
6 | const { Consumer, Provider } = MyContext;
7 | const func = (value: number) => {value};
8 | return (
9 |
10 | Hello world
11 | <>
12 | Static text
13 | >
14 | {func}
15 |
16 | );
17 | }
18 |
19 | Component.compileRootComponent = true;
20 |
21 | module.exports = Component;
22 |
--------------------------------------------------------------------------------
/tests/jsx/hooks/hooks.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component() {
4 | const [value, updateValue] = React.useState("Hello world");
5 | return {value}
;
6 | }
7 |
8 | Component.compileRootComponent = true;
9 |
10 | module.exports = Component;
11 |
--------------------------------------------------------------------------------
/tests/jsx/hooks/hooks2.js:
--------------------------------------------------------------------------------
1 | // Due to a Closure Compiler Bug, this code can't compile in ADVANCED
2 | // https://github.com/google/closure-compiler/issues/3145
3 | // gcc_SIMPLE
4 |
5 | var React = require("react");
6 | var { useState } = React;
7 |
8 | function Component() {
9 | const [value, updateValue] = useState("Hello world 2");
10 | return (
11 |
12 | {value}
13 | {value + " - again"}
14 |
15 | );
16 | }
17 |
18 | Component.compileRootComponent = true;
19 |
20 | module.exports = Component;
21 |
--------------------------------------------------------------------------------
/tests/jsx/hooks/hooks3.js:
--------------------------------------------------------------------------------
1 | // Due to a Closure Compiler Bug, this code can't compile in ADVANCED
2 | // https://github.com/google/closure-compiler/issues/3145
3 | // gcc_SIMPLE
4 | // props:{cond: true}
5 |
6 | var React = require("react");
7 | var { useState } = React;
8 |
9 | function Component({ cond }: { cond: boolean }) {
10 | const [value, updateValue] = useState("Hello world 2");
11 |
12 | if (cond) {
13 | return null;
14 | }
15 | return (
16 |
17 | {value}
18 | {value + " - again"}
19 |
20 | );
21 | }
22 |
23 | Component.compileRootComponent = true;
24 |
25 | module.exports = Component;
26 |
--------------------------------------------------------------------------------
/tests/jsx/hooks/hooks4.js:
--------------------------------------------------------------------------------
1 | // Due to a Closure Compiler Bug, this code can't compile in ADVANCED
2 | // https://github.com/google/closure-compiler/issues/3145
3 | // gcc_SIMPLE
4 | // props:{cond: false, cond2: false}
5 |
6 | var React = require("react");
7 | var { useState } = React;
8 |
9 | function Component({ cond, cond2 }: { cond: boolean, cond2: boolean }) {
10 | const [value, updateValue] = useState("Hello world 2");
11 |
12 | if (cond) {
13 | return {value};
14 | }
15 | if (cond2) {
16 | return {value}
;
17 | }
18 | return (
19 |
20 | {value}
21 | {value + " - again"}
22 |
23 | );
24 | }
25 |
26 | Component.compileRootComponent = true;
27 |
28 | module.exports = Component;
29 |
--------------------------------------------------------------------------------
/tests/jsx/hooks/hooks5.js:
--------------------------------------------------------------------------------
1 | // Due to a Closure Compiler Bug, this code can't compile in ADVANCED
2 | // https://github.com/google/closure-compiler/issues/3145
3 | // gcc_SIMPLE
4 | // props:{cond: false}
5 |
6 | var React = require("react");
7 | var { useState } = React;
8 |
9 | function Component({ cond }: { cond: boolean }) {
10 | const [value, updateValue] = useState("Hello world 2");
11 |
12 | if (cond) {
13 | return null;
14 | }
15 | return (
16 |
17 | {value}
18 | {value + " - again"}
19 |
20 | );
21 | }
22 |
23 | Component.compileRootComponent = true;
24 |
25 | module.exports = Component;
26 |
--------------------------------------------------------------------------------
/tests/jsx/large/large.js:
--------------------------------------------------------------------------------
1 | // props:{val1: "val1", val2: "val2", val3: "val3", val4: "val4", val5: "val5", val6: "val6", val7: "val7"}
2 | var React = require("react");
3 |
4 | function Component({
5 | val1,
6 | val2,
7 | val3,
8 | val4,
9 | val5,
10 | val6,
11 | val7
12 | }: {
13 | val1: string,
14 | val2: string,
15 | val3: string,
16 | val4: string,
17 | val5: string,
18 | val6: string,
19 | val7: string,
20 | }) {
21 | return (
22 |
23 |
26 |
27 |
28 |
Sub-section #1
29 |
30 |
{val2}
31 |
{val4}
32 |
35 |
38 |
39 |
40 |
41 |
Sub-section #2
42 |
43 |
{val5}
44 |
{val7}
45 |
48 |
51 |
52 |
53 |
54 |
55 | );
56 | }
57 |
58 | Component.compileRootComponent = true;
59 |
60 | module.exports = Component;
61 |
--------------------------------------------------------------------------------
/tests/jsx/style/style.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component() {
4 | return (
5 |
8 | );
9 | }
10 |
11 | Component.compileRootComponent = true;
12 |
13 | module.exports = Component;
14 |
--------------------------------------------------------------------------------
/tests/jsx/style/style2.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component() {
4 | return (
5 |
8 | );
9 | }
10 |
11 | Component.compileRootComponent = true;
12 |
13 | module.exports = Component;
14 |
--------------------------------------------------------------------------------
/tests/jsx/style/style3.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Component() {
4 | return (
5 |
6 |
14 | Hello world
15 |
16 |
17 | );
18 | }
19 |
20 | Component.compileRootComponent = true;
21 |
22 | module.exports = Component;
23 |
--------------------------------------------------------------------------------
/tests/jsx/style/style4.js:
--------------------------------------------------------------------------------
1 | // props:{styles: {color: "red", backgroundColor: "black", margin: 10}}
2 |
3 | var React = require("react");
4 |
5 | function Component({ styles }: { styles: { color: string, backgroundColor: string, margin: number } }) {
6 | return (
7 |
10 | );
11 | }
12 |
13 | Component.compileRootComponent = true;
14 |
15 | module.exports = Component;
16 |
--------------------------------------------------------------------------------
/tests/modules/simple-app/Header.compiled.js:
--------------------------------------------------------------------------------
1 | const Header = [98305, [102402, "div", "Header"]];
2 | module.exports = Header;
--------------------------------------------------------------------------------
/tests/modules/simple-app/Header.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | function Header() {
4 | return Header
;
5 | }
6 |
7 | module.exports = Header;
8 |
--------------------------------------------------------------------------------
/tests/modules/simple-app/index.compiled.js:
--------------------------------------------------------------------------------
1 | var Header = require("./Header.compiled.js");
2 |
3 | const App = [98304, [100354, "div", [[102402, "div", "Header"], [32771, "App"]]]];
4 | module["exports"] = App;
--------------------------------------------------------------------------------
/tests/modules/simple-app/index.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 | var Header = require("./Header");
3 |
4 | function App() {
5 | return (
6 |
7 |
8 | App
9 |
10 | );
11 | }
12 |
13 | App.compileRootComponent = true;
14 |
15 | module.exports = App;
16 |
--------------------------------------------------------------------------------
/tests/modules/simple-app2/Footer.compiled.js:
--------------------------------------------------------------------------------
1 | import type { FooterProps } from "./type";
2 |
3 | function Footer_ComputeFunction(x) {
4 | return [x];
5 | }
6 |
7 | const Footer = [1, Footer_ComputeFunction, [8194, "div", 0]];
8 | module.exports = Footer;
--------------------------------------------------------------------------------
/tests/modules/simple-app2/Footer.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 |
3 | import type { FooterProps } from "./type";
4 |
5 | function Footer({ x }: FooterProps) {
6 | return {x}
;
7 | }
8 |
9 | module.exports = Footer;
10 |
--------------------------------------------------------------------------------
/tests/modules/simple-app2/Header.compiled.js:
--------------------------------------------------------------------------------
1 | const {
2 | formatString
3 | } = require("./utils");
4 |
5 | import type { HeaderProps } from "./type";
6 |
7 | function Header_ComputeFunction(x) {
8 | return [x];
9 | }
10 |
11 | const Header = [1, Header_ComputeFunction, [8194, "div", 0]];
12 | module.exports = {
13 | formatString,
14 | Header
15 | };
--------------------------------------------------------------------------------
/tests/modules/simple-app2/Header.js:
--------------------------------------------------------------------------------
1 | const React = require("react");
2 | const { formatString } = require("./utils");
3 |
4 | import type { HeaderProps } from "./type";
5 |
6 | function Header({ x }: HeaderProps) {
7 | return {x}
;
8 | }
9 |
10 | module.exports = {
11 | formatString,
12 | Header,
13 | };
14 |
--------------------------------------------------------------------------------
/tests/modules/simple-app2/index.compiled.js:
--------------------------------------------------------------------------------
1 | // props:{x: "Hello world!"}
2 | var {
3 | ["Header"]: Header,
4 | ["formatString"]: formatString
5 | } = require("./Header.compiled.js");
6 |
7 | var Footer = require("./Footer.compiled.js");
8 |
9 | import type { IndexProps } from "./type";
10 |
11 | function App_ComputeFunction(x) {
12 | var __cached__0;
13 |
14 | __cached__0 = formatString(x);
15 | return [[x], __cached__0, [x]];
16 | }
17 |
18 | const App = [0, ["x"], App_ComputeFunction, [2050, "div", [[12, Header, 0], [3, 1], [12, Footer, 2]]]];
19 | module["exports"] = App;
--------------------------------------------------------------------------------
/tests/modules/simple-app2/index.js:
--------------------------------------------------------------------------------
1 | // props:{x: "Hello world!"}
2 | var React = require("react");
3 | var { Header, formatString } = require("./Header");
4 | var Footer = require("./Footer");
5 |
6 | import type { IndexProps } from "./type";
7 |
8 | function App({ x }: IndexProps) {
9 | return (
10 |
11 |
12 | {formatString(x)}
13 |
14 |
15 | );
16 | }
17 |
18 | App.compileRootComponent = true;
19 |
20 | module.exports = App;
21 |
--------------------------------------------------------------------------------
/tests/modules/simple-app2/type.js:
--------------------------------------------------------------------------------
1 | export type IndexProps = {
2 | x: string,
3 | };
4 |
5 | export type HeaderProps = {
6 | x: string,
7 | };
8 |
9 | export type FooterProps = {
10 | x: string,
11 | };
12 |
--------------------------------------------------------------------------------
/tests/modules/simple-app2/utils.js:
--------------------------------------------------------------------------------
1 | function formatString(x): string {
2 | return x.toString() + " 123";
3 | }
4 |
5 | module.exports = {
6 | formatString,
7 | };
8 |
--------------------------------------------------------------------------------
/tests/modules/simple-app3/Foo.compiled.js:
--------------------------------------------------------------------------------
1 | function Foo_ComputeFunction(text) {
2 | return [text];
3 | }
4 |
5 | export default [1, Foo_ComputeFunction, [2050, "span", [[32771, "Hello world"], [8194, "em", 0]]]];
--------------------------------------------------------------------------------
/tests/modules/simple-app3/Foo.js:
--------------------------------------------------------------------------------
1 | import React from "react";
2 |
3 | export default function Foo({ text }: { text: string }) {
4 | return (
5 |
6 | Hello world{text}
7 |
8 | );
9 | }
10 |
--------------------------------------------------------------------------------
/tests/modules/simple-app3/index.compiled.js:
--------------------------------------------------------------------------------
1 | import Foo from "./Foo.compiled.js";
2 | import { renderText, renderDiv, renderDivWithText, renderSpan } from "./utils.compiled.js";
3 |
4 | function App_ComputeFunction() {
5 | var __cached__1;
6 |
7 | var __cached__0;
8 |
9 | __cached__1 = renderDivWithText("foo bar!");
10 | __cached__0 = renderText();
11 | renderText();
12 | return [[renderText()], __cached__0, __cached__1];
13 | }
14 |
15 | const App = [0, 0, App_ComputeFunction, [2050, "div", [[12, Foo, 0], [3, 1], [102402, "div", "span!"], [8, [8194, "div", 0], 2], [102402, "span", "Span!"]]]];
16 | module["exports"] = App;
--------------------------------------------------------------------------------
/tests/modules/simple-app3/index.js:
--------------------------------------------------------------------------------
1 | import React from "react";
2 | import Foo from "./Foo";
3 | import {renderText, renderDiv, renderDivWithText, renderSpan} from "./utils";
4 |
5 | function App() {
6 | return (
7 |
8 |
9 | {renderText()}
10 | {renderDiv()}
11 | {renderDivWithText("foo bar!")}
12 | {renderSpan()}
13 |
14 | );
15 | }
16 |
17 | App.compileRootComponent = true;
18 |
19 | module.exports = App;
20 |
--------------------------------------------------------------------------------
/tests/modules/simple-app3/utils.compiled.js:
--------------------------------------------------------------------------------
1 | export function renderText(): string {
2 | return "Text!";
3 | }
4 | export function renderDiv(): React.Node {
5 | return [];
6 | }
7 | export function renderDivWithText(text: string): React.Node {
8 | return [text];
9 | }
10 | export const renderSpan = (): React.Node => [];
--------------------------------------------------------------------------------
/tests/modules/simple-app3/utils.js:
--------------------------------------------------------------------------------
1 | import React from "react";
2 |
3 | export function renderText(): string {
4 | return "Text!";
5 | }
6 |
7 | export function renderDiv(): React.Node {
8 | return span!
;
9 | }
10 |
11 | export function renderDivWithText(text: string): React.Node {
12 | return {text}
;
13 | }
14 |
15 | export const renderSpan = (): React.Node => Span!;
--------------------------------------------------------------------------------
/tests/modules/simple-app4/Foo.compiled.js:
--------------------------------------------------------------------------------
1 | function Foo_ComputeFunction(text) {
2 | return [text];
3 | }
4 |
5 | export default [1, Foo_ComputeFunction, [2050, "span", [[32771, "Hello world"], [8194, "em", 0]]]];
--------------------------------------------------------------------------------
/tests/modules/simple-app4/Foo.js:
--------------------------------------------------------------------------------
1 | import React from "react";
2 |
3 | export default function Foo({ text }: { text: string }) {
4 | return (
5 |
6 | Hello world{text}
7 |
8 | );
9 | }
10 |
--------------------------------------------------------------------------------
/tests/modules/simple-app4/index.compiled.js:
--------------------------------------------------------------------------------
1 | import Foo from "./Foo.compiled.js";
2 | import { renderText, renderDivOrNull, renderDivWithText, renderSpan } from "./utils.compiled.js";
3 |
4 | function App_ComputeFunction() {
5 | var __cached__2;
6 |
7 | var __cached__1;
8 |
9 | var __cached__0;
10 |
11 | __cached__2 = renderDivWithText("foo bar!");
12 | __cached__1 = renderDivOrNull(true);
13 | __cached__0 = renderText();
14 | renderText();
15 | return [[renderText()], __cached__0, __cached__1, __cached__2];
16 | }
17 |
18 | const App = [0, 0, App_ComputeFunction, [2050, "div", [[12, Foo, 0], [3, 1], [8, [4098, "div", "another span!"], 2], [8, [8194, "div", 0], 3], [102402, "span", "Span!"]]]];
19 | module["exports"] = App;
--------------------------------------------------------------------------------
/tests/modules/simple-app4/index.js:
--------------------------------------------------------------------------------
1 | import React from "react";
2 | import Foo from "./Foo";
3 | import {renderText, renderDivOrNull, renderDivWithText, renderSpan} from "./utils";
4 |
5 | function App() {
6 | return (
7 |
8 |
9 | {renderText()}
10 | {renderDivOrNull(true)}
11 | {renderDivWithText("foo bar!")}
12 | {renderSpan()}
13 |
14 | );
15 | }
16 |
17 | App.compileRootComponent = true;
18 |
19 | module.exports = App;
20 |
--------------------------------------------------------------------------------
/tests/modules/simple-app4/utils.compiled.js:
--------------------------------------------------------------------------------
1 | export function renderText(): string {
2 | return "Text!";
3 | }
4 | export function renderDivOrNull(cond: boolean): React.Node {
5 | if (cond) {
6 | return null;
7 | }
8 |
9 | return [];
10 | }
11 | export function renderDivWithText(text: string): React.Node {
12 | return [text];
13 | }
14 | export const renderSpan = (): React.Node => [];
--------------------------------------------------------------------------------
/tests/modules/simple-app4/utils.js:
--------------------------------------------------------------------------------
1 | import React from "react";
2 |
3 | export function renderText(): string {
4 | return "Text!";
5 | }
6 |
7 | export function renderDivOrNull(cond: boolean): React.Node {
8 | if (cond) {
9 | return null;
10 | }
11 | return another span!
;
12 | }
13 |
14 | export function renderDivWithText(text: string): React.Node {
15 | return {text}
;
16 | }
17 |
18 | export const renderSpan = (): React.Node => Span!;
--------------------------------------------------------------------------------