Skip to content

Commit 76d603a

Browse files
committed
[compiler] Support for non-declatation for in/of iterators
ghstack-source-id: a28801e022561029e2f46c3dcb858bd4a81dea6a Pull Request resolved: facebook#31710
1 parent 226b859 commit 76d603a

File tree

2 files changed

+35
-29
lines changed

2 files changed

+35
-29
lines changed

compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts

+35-23
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,12 @@ function lowerStatement(
10781078
const left = stmt.get('left');
10791079
const leftLoc = left.node.loc ?? GeneratedSource;
10801080
let test: Place;
1081+
const advanceIterator = lowerValueToTemporary(builder, {
1082+
kind: 'IteratorNext',
1083+
loc: leftLoc,
1084+
iterator: {...iterator},
1085+
collection: {...value},
1086+
});
10811087
if (left.isVariableDeclaration()) {
10821088
const declarations = left.get('declarations');
10831089
CompilerError.invariant(declarations.length === 1, {
@@ -1087,12 +1093,6 @@ function lowerStatement(
10871093
suggestions: null,
10881094
});
10891095
const id = declarations[0].get('id');
1090-
const advanceIterator = lowerValueToTemporary(builder, {
1091-
kind: 'IteratorNext',
1092-
loc: leftLoc,
1093-
iterator: {...iterator},
1094-
collection: {...value},
1095-
});
10961096
const assign = lowerAssignment(
10971097
builder,
10981098
leftLoc,
@@ -1103,13 +1103,19 @@ function lowerStatement(
11031103
);
11041104
test = lowerValueToTemporary(builder, assign);
11051105
} else {
1106-
builder.errors.push({
1107-
reason: `(BuildHIR::lowerStatement) Handle ${left.type} inits in ForOfStatement`,
1108-
severity: ErrorSeverity.Todo,
1109-
loc: left.node.loc ?? null,
1110-
suggestions: null,
1106+
CompilerError.invariant(left.isLVal(), {
1107+
loc: leftLoc,
1108+
reason: 'Expected ForOf init to be a variable declaration or lval',
11111109
});
1112-
return;
1110+
const assign = lowerAssignment(
1111+
builder,
1112+
leftLoc,
1113+
InstructionKind.Reassign,
1114+
left,
1115+
advanceIterator,
1116+
'Assignment',
1117+
);
1118+
test = lowerValueToTemporary(builder, assign);
11131119
}
11141120
builder.terminateWithContinuation(
11151121
{
@@ -1166,6 +1172,11 @@ function lowerStatement(
11661172
const left = stmt.get('left');
11671173
const leftLoc = left.node.loc ?? GeneratedSource;
11681174
let test: Place;
1175+
const nextPropertyTemp = lowerValueToTemporary(builder, {
1176+
kind: 'NextPropertyOf',
1177+
loc: leftLoc,
1178+
value,
1179+
});
11691180
if (left.isVariableDeclaration()) {
11701181
const declarations = left.get('declarations');
11711182
CompilerError.invariant(declarations.length === 1, {
@@ -1175,11 +1186,6 @@ function lowerStatement(
11751186
suggestions: null,
11761187
});
11771188
const id = declarations[0].get('id');
1178-
const nextPropertyTemp = lowerValueToTemporary(builder, {
1179-
kind: 'NextPropertyOf',
1180-
loc: leftLoc,
1181-
value,
1182-
});
11831189
const assign = lowerAssignment(
11841190
builder,
11851191
leftLoc,
@@ -1190,13 +1196,19 @@ function lowerStatement(
11901196
);
11911197
test = lowerValueToTemporary(builder, assign);
11921198
} else {
1193-
builder.errors.push({
1194-
reason: `(BuildHIR::lowerStatement) Handle ${left.type} inits in ForInStatement`,
1195-
severity: ErrorSeverity.Todo,
1196-
loc: left.node.loc ?? null,
1197-
suggestions: null,
1199+
CompilerError.invariant(left.isLVal(), {
1200+
loc: leftLoc,
1201+
reason: 'Expected ForIn init to be a variable declaration or lval',
11981202
});
1199-
return;
1203+
const assign = lowerAssignment(
1204+
builder,
1205+
leftLoc,
1206+
InstructionKind.Reassign,
1207+
left,
1208+
nextPropertyTemp,
1209+
'Assignment',
1210+
);
1211+
test = lowerValueToTemporary(builder, assign);
12001212
}
12011213
builder.terminateWithContinuation(
12021214
{

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-kitchensink.expect.md

-6
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,6 @@ Todo: (BuildHIR::lowerExpression) Handle tagged template with interpolations (30
9898
9999
Todo: (BuildHIR::lowerExpression) Handle tagged template where cooked value is different from raw value (34:34)
100100
101-
Todo: (BuildHIR::lowerStatement) Handle Identifier inits in ForOfStatement (36:36)
102-
103-
Todo: (BuildHIR::lowerStatement) Handle ArrayPattern inits in ForOfStatement (38:38)
104-
105-
Todo: (BuildHIR::lowerStatement) Handle ObjectPattern inits in ForOfStatement (40:40)
106-
107101
Todo: (BuildHIR::node.lowerReorderableExpression) Expression type `MemberExpression` cannot be safely reordered (57:57)
108102
109103
Todo: (BuildHIR::node.lowerReorderableExpression) Expression type `BinaryExpression` cannot be safely reordered (53:53)

0 commit comments

Comments
 (0)