From 4bc676d3dbd78f5c4c3f9be7f86c3b084a268ac9 Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Thu, 21 Mar 2024 14:11:04 -0700 Subject: [PATCH] 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). --- ...e-alias-used-as-annotation_.flow.expect.md | 30 ++++++++++++ ...ror.type-alias-used-as-annotation_.flow.js | 9 ++++ ...sed-as-variable-annotation_.flow.expect.md | 31 +++++++++++++ ...alias-used-as-variable-annotation_.flow.js | 10 ++++ .../type-alias-used-as-annotation.expect.md | 44 ++++++++++++++++++ .../compiler/type-alias-used-as-annotation.ts | 14 ++++++ ...lias-used-as-variable-annotation.expect.md | 46 +++++++++++++++++++ .../type-alias-used-as-variable-annotation.ts | 15 ++++++ 8 files changed, 199 insertions(+) create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.type-alias-used-as-annotation_.flow.expect.md create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.type-alias-used-as-annotation_.flow.js create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.type-alias-used-as-variable-annotation_.flow.expect.md create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.type-alias-used-as-variable-annotation_.flow.js create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias-used-as-annotation.expect.md create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias-used-as-annotation.ts create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias-used-as-variable-annotation.expect.md create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias-used-as-variable-annotation.ts diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.type-alias-used-as-annotation_.flow.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.type-alias-used-as-annotation_.flow.expect.md new file mode 100644 index 0000000000..0f9f6287d2 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.type-alias-used-as-annotation_.flow.expect.md @@ -0,0 +1,30 @@ + +## Input + +```javascript +// @flow @enableAssumeHooksFollowRulesOfReact @enableTransitivelyFreezeFunctionExpressions +type Bar = string; +function TypeAliasUsedAsAnnotation() { + type Foo = Bar; + const fun = (f: Foo) => { + console.log(f); + }; + fun("hello, world"); +} + +``` + + +## Error + +``` + 3 | function TypeAliasUsedAsAnnotation() { + 4 | type Foo = Bar; +> 5 | const fun = (f: Foo) => { + | ^^^ [ReactForget] Invariant: [hoisting] Expected value for identifier to be initialized. Foo$0 (5:5) + 6 | console.log(f); + 7 | }; + 8 | fun("hello, world"); +``` + + \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.type-alias-used-as-annotation_.flow.js b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.type-alias-used-as-annotation_.flow.js new file mode 100644 index 0000000000..748635b321 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.type-alias-used-as-annotation_.flow.js @@ -0,0 +1,9 @@ +// @flow @enableAssumeHooksFollowRulesOfReact @enableTransitivelyFreezeFunctionExpressions +type Bar = string; +function TypeAliasUsedAsAnnotation() { + type Foo = Bar; + const fun = (f: Foo) => { + console.log(f); + }; + fun("hello, world"); +} diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.type-alias-used-as-variable-annotation_.flow.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.type-alias-used-as-variable-annotation_.flow.expect.md new file mode 100644 index 0000000000..ab781e8db9 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.type-alias-used-as-variable-annotation_.flow.expect.md @@ -0,0 +1,31 @@ + +## Input + +```javascript +// @flow @enableAssumeHooksFollowRulesOfReact @enableTransitivelyFreezeFunctionExpressions +type Bar = string; +function TypeAliasUsedAsAnnotation() { + type Foo = Bar; + const fun = (f) => { + let g: Foo = f; + console.log(g); + }; + fun("hello, world"); +} + +``` + + +## Error + +``` + 4 | type Foo = Bar; + 5 | const fun = (f) => { +> 6 | let g: Foo = f; + | ^^^ [ReactForget] Invariant: [hoisting] Expected value for identifier to be initialized. Foo$0 (6:6) + 7 | console.log(g); + 8 | }; + 9 | fun("hello, world"); +``` + + \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.type-alias-used-as-variable-annotation_.flow.js b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.type-alias-used-as-variable-annotation_.flow.js new file mode 100644 index 0000000000..64966640a9 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.type-alias-used-as-variable-annotation_.flow.js @@ -0,0 +1,10 @@ +// @flow @enableAssumeHooksFollowRulesOfReact @enableTransitivelyFreezeFunctionExpressions +type Bar = string; +function TypeAliasUsedAsAnnotation() { + type Foo = Bar; + const fun = (f) => { + let g: Foo = f; + console.log(g); + }; + fun("hello, world"); +} diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias-used-as-annotation.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias-used-as-annotation.expect.md new file mode 100644 index 0000000000..02a31ec938 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias-used-as-annotation.expect.md @@ -0,0 +1,44 @@ + +## Input + +```javascript +// @enableAssumeHooksFollowRulesOfReact @enableTransitivelyFreezeFunctionExpressions +type Bar = string; +function TypeAliasUsedAsParamAnnotation() { + type Foo = Bar; + const fun = (f: Foo) => { + console.log(f); + }; + fun("hello, world"); +} + +export const FIXTURE_ENTRYPOINT = { + fn: TypeAliasUsedAsParamAnnotation, + params: [], +}; + +``` + +## Code + +```javascript +// @enableAssumeHooksFollowRulesOfReact @enableTransitivelyFreezeFunctionExpressions +type Bar = string; +function TypeAliasUsedAsParamAnnotation() { + const fun = (f) => { + console.log(f); + }; + + fun("hello, world"); +} + +export const FIXTURE_ENTRYPOINT = { + fn: TypeAliasUsedAsParamAnnotation, + params: [], +}; + +``` + +### Eval output +(kind: ok) +logs: ['hello, world'] \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias-used-as-annotation.ts b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias-used-as-annotation.ts new file mode 100644 index 0000000000..d8eb6093d5 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias-used-as-annotation.ts @@ -0,0 +1,14 @@ +// @enableAssumeHooksFollowRulesOfReact @enableTransitivelyFreezeFunctionExpressions +type Bar = string; +function TypeAliasUsedAsParamAnnotation() { + type Foo = Bar; + const fun = (f: Foo) => { + console.log(f); + }; + fun("hello, world"); +} + +export const FIXTURE_ENTRYPOINT = { + fn: TypeAliasUsedAsParamAnnotation, + params: [], +}; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias-used-as-variable-annotation.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias-used-as-variable-annotation.expect.md new file mode 100644 index 0000000000..f97bcbb3a6 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias-used-as-variable-annotation.expect.md @@ -0,0 +1,46 @@ + +## Input + +```javascript +// @enableAssumeHooksFollowRulesOfReact @enableTransitivelyFreezeFunctionExpressions +type Bar = string; +function TypeAliasUsedAsVariableAnnotation() { + type Foo = Bar; + const fun = (f) => { + let g: Foo = f; + console.log(g); + }; + fun("hello, world"); +} + +export const FIXTURE_ENTRYPOINT = { + fn: TypeAliasUsedAsVariableAnnotation, + params: [], +}; + +``` + +## Code + +```javascript +// @enableAssumeHooksFollowRulesOfReact @enableTransitivelyFreezeFunctionExpressions +type Bar = string; +function TypeAliasUsedAsVariableAnnotation() { + const fun = (f) => { + const g = f; + console.log(g); + }; + + fun("hello, world"); +} + +export const FIXTURE_ENTRYPOINT = { + fn: TypeAliasUsedAsVariableAnnotation, + params: [], +}; + +``` + +### Eval output +(kind: ok) +logs: ['hello, world'] \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias-used-as-variable-annotation.ts b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias-used-as-variable-annotation.ts new file mode 100644 index 0000000000..8b8b0bfb41 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-alias-used-as-variable-annotation.ts @@ -0,0 +1,15 @@ +// @enableAssumeHooksFollowRulesOfReact @enableTransitivelyFreezeFunctionExpressions +type Bar = string; +function TypeAliasUsedAsVariableAnnotation() { + type Foo = Bar; + const fun = (f) => { + let g: Foo = f; + console.log(g); + }; + fun("hello, world"); +} + +export const FIXTURE_ENTRYPOINT = { + fn: TypeAliasUsedAsVariableAnnotation, + params: [], +};