[lower][patch] Recognize UpdateExpression as lval assignment
--- Oops, I broke this in #2552. We never handled UpdateExpressions to context variables previously, so this PR also adds a todo bailout
Mofei Zhang committed
Feb 9, 2024 at 16:18 UTC
5dd55e881b374a5a0b8c5c962c08bee194297f15
4 files changed
+59
compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts
+8
@@ -2238,6 +2238,14 @@ function lowerExpression(
2238
suggestions: null,
2239
});
2240
return { kind: "UnsupportedNode", node: exprNode, loc: exprLoc };
2241
+ } else if (builder.isContextIdentifier(argument)) {
2242
+ builder.errors.push({
2243
+ reason: `(BuildHIR::lowerExpression) Handle UpdateExpression to variables captured within lambdas.`,
2244
+ severity: ErrorSeverity.Todo,
2245
+ loc: exprPath.node.loc ?? null,
2246
+ suggestions: null,
2247
+ });
2248
+ return { kind: "UnsupportedNode", node: exprNode, loc: exprLoc };
2249
}
2250
const lvalue = lowerIdentifierForAssignment(
2251
builder,
compiler/packages/babel-plugin-react-forget/src/HIR/FindContextIdentifiers.ts
+10
@@ -66,6 +66,16 @@ export function findContextIdentifiers(
66
const currentFn = state.currentFn.at(-1) ?? null;
67
handleAssignment(currentFn, state.identifiers, left);
68
},
69
+ UpdateExpression(
70
+ path: NodePath<t.UpdateExpression>,
71
+ state: FindContextIdentifierState
72
+ ): void {
73
+ const argument = path.get("argument");
74
+ const currentFn = state.currentFn.at(-1) ?? null;
75
+ if (argument.isLVal()) {
76
+ handleAssignment(currentFn, state.identifiers, argument);
77
+ }
78
+ },
79
Identifier(
80
path: NodePath<t.Identifier>,
81
state: FindContextIdentifierState
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-handle-update-context-identifiers.expect.md
new
+28
@@ -0,0 +1,28 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function useFoo() {
6
+ let counter = 2;
7
+ const fn = () => {
8
+ return counter++;
9
+ };
10
+
11
+ return fn();
12
+}
13
+
14
+export const FIXTURE_ENTRYPOINT = {
15
+ fn: useFoo,
16
+ params: [],
17
+};
18
+
19
+```
20
+
21
+
22
+## Error
23
+
24
+```
25
+[ReactForget] Todo: (BuildHIR::lowerExpression) Handle UpdateExpression to variables captured within lambdas. (4:4)
26
+```
27
+
28
+
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-handle-update-context-identifiers.js
new
+13
@@ -0,0 +1,13 @@
1
+function useFoo() {
2
+ let counter = 2;
3
+ const fn = () => {
4
+ return counter++;
5
+ };
6
+
7
+ return fn();
8
+}
9
+
10
+export const FIXTURE_ENTRYPOINT = {
11
+ fn: useFoo,
12
+ params: [],
13
+};