Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 32 additions & 21 deletions compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1934,7 +1934,6 @@ function lowerExpression(
switch (leftNode.type) {
case 'Identifier': {
const leftExpr = left as NodePath<t.Identifier>;
const identifier = lowerIdentifier(builder, leftExpr);
const leftPlace = lowerExpressionToTemporary(builder, leftExpr);
const right = lowerExpressionToTemporary(builder, expr.get('right'));
const binaryPlace = lowerValueToTemporary(builder, {
Expand All @@ -1944,30 +1943,42 @@ function lowerExpression(
right,
loc: exprLoc,
});
const kind = getStoreKind(builder, leftExpr);
if (kind === 'StoreLocal') {
lowerValueToTemporary(builder, {
kind: 'StoreLocal',
lvalue: {
place: {...identifier},
kind: InstructionKind.Reassign,
},
value: {...binaryPlace},
type: null,
loc: exprLoc,
});
return {kind: 'LoadLocal', place: identifier, loc: exprLoc};
const binding = builder.resolveIdentifier(leftExpr);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the key change: we check if the identifier is a local (or non-local) binding

if (binding.kind === 'Identifier') {
const identifier = lowerIdentifier(builder, leftExpr);
const kind = getStoreKind(builder, leftExpr);
if (kind === 'StoreLocal') {
lowerValueToTemporary(builder, {
kind: 'StoreLocal',
lvalue: {
place: {...identifier},
kind: InstructionKind.Reassign,
},
value: {...binaryPlace},
type: null,
loc: exprLoc,
});
return {kind: 'LoadLocal', place: identifier, loc: exprLoc};
} else {
lowerValueToTemporary(builder, {
kind: 'StoreContext',
lvalue: {
place: {...identifier},
kind: InstructionKind.Reassign,
},
value: {...binaryPlace},
loc: exprLoc,
});
return {kind: 'LoadContext', place: identifier, loc: exprLoc};
Comment on lines +1948 to +1972
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Only whitespace changes here)

}
} else {
lowerValueToTemporary(builder, {
kind: 'StoreContext',
lvalue: {
place: {...identifier},
kind: InstructionKind.Reassign,
},
const temporary = lowerValueToTemporary(builder, {
kind: 'StoreGlobal',
name: leftExpr.node.name,
value: {...binaryPlace},
loc: exprLoc,
});
return {kind: 'LoadContext', place: identifier, loc: exprLoc};
return {kind: 'LoadLocal', place: temporary, loc: temporary.loc};
}
}
case 'MemberExpression': {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

## Input

```javascript
let renderCount = 0;
function useFoo() {
renderCount += 1;
return renderCount;
}

export const FIXTURE_ENTRYPOINT = {
fn: useFoo,
params: [],
};

```


## Error

```
1 | let renderCount = 0;
2 | function useFoo() {
> 3 | renderCount += 1;
| ^^^^^^^^^^^^^^^^ InvalidReact: Unexpected reassignment of a variable which was defined outside of the component. Components and hooks should be pure and side-effect free, but variable reassignment is a form of side-effect. If this variable is used in rendering, use useState instead. (https://react.dev/reference/rules/components-and-hooks-must-be-pure#side-effects-must-run-outside-of-render) (3:3)
4 | return renderCount;
5 | }
6 |
```


Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function Foo() {
return t0;
}
function _temp() {
renderCount = renderCount + 1;
return renderCount;
}

Expand All @@ -49,4 +50,6 @@ export const FIXTURE_ENTRYPOINT = {
};

```


### Eval output
(kind: ok) <div>{"cb":{"kind":"Function","result":1},"shouldInvokeFns":true}</div>
2 changes: 0 additions & 2 deletions compiler/packages/snap/src/SproutTodoFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,6 @@ const skipFilter = new Set([
'rules-of-hooks/rules-of-hooks-69521d94fa03',

// bugs
'bug-invalid-update-global-should-bailout',
'bug-update-global-in-callback',
'bug-invalid-hoisting-functionexpr',
'original-reactive-scopes-fork/bug-nonmutating-capture-in-unsplittable-memo-block',
'original-reactive-scopes-fork/bug-hoisted-declaration-with-scope',
Expand Down