Skip to content

Commit 814a418

Browse files
committed
[compiler] Make unary and binary operator types more precise
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-source-id: 9bf3b1a Pull Request resolved: #29880
1 parent 7f3911f commit 814a418

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1668,6 +1668,15 @@ function lowerExpression(
16681668
const left = lowerExpressionToTemporary(builder, leftPath);
16691669
const right = lowerExpressionToTemporary(builder, expr.get("right"));
16701670
const operator = expr.node.operator;
1671+
if (operator === "|>") {
1672+
builder.errors.push({
1673+
reason: `(BuildHIR::lowerExpression) Pipe operator not supported`,
1674+
severity: ErrorSeverity.Todo,
1675+
loc: leftPath.node.loc ?? null,
1676+
suggestions: null,
1677+
});
1678+
return { kind: "UnsupportedNode", node: exprNode, loc: exprLoc };
1679+
}
16711680
return {
16721681
kind: "BinaryExpression",
16731682
operator,
@@ -1893,7 +1902,9 @@ function lowerExpression(
18931902
);
18941903
}
18951904

1896-
const operators: { [key: string]: t.BinaryExpression["operator"] } = {
1905+
const operators: {
1906+
[key: string]: Exclude<t.BinaryExpression["operator"], "|>">;
1907+
} = {
18971908
"+=": "+",
18981909
"-=": "-",
18991910
"/=": "/",
@@ -2307,6 +2318,20 @@ function lowerExpression(
23072318
});
23082319
return { kind: "UnsupportedNode", node: expr.node, loc: exprLoc };
23092320
}
2321+
} else if (expr.node.operator === "throw") {
2322+
builder.errors.push({
2323+
reason: `Throw expressions are not supported`,
2324+
severity: ErrorSeverity.InvalidJS,
2325+
loc: expr.node.loc ?? null,
2326+
suggestions: [
2327+
{
2328+
description: "Remove this line",
2329+
range: [expr.node.start!, expr.node.end!],
2330+
op: CompilerSuggestionOperation.Remove,
2331+
},
2332+
],
2333+
});
2334+
return { kind: "UnsupportedNode", node: expr.node, loc: exprLoc };
23102335
} else {
23112336
return {
23122337
kind: "UnaryExpression",

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ export type InstructionValue =
866866
| JSXText
867867
| {
868868
kind: "BinaryExpression";
869-
operator: t.BinaryExpression["operator"];
869+
operator: Exclude<t.BinaryExpression["operator"], "|>">;
870870
left: Place;
871871
right: Place;
872872
loc: SourceLocation;
@@ -881,7 +881,7 @@ export type InstructionValue =
881881
| MethodCall
882882
| {
883883
kind: "UnaryExpression";
884-
operator: t.UnaryExpression["operator"];
884+
operator: Exclude<t.UnaryExpression["operator"], "throw" | "delete">;
885885
value: Place;
886886
loc: SourceLocation;
887887
}

0 commit comments

Comments
 (0)