Skip to content

Commit

Permalink
[compiler] Make unary and binary operator types more precise
Browse files Browse the repository at this point in the history
Summary: Minor change inspired by #29863: the BuildHIR pass ensures that Binary and UnaryOperator nodes only use a limited set of the operators that babel's operator types represent, which that pr relies on for safe reorderability, but the type of those HIR nodes admits the other operators. For example, even though you can't build an HIR UnaryOperator with `delete` as the operator, it is a valid HIR node--and if we made a mistaken change that let you build such a node, it would be unsafe to reorder.

This pr makes the typing of operators stricter to prevent that.

[ghstack-poisoned]
  • Loading branch information
mvitousek committed Jun 12, 2024
1 parent 55fdcf8 commit 1c8cb1b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1668,6 +1668,15 @@ function lowerExpression(
const left = lowerExpressionToTemporary(builder, leftPath);
const right = lowerExpressionToTemporary(builder, expr.get("right"));
const operator = expr.node.operator;
if (operator === "|>") {
builder.errors.push({
reason: `(BuildHIR::lowerExpression) Pipe operator not supported`,
severity: ErrorSeverity.Todo,
loc: leftPath.node.loc ?? null,
suggestions: null,
});
return { kind: "UnsupportedNode", node: exprNode, loc: exprLoc };
}
return {
kind: "BinaryExpression",
operator,
Expand Down Expand Up @@ -1893,7 +1902,9 @@ function lowerExpression(
);
}

const operators: { [key: string]: t.BinaryExpression["operator"] } = {
const operators: {
[key: string]: Exclude<t.BinaryExpression["operator"], "|>">;
} = {
"+=": "+",
"-=": "-",
"/=": "/",
Expand Down Expand Up @@ -2307,6 +2318,20 @@ function lowerExpression(
});
return { kind: "UnsupportedNode", node: expr.node, loc: exprLoc };
}
} else if (expr.node.operator === "throw") {
builder.errors.push({
reason: `Throw expressions are not supported`,
severity: ErrorSeverity.InvalidJS,
loc: expr.node.loc ?? null,
suggestions: [
{
description: "Remove this line",
range: [expr.node.start!, expr.node.end!],
op: CompilerSuggestionOperation.Remove,
},
],
});
return { kind: "UnsupportedNode", node: expr.node, loc: exprLoc };
} else {
return {
kind: "UnaryExpression",
Expand Down
4 changes: 2 additions & 2 deletions compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ export type InstructionValue =
| JSXText
| {
kind: "BinaryExpression";
operator: t.BinaryExpression["operator"];
operator: Exclude<t.BinaryExpression["operator"], "|>">;
left: Place;
right: Place;
loc: SourceLocation;
Expand All @@ -881,7 +881,7 @@ export type InstructionValue =
| MethodCall
| {
kind: "UnaryExpression";
operator: t.UnaryExpression["operator"];
operator: Exclude<t.UnaryExpression["operator"], "throw" | "delete">;
value: Place;
loc: SourceLocation;
}
Expand Down

0 comments on commit 1c8cb1b

Please sign in to comment.