diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts b/compiler/forget/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts index dcb36b25c0..3763708c77 100644 --- a/compiler/forget/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts +++ b/compiler/forget/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts @@ -1790,10 +1790,15 @@ function lowerExpression( builder, expr.node.loc ?? GeneratedSource ); - const identifier = lowerIdentifier( + const identifier = lowerIdentifierForAssignment( builder, + argument.node.loc ?? GeneratedSource, + InstructionKind.Reassign, argument as NodePath ); + if (identifier === null) { + return { kind: "UnsupportedNode", node: exprNode, loc: exprLoc }; + } builder.push({ id: makeInstructionId(0), lvalue: { ...temp }, diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.mutate-global-increment-op-invalid-react.expect.md b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.mutate-global-increment-op-invalid-react.expect.md new file mode 100644 index 0000000000..260365eb2c --- /dev/null +++ b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.mutate-global-increment-op-invalid-react.expect.md @@ -0,0 +1,21 @@ + +## Input + +```javascript +let renderCount = 0; + +function NoHooks() { + renderCount++; + return
; +} + +``` + + +## Error + +``` +[ReactForget] InvalidReact: This reassigns a variable which was not defined inside of the component. Components should be pure and side-effect free. If this variable is used in rendering, use useState instead. (https://react.dev/learn/keeping-components-pure) (4:4) +``` + + \ No newline at end of file diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/mutate-global-increment-op-invalid-react.js b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.mutate-global-increment-op-invalid-react.js similarity index 100% rename from compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/mutate-global-increment-op-invalid-react.js rename to compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.mutate-global-increment-op-invalid-react.js diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/mutate-global-increment-op-invalid-react.expect.md b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/mutate-global-increment-op-invalid-react.expect.md deleted file mode 100644 index 0f8b811533..0000000000 --- a/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/mutate-global-increment-op-invalid-react.expect.md +++ /dev/null @@ -1,33 +0,0 @@ - -## Input - -```javascript -let renderCount = 0; - -function NoHooks() { - renderCount++; - return
; -} - -``` - -## Code - -```javascript -import { unstable_useMemoCache as useMemoCache } from "react"; -let renderCount = 0; - -function NoHooks() { - const $ = useMemoCache(1); - let t0; - if ($[0] === Symbol.for("react.memo_cache_sentinel")) { - t0 =
; - $[0] = t0; - } else { - t0 = $[0]; - } - return t0; -} - -``` - \ No newline at end of file