@samitouri / QOS-React-1 / commits / 4bc676d3db

Repro for undefined "hoisted" variable from type alias

We inadvertently think the type annotation on the function expression param is an identifier and create a LoadLocal for it, which fails. This happens to trip up on the InferReferenceEffects initialization check, which we had assumed would only fire for invalid hoisting cases (hence the specific error message).

Joe Savona committed Mar 21, 2024 at 14:11 UTC 4bc676d3dbd78f5c4c3f9be7f86c3b084a268ac9
8 files changed +199
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.type-alias-used-as-annotation_.flow.expect.md new
+30
@@ -0,0 +1,30 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +// @flow @enableAssumeHooksFollowRulesOfReact @enableTransitivelyFreezeFunctionExpressions
6 +type Bar = string;
7 +function TypeAliasUsedAsAnnotation() {
8 + type Foo = Bar;
9 + const fun = (f: Foo) => {
10 + console.log(f);
11 + };
12 + fun("hello, world");
13 +}
14 +
15 +```
16 +
17 +
18 +## Error
19 +
20 +```
21 + 3 | function TypeAliasUsedAsAnnotation() {
22 + 4 | type Foo = Bar;
23 +> 5 | const fun = (f: Foo) => {
24 + | ^^^ [ReactForget] Invariant: [hoisting] Expected value for identifier to be initialized. Foo$0 (5:5)
25 + 6 | console.log(f);
26 + 7 | };
27 + 8 | fun("hello, world");
28 +```
29 +
30 +
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.type-alias-used-as-annotation_.flow.js new
+9
@@ -0,0 +1,9 @@
1 +// @flow @enableAssumeHooksFollowRulesOfReact @enableTransitivelyFreezeFunctionExpressions
2 +type Bar = string;
3 +function TypeAliasUsedAsAnnotation() {
4 + type Foo = Bar;
5 + const fun = (f: Foo) => {
6 + console.log(f);
7 + };
8 + fun("hello, world");
9 +}
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.type-alias-used-as-variable-annotation_.flow.expect.md new
+31
@@ -0,0 +1,31 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +// @flow @enableAssumeHooksFollowRulesOfReact @enableTransitivelyFreezeFunctionExpressions
6 +type Bar = string;
7 +function TypeAliasUsedAsAnnotation() {
8 + type Foo = Bar;
9 + const fun = (f) => {
10 + let g: Foo = f;
11 + console.log(g);
12 + };
13 + fun("hello, world");
14 +}
15 +
16 +```
17 +
18 +
19 +## Error
20 +
21 +```
22 + 4 | type Foo = Bar;
23 + 5 | const fun = (f) => {
24 +> 6 | let g: Foo = f;
25 + | ^^^ [ReactForget] Invariant: [hoisting] Expected value for identifier to be initialized. Foo$0 (6:6)
26 + 7 | console.log(g);
27 + 8 | };
28 + 9 | fun("hello, world");
29 +```
30 +
31 +
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.type-alias-used-as-variable-annotation_.flow.js new
+10
@@ -0,0 +1,10 @@
1 +// @flow @enableAssumeHooksFollowRulesOfReact @enableTransitivelyFreezeFunctionExpressions
2 +type Bar = string;
3 +function TypeAliasUsedAsAnnotation() {
4 + type Foo = Bar;
5 + const fun = (f) => {
6 + let g: Foo = f;
7 + console.log(g);
8 + };
9 + fun("hello, world");
10 +}
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias-used-as-annotation.expect.md new
+44
@@ -0,0 +1,44 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +// @enableAssumeHooksFollowRulesOfReact @enableTransitivelyFreezeFunctionExpressions
6 +type Bar = string;
7 +function TypeAliasUsedAsParamAnnotation() {
8 + type Foo = Bar;
9 + const fun = (f: Foo) => {
10 + console.log(f);
11 + };
12 + fun("hello, world");
13 +}
14 +
15 +export const FIXTURE_ENTRYPOINT = {
16 + fn: TypeAliasUsedAsParamAnnotation,
17 + params: [],
18 +};
19 +
20 +```
21 +
22 +## Code
23 +
24 +```javascript
25 +// @enableAssumeHooksFollowRulesOfReact @enableTransitivelyFreezeFunctionExpressions
26 +type Bar = string;
27 +function TypeAliasUsedAsParamAnnotation() {
28 + const fun = (f) => {
29 + console.log(f);
30 + };
31 +
32 + fun("hello, world");
33 +}
34 +
35 +export const FIXTURE_ENTRYPOINT = {
36 + fn: TypeAliasUsedAsParamAnnotation,
37 + params: [],
38 +};
39 +
40 +```
41 +
42 +### Eval output
43 +(kind: ok)
44 +logs: ['hello, world']
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias-used-as-annotation.ts new
+14
@@ -0,0 +1,14 @@
1 +// @enableAssumeHooksFollowRulesOfReact @enableTransitivelyFreezeFunctionExpressions
2 +type Bar = string;
3 +function TypeAliasUsedAsParamAnnotation() {
4 + type Foo = Bar;
5 + const fun = (f: Foo) => {
6 + console.log(f);
7 + };
8 + fun("hello, world");
9 +}
10 +
11 +export const FIXTURE_ENTRYPOINT = {
12 + fn: TypeAliasUsedAsParamAnnotation,
13 + params: [],
14 +};
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias-used-as-variable-annotation.expect.md new
+46
@@ -0,0 +1,46 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +// @enableAssumeHooksFollowRulesOfReact @enableTransitivelyFreezeFunctionExpressions
6 +type Bar = string;
7 +function TypeAliasUsedAsVariableAnnotation() {
8 + type Foo = Bar;
9 + const fun = (f) => {
10 + let g: Foo = f;
11 + console.log(g);
12 + };
13 + fun("hello, world");
14 +}
15 +
16 +export const FIXTURE_ENTRYPOINT = {
17 + fn: TypeAliasUsedAsVariableAnnotation,
18 + params: [],
19 +};
20 +
21 +```
22 +
23 +## Code
24 +
25 +```javascript
26 +// @enableAssumeHooksFollowRulesOfReact @enableTransitivelyFreezeFunctionExpressions
27 +type Bar = string;
28 +function TypeAliasUsedAsVariableAnnotation() {
29 + const fun = (f) => {
30 + const g = f;
31 + console.log(g);
32 + };
33 +
34 + fun("hello, world");
35 +}
36 +
37 +export const FIXTURE_ENTRYPOINT = {
38 + fn: TypeAliasUsedAsVariableAnnotation,
39 + params: [],
40 +};
41 +
42 +```
43 +
44 +### Eval output
45 +(kind: ok)
46 +logs: ['hello, world']
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias-used-as-variable-annotation.ts new
+15
@@ -0,0 +1,15 @@
1 +// @enableAssumeHooksFollowRulesOfReact @enableTransitivelyFreezeFunctionExpressions
2 +type Bar = string;
3 +function TypeAliasUsedAsVariableAnnotation() {
4 + type Foo = Bar;
5 + const fun = (f) => {
6 + let g: Foo = f;
7 + console.log(g);
8 + };
9 + fun("hello, world");
10 +}
11 +
12 +export const FIXTURE_ENTRYPOINT = {
13 + fn: TypeAliasUsedAsVariableAnnotation,
14 + params: [],
15 +};