diff --git a/compiler/forget/packages/playground/components/Editor/Output.tsx b/compiler/forget/packages/playground/components/Editor/Output.tsx index 2d691b9b8f..9ba6de1b82 100644 --- a/compiler/forget/packages/playground/components/Editor/Output.tsx +++ b/compiler/forget/packages/playground/components/Editor/Output.tsx @@ -37,6 +37,7 @@ function parseFunctions( try { const ast = parse(source, { plugins: ["typescript", "jsx"], + sourceType: "module", }); const items: Array> = []; traverse(ast, { diff --git a/compiler/forget/src/Babel/RunReactForgetBabelPlugin.ts b/compiler/forget/src/Babel/RunReactForgetBabelPlugin.ts index 4971a9ced9..365f94a78d 100644 --- a/compiler/forget/src/Babel/RunReactForgetBabelPlugin.ts +++ b/compiler/forget/src/Babel/RunReactForgetBabelPlugin.ts @@ -18,12 +18,14 @@ export default function runReactForgetBabelPlugin( const ast = parser.parse(text, { sourceFilename: file, plugins: ["typescript", "jsx"], + sourceType: "module", }); const result = transformFromAstSync(ast, text, { filename: file, highlightCode: false, retainLines: true, plugins: [ReactForgetBabelPlugin], + sourceType: "module", }); invariant( result?.code != null, diff --git a/compiler/forget/src/__tests__/fixtures/hir/conditional-break.expect.md b/compiler/forget/src/__tests__/fixtures/hir/conditional-break.expect.md index 3492311ea1..5207a7054f 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/conditional-break.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/conditional-break.expect.md @@ -5,7 +5,7 @@ /** * props.b does *not* influence `a` */ -function Component(props) { +function ComponentA(props) { const a_DEBUG = []; a_DEBUG.push(props.a); if (props.b) { @@ -18,7 +18,7 @@ function Component(props) { /** * props.b *does* influence `a` */ -function Component(props) { +function ComponentB(props) { const a = []; a.push(props.a); if (props.b) { @@ -31,7 +31,7 @@ function Component(props) { /** * props.b *does* influence `a`, but only in a way that is never observable */ -function Component(props) { +function ComponentC(props) { const a = []; a.push(props.a); if (props.b) { @@ -45,7 +45,7 @@ function Component(props) { /** * props.b *does* influence `a` */ -function Component(props) { +function ComponentD(props) { const a = []; a.push(props.a); if (props.b) { @@ -64,7 +64,7 @@ function Component(props) { /** * props.b does *not* influence `a` */ -function Component(props) { +function ComponentA(props) { const $ = React.useMemoCache(); const c_0 = $[0] !== props.a; const c_1 = $[1] !== props.b; @@ -91,7 +91,7 @@ function Component(props) { /** * props.b *does* influence `a` */ -function Component(props) { +function ComponentB(props) { const $ = React.useMemoCache(); const c_0 = $[0] !== props.a; const c_1 = $[1] !== props.b; @@ -120,7 +120,7 @@ function Component(props) { /** * props.b *does* influence `a`, but only in a way that is never observable */ -function Component(props) { +function ComponentC(props) { const $ = React.useMemoCache(); const c_0 = $[0] !== props.a; const c_1 = $[1] !== props.b; @@ -150,7 +150,7 @@ function Component(props) { /** * props.b *does* influence `a` */ -function Component(props) { +function ComponentD(props) { const $ = React.useMemoCache(); const c_0 = $[0] !== props.a; const c_1 = $[1] !== props.b; diff --git a/compiler/forget/src/__tests__/fixtures/hir/conditional-break.js b/compiler/forget/src/__tests__/fixtures/hir/conditional-break.js index 297a2db070..7b5baa9ea5 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/conditional-break.js +++ b/compiler/forget/src/__tests__/fixtures/hir/conditional-break.js @@ -1,7 +1,7 @@ /** * props.b does *not* influence `a` */ -function Component(props) { +function ComponentA(props) { const a_DEBUG = []; a_DEBUG.push(props.a); if (props.b) { @@ -14,7 +14,7 @@ function Component(props) { /** * props.b *does* influence `a` */ -function Component(props) { +function ComponentB(props) { const a = []; a.push(props.a); if (props.b) { @@ -27,7 +27,7 @@ function Component(props) { /** * props.b *does* influence `a`, but only in a way that is never observable */ -function Component(props) { +function ComponentC(props) { const a = []; a.push(props.a); if (props.b) { @@ -41,7 +41,7 @@ function Component(props) { /** * props.b *does* influence `a` */ -function Component(props) { +function ComponentD(props) { const a = []; a.push(props.a); if (props.b) { diff --git a/compiler/forget/src/__tests__/fixtures/hir/conditional-on-mutable.expect.md b/compiler/forget/src/__tests__/fixtures/hir/conditional-on-mutable.expect.md index 48ca4ec80c..7ced099303 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/conditional-on-mutable.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/conditional-on-mutable.expect.md @@ -2,7 +2,7 @@ ## Input ```javascript -function Component(props) { +function ComponentA(props) { const a = []; const b = []; if (b) { @@ -14,7 +14,7 @@ function Component(props) { return ; } -function Component(props) { +function ComponentB(props) { const a = []; const b = []; if (mayMutate(b)) { @@ -34,7 +34,7 @@ function mayMutate() {} ## Code ```javascript -function Component(props) { +function ComponentA(props) { const $ = React.useMemoCache(); const c_0 = $[0] !== props.p0; const c_1 = $[1] !== props.p1; @@ -73,7 +73,7 @@ function Component(props) { return t7; } -function Component(props) { +function ComponentB(props) { const $ = React.useMemoCache(); const c_0 = $[0] !== props.p0; const c_1 = $[1] !== props.p1; diff --git a/compiler/forget/src/__tests__/fixtures/hir/conditional-on-mutable.js b/compiler/forget/src/__tests__/fixtures/hir/conditional-on-mutable.js index 7dc4fdce49..0378ab655c 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/conditional-on-mutable.js +++ b/compiler/forget/src/__tests__/fixtures/hir/conditional-on-mutable.js @@ -1,4 +1,4 @@ -function Component(props) { +function ComponentA(props) { const a = []; const b = []; if (b) { @@ -10,7 +10,7 @@ function Component(props) { return ; } -function Component(props) { +function ComponentB(props) { const a = []; const b = []; if (mayMutate(b)) { diff --git a/compiler/forget/src/__tests__/fixtures/hir/error.todo-kitchensink.expect.md b/compiler/forget/src/__tests__/fixtures/hir/error.todo-kitchensink.expect.md index bd2443718e..6038a61ba7 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/error.todo-kitchensink.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/error.todo-kitchensink.expect.md @@ -13,9 +13,6 @@ function foo([a, b], { c, d, e = "e" }, f = "f", ...args) { } } - with (true) { - } - const g = { ...a }; const h = [...b]; new c(...args); @@ -91,191 +88,182 @@ function foo([a, b], { c, d, e = "e" }, f = "f", ...args) { 7 | constructor() { 8 | console.log(this.#secretSauce); -[ReactForget] TodoError: Unhandled statement type: WithStatement +[ReactForget] TodoError: Handle object property spread 10 | } 11 | -> 12 | with (true) { - | ^ - 13 | } - 14 | - 15 | const g = { ...a }; - -[ReactForget] TodoError: Handle object property spread - 13 | } - 14 | -> 15 | const g = { ...a }; +> 12 | const g = { ...a }; | ^^^^ - 16 | const h = [...b]; - 17 | new c(...args); - 18 | c(...args); + 13 | const h = [...b]; + 14 | new c(...args); + 15 | c(...args); [ReactForget] TodoError: Handle non-expression array elements - 14 | - 15 | const g = { ...a }; -> 16 | const h = [...b]; + 11 | + 12 | const g = { ...a }; +> 13 | const h = [...b]; | ^^^^ - 17 | new c(...args); - 18 | c(...args); - 19 | g["e"] += 1; + 14 | new c(...args); + 15 | c(...args); + 16 | g["e"] += 1; [ReactForget] TodoError: Support non-expression arguments to NewExpression - 15 | const g = { ...a }; - 16 | const h = [...b]; -> 17 | new c(...args); + 12 | const g = { ...a }; + 13 | const h = [...b]; +> 14 | new c(...args); | ^^^^^^^ - 18 | c(...args); - 19 | g["e"] += 1; - 20 | const [y, ...yy] = useState(0); + 15 | c(...args); + 16 | g["e"] += 1; + 17 | const [y, ...yy] = useState(0); [ReactForget] TodoError: Support non-expression arguments to CallExpression - 16 | const h = [...b]; - 17 | new c(...args); -> 18 | c(...args); + 13 | const h = [...b]; + 14 | new c(...args); +> 15 | c(...args); | ^^^^^^^ - 19 | g["e"] += 1; - 20 | const [y, ...yy] = useState(0); - 21 | const { z, aa = "aa", ...zz } = useCustom(); + 16 | g["e"] += 1; + 17 | const [y, ...yy] = useState(0); + 18 | const { z, aa = "aa", ...zz } = useCustom(); [ReactForget] TodoError: Assignment expression to dynamic properties is not yet supported - 17 | new c(...args); - 18 | c(...args); -> 19 | g["e"] += 1; + 14 | new c(...args); + 15 | c(...args); +> 16 | g["e"] += 1; | ^^^ - 20 | const [y, ...yy] = useState(0); - 21 | const { z, aa = "aa", ...zz } = useCustom(); - 22 | + 17 | const [y, ...yy] = useState(0); + 18 | const { z, aa = "aa", ...zz } = useCustom(); + 19 | [ReactForget] TodoError: Rest elements are not supported yet - 18 | c(...args); - 19 | g["e"] += 1; -> 20 | const [y, ...yy] = useState(0); + 15 | c(...args); + 16 | g["e"] += 1; +> 17 | const [y, ...yy] = useState(0); | ^^^^^ - 21 | const { z, aa = "aa", ...zz } = useCustom(); - 22 | - 23 | ; + 18 | const { z, aa = "aa", ...zz } = useCustom(); + 19 | + 20 | ; [ReactForget] TodoError: Support other lvalue types beyond identifier - 19 | g["e"] += 1; - 20 | const [y, ...yy] = useState(0); -> 21 | const { z, aa = "aa", ...zz } = useCustom(); + 16 | g["e"] += 1; + 17 | const [y, ...yy] = useState(0); +> 18 | const { z, aa = "aa", ...zz } = useCustom(); | ^^^^^^^^^ - 22 | - 23 | ; - 24 | ; + 19 | + 20 | ; + 21 | ; [ReactForget] TodoError: Rest elements are not supported yet - 19 | g["e"] += 1; - 20 | const [y, ...yy] = useState(0); -> 21 | const { z, aa = "aa", ...zz } = useCustom(); + 16 | g["e"] += 1; + 17 | const [y, ...yy] = useState(0); +> 18 | const { z, aa = "aa", ...zz } = useCustom(); | ^^^^^ - 22 | - 23 | ; - 24 | ; + 19 | + 20 | ; + 21 | ; [ReactForget] TodoError: Handle spread attributes - 21 | const { z, aa = "aa", ...zz } = useCustom(); - 22 | -> 23 | ; + 18 | const { z, aa = "aa", ...zz } = useCustom(); + 19 | +> 20 | ; | ^^^^^^^^^ - 24 | ; - 25 | ; - 26 | ; + 21 | ; + 22 | ; + 23 | ; [ReactForget] TodoError: Handle non-identifier jsx attribute names - 22 | - 23 | ; -> 24 | ; + 19 | + 20 | ; +> 21 | ; | ^^^^^^^^^^ - 25 | ; - 26 | ; - 27 | ; + 22 | ; + 23 | ; + 24 | ; [ReactForget] TodoError: Handle empty expressions - 24 | ; - 25 | ; -> 26 | ; + 21 | ; + 22 | ; +> 23 | ; | ^^^^^^^^^^^^ - 27 | ; - 28 | - 29 | const j = function bar([quz, qux], ...args) {}; + 24 | ; + 25 | + 26 | const j = function bar([quz, qux], ...args) {}; [ReactForget] TodoError: Handle non-identifier tags - 25 | ; - 26 | ; -> 27 | ; + 22 | ; + 23 | ; +> 24 | ; | ^^^^^^^^^^^^^^^^^^^ - 28 | - 29 | const j = function bar([quz, qux], ...args) {}; - 30 | + 25 | + 26 | const j = function bar([quz, qux], ...args) {}; + 27 | [ReactForget] TodoError: Support non-identifier params: ArrayPattern - 27 | ; - 28 | -> 29 | const j = function bar([quz, qux], ...args) {}; + 24 | ; + 25 | +> 26 | const j = function bar([quz, qux], ...args) {}; | ^^^^^^^^^^ - 30 | - 31 | for (; i < 3; i += 1) { - 32 | x.push(i); + 27 | + 28 | for (; i < 3; i += 1) { + 29 | x.push(i); [ReactForget] TodoError: Support non-identifier params: RestElement - 27 | ; - 28 | -> 29 | const j = function bar([quz, qux], ...args) {}; + 24 | ; + 25 | +> 26 | const j = function bar([quz, qux], ...args) {}; | ^^^^^^^ - 30 | - 31 | for (; i < 3; i += 1) { - 32 | x.push(i); + 27 | + 28 | for (; i < 3; i += 1) { + 29 | x.push(i); [ReactForget] TodoError: Support non-variable initialization in for - 29 | const j = function bar([quz, qux], ...args) {}; - 30 | -> 31 | for (; i < 3; i += 1) { + 26 | const j = function bar([quz, qux], ...args) {}; + 27 | +> 28 | for (; i < 3; i += 1) { | ^ - 32 | x.push(i); - 33 | } - 34 | for (; i < 3; ) {} + 29 | x.push(i); + 30 | } + 31 | for (; i < 3; ) {} [ReactForget] TodoError: Support non-variable initialization in for - 32 | x.push(i); - 33 | } -> 34 | for (; i < 3; ) {} + 29 | x.push(i); + 30 | } +> 31 | for (; i < 3; ) {} | ^^^^^^^^^^^^^^^^^^ - 35 | for (;;) {} - 36 | } - 37 | + 32 | for (;;) {} + 33 | } + 34 | [ReactForget] TodoError: Handle empty for updater - 32 | x.push(i); - 33 | } -> 34 | for (; i < 3; ) {} + 29 | x.push(i); + 30 | } +> 31 | for (; i < 3; ) {} | ^^^^^^^^^^^^^^^^^^ - 35 | for (;;) {} - 36 | } - 37 | + 32 | for (;;) {} + 33 | } + 34 | [ReactForget] TodoError: Support non-variable initialization in for - 33 | } - 34 | for (; i < 3; ) {} -> 35 | for (;;) {} + 30 | } + 31 | for (; i < 3; ) {} +> 32 | for (;;) {} | ^^^^^^^^^^^ - 36 | } - 37 | + 33 | } + 34 | [ReactForget] TodoError: Handle empty for updater - 33 | } - 34 | for (; i < 3; ) {} -> 35 | for (;;) {} + 30 | } + 31 | for (; i < 3; ) {} +> 32 | for (;;) {} | ^^^^^^^^^^^ - 36 | } - 37 | + 33 | } + 34 | [ReactForget] TodoError: ForStatement without test - 33 | } - 34 | for (; i < 3; ) {} -> 35 | for (;;) {} + 30 | } + 31 | for (; i < 3; ) {} +> 32 | for (;;) {} | ^^^^^^^^^^^ - 36 | } - 37 | + 33 | } + 34 | ``` \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/hir/error.todo-kitchensink.js b/compiler/forget/src/__tests__/fixtures/hir/error.todo-kitchensink.js index 95826adcc1..db07790e98 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/error.todo-kitchensink.js +++ b/compiler/forget/src/__tests__/fixtures/hir/error.todo-kitchensink.js @@ -9,9 +9,6 @@ function foo([a, b], { c, d, e = "e" }, f = "f", ...args) { } } - with (true) { - } - const g = { ...a }; const h = [...b]; new c(...args); diff --git a/compiler/forget/src/__tests__/fixtures/hir/simple.expect.md b/compiler/forget/src/__tests__/fixtures/hir/simple.expect.md index 9f2778594f..585974ae54 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/simple.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/simple.expect.md @@ -2,7 +2,7 @@ ## Input ```javascript -function foo(x, y) { +export default function foo(x, y) { if (x) { return foo(false, y); } @@ -14,7 +14,7 @@ function foo(x, y) { ## Code ```javascript -function foo(x, y) { +export default function foo(x, y) { const $ = React.useMemoCache(); if (x) { const c_0 = $[0] !== y; diff --git a/compiler/forget/src/__tests__/fixtures/hir/simple.js b/compiler/forget/src/__tests__/fixtures/hir/simple.js index 62369ad258..e804953134 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/simple.js +++ b/compiler/forget/src/__tests__/fixtures/hir/simple.js @@ -1,4 +1,4 @@ -function foo(x, y) { +export default function foo(x, y) { if (x) { return foo(false, y); }