From bc516409a6ab98a2179fc7628fe75a49827d85c7 Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Fri, 22 Mar 2024 09:15:29 -0700 Subject: [PATCH] Repro of for loop with context variable iterator variable `let` bindings of context variables are lowered to a DeclareContext + StoreContext, which breaks codegen for `for` loops which expect that all statements of the init block will lower to variable declarations. The two instructions produce a variable declaration and a reassignment. --- ...p-with-context-variable-iterator.expect.md | 35 +++++++++++++++++++ ...for-loop-with-context-variable-iterator.js | 10 ++++++ 2 files changed, 45 insertions(+) create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-for-loop-with-context-variable-iterator.expect.md create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-for-loop-with-context-variable-iterator.js diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-for-loop-with-context-variable-iterator.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-for-loop-with-context-variable-iterator.expect.md new file mode 100644 index 0000000000..a2fb2d6d60 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-for-loop-with-context-variable-iterator.expect.md @@ -0,0 +1,35 @@ + +## Input + +```javascript +function Component() { + const data = useData(); + const items = []; + // NOTE: `i` is a context variable because it's reassigned and also referenced + // within a closure, the `onClick` handler of each item + for (let i = MIN; i <= MAX; i += INCREMENT) { + items.push( data.set(i)} />); + } + return items; +} + +``` + + +## Error + +``` + 4 | // NOTE: `i` is a context variable because it's reassigned and also referenced + 5 | // within a closure, the `onClick` handler of each item +> 6 | for (let i = MIN; i <= MAX; i += INCREMENT) { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +> 7 | items.push( data.set(i)} />); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +> 8 | } + | ^^^^ [ReactForget] Invariant: Expected a variable declaration (6:8) + 9 | return items; + 10 | } + 11 | +``` + + \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-for-loop-with-context-variable-iterator.js b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-for-loop-with-context-variable-iterator.js new file mode 100644 index 0000000000..5330874b89 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-for-loop-with-context-variable-iterator.js @@ -0,0 +1,10 @@ +function Component() { + const data = useData(); + const items = []; + // NOTE: `i` is a context variable because it's reassigned and also referenced + // within a closure, the `onClick` handler of each item + for (let i = MIN; i <= MAX; i += INCREMENT) { + items.push( data.set(i)} />); + } + return items; +}