@samitouri / QOS-React / commits / fb0f7e5c39

Repro for closures reassigning hoisted let variables

Adds examples for closures that reassign hoisted let bindings. We currently compile these as context variables without hoisting, so we hit an invariant in InferReferenceEffects when the variable is referenced before being declared.

Joe Savona committed Mar 22, 2024 at 11:53 UTC fb0f7e5c39c01ee726205746439ecd7a1ceedb75
4 files changed +70
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-function-expression-references-later-variable-declaration.expect.md new
+29
@@ -0,0 +1,29 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +function Component() {
6 + let callback = () => {
7 + onClick = () => {};
8 + };
9 + let onClick;
10 +
11 + return <div onClick={callback} />;
12 +}
13 +
14 +```
15 +
16 +
17 +## Error
18 +
19 +```
20 + 1 | function Component() {
21 + 2 | let callback = () => {
22 +> 3 | onClick = () => {};
23 + | ^^^^^^^ [ReactForget] Invariant: [hoisting] Expected value kind to be initialized. read onClick$0_@1 (3:3)
24 + 4 | };
25 + 5 | let onClick;
26 + 6 |
27 +```
28 +
29 +
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-function-expression-references-later-variable-declaration.js new
+8
@@ -0,0 +1,8 @@
1 +function Component() {
2 + let callback = () => {
3 + onClick = () => {};
4 + };
5 + let onClick;
6 +
7 + return <div onClick={callback} />;
8 +}
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-function-expression-references-variable-its-assigned-to.expect.md new
+27
@@ -0,0 +1,27 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +function Component() {
6 + let callback = () => {
7 + callback = null;
8 + };
9 + return <div onClick={callback} />;
10 +}
11 +
12 +```
13 +
14 +
15 +## Error
16 +
17 +```
18 + 1 | function Component() {
19 + 2 | let callback = () => {
20 +> 3 | callback = null;
21 + | ^^^^^^^^ [ReactForget] Invariant: [hoisting] Expected value kind to be initialized. read callback$0_@0 (3:3)
22 + 4 | };
23 + 5 | return <div onClick={callback} />;
24 + 6 | }
25 +```
26 +
27 +
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-function-expression-references-variable-its-assigned-to.js new
+6
@@ -0,0 +1,6 @@
1 +function Component() {
2 + let callback = () => {
3 + callback = null;
4 + };
5 + return <div onClick={callback} />;
6 +}