[hir] Check for globals while lowering UpdateExpression

lowerIdentifierForAssignment does extra checks such as checking if globals are 
lvalues. 

UpdateExpression lowers into an assignment and previously we missed out on such 
checks.
This commit is contained in:
Sathya Gunasekaran
2023-07-05 20:21:09 +05:30
parent 8eca031508
commit 4a8b36b8b1
4 changed files with 27 additions and 34 deletions
@@ -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<t.Identifier>
);
if (identifier === null) {
return { kind: "UnsupportedNode", node: exprNode, loc: exprLoc };
}
builder.push({
id: makeInstructionId(0),
lvalue: { ...temp },
@@ -0,0 +1,21 @@
## Input
```javascript
let renderCount = 0;
function NoHooks() {
renderCount++;
return <div />;
}
```
## 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)
```
@@ -1,33 +0,0 @@
## Input
```javascript
let renderCount = 0;
function NoHooks() {
renderCount++;
return <div />;
}
```
## 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 = <div />;
$[0] = t0;
} else {
t0 = $[0];
}
return t0;
}
```