Skip to content

Commit

Permalink
refactor(babel-plugin-react-compiler): throw if OptionalMemberExpress…
Browse files Browse the repository at this point in the history
…ion is on the left side of an AssignmentExpression before calling lowerAssignment
  • Loading branch information
michaelfaith committed Mar 8, 2025
1 parent 0b2bc53 commit b00503e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
12 changes: 10 additions & 2 deletions compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1909,11 +1909,19 @@ function lowerExpression(

if (operator === '=') {
const left = expr.get('left');

// OptionalMemberExpressions as the left side of an AssignmentExpression are not yet supported
if (left.isNodeType('OptionalMemberExpression')) {
CompilerError.throwTodo({
reason: `Unsupported syntax: OptionalMemberExpression on the left side of an AssignmentExpression`,
loc: left.node.loc ?? null,
});
}
return lowerAssignment(
builder,
left.node.loc ?? GeneratedSource,
InstructionKind.Reassign,
left,
left as NodePath<t.LVal>,
lowerExpressionToTemporary(builder, expr.get('right')),
left.isArrayPattern() || left.isObjectPattern()
? 'Destructure'
Expand Down Expand Up @@ -3545,7 +3553,7 @@ function lowerAssignment(
builder: HIRBuilder,
loc: SourceLocation,
kind: InstructionKind,
lvaluePath: NodePath<t.LVal | t.OptionalMemberExpression>,
lvaluePath: NodePath<t.LVal>,
value: Place,
assignmentKind: 'Destructure' | 'Assignment',
): InstructionValue {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,21 @@ export function findContextIdentifiers(
state: FindContextIdentifierState,
): void {
const left = path.get('left');

// OptionalMemberExpressions as the left side of an AssignmentExpression are not yet supported
if (left.isNodeType('OptionalMemberExpression')) {
CompilerError.throwTodo({
reason: `Unsupported syntax: OptionalMemberExpression on the left side of an AssignmentExpression`,
loc: left.node.loc ?? null,
});
}

const currentFn = state.currentFn.at(-1) ?? null;
handleAssignment(currentFn, state.identifiers, left);
handleAssignment(
currentFn,
state.identifiers,
left as NodePath<t.LVal>,
);
},
UpdateExpression(
path: NodePath<t.UpdateExpression>,
Expand Down Expand Up @@ -126,7 +139,7 @@ function handleIdentifier(
function handleAssignment(
currentFn: BabelFunction | null,
identifiers: Map<t.Identifier, IdentifierInfo>,
lvalPath: NodePath<t.LVal | t.OptionalMemberExpression>,
lvalPath: NodePath<t.LVal>,
): void {
/*
* Find all reassignments to identifiers declared outside of currentFn
Expand Down

0 comments on commit b00503e

Please sign in to comment.