Skip to content

Commit 73fec52

Browse files
potetomichaelfaith
andcommitted
[compiler] Dedupe @babel/types
Extracting portions of #32416 for easier review. This PR dedupes @babel/types to resolve to 7.26.3, for compatibility in the root workspace where eslint-plugin-react-hooks resides. I also needed to update @babel/preset-typescript in snap. The compiler changes in HIR and ReactiveScopes were needed due to types changing. Notably, Babel [added support for optional chaining assignment](babel/babel#15751) (currently [Stage 1](https://github.com/tc39/proposal-optional-chaining-assignment)), so in the latest versions of @babel/types, AssignmentExpression.left can now also be of t.OptionalMemberExpression. Given that this is in Stage 1, the compiler probably shouldn't support this syntax, so this PR updates HIR to bailout with a TODO if there is a non LVal on the lhs of an Assignment Expression. There was also a small superficial SourceLocation change needed in `InferReactiveScopeVariables` as Babel 8 changes were [accidentally released in 7](babel/babel#10746 (comment)). It doesn't affect our analysis so it seems fine to just update with the new properties. Co-authored-by: michael faith <michaelfaith@users.noreply.github.com>
1 parent 3456b66 commit 73fec52

File tree

9 files changed

+194
-104
lines changed

9 files changed

+194
-104
lines changed

compiler/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"react-is": "0.0.0-experimental-4beb1fd8-20241118"
2727
},
2828
"devDependencies": {
29+
"@babel/types": "^7.26.0",
2930
"@tsconfig/strictest": "^2.0.5",
3031
"concurrently": "^7.4.0",
3132
"esbuild": "^0.25.0",
@@ -37,13 +38,15 @@
3738
"prettier-plugin-hermes-parser": "^0.26.0",
3839
"prompt-promise": "^1.0.3",
3940
"rimraf": "^5.0.10",
41+
"to-fast-properties": "^2.0.0",
4042
"tsup": "^8.4.0",
4143
"typescript": "^5.4.3",
4244
"wait-on": "^7.2.0",
4345
"yargs": "^17.7.2"
4446
},
4547
"resolutions": {
46-
"rimraf": "5.0.10"
48+
"rimraf": "5.0.10",
49+
"@babel/types": "7.26.3"
4750
},
4851
"packageManager": "yarn@1.22.22"
4952
}

compiler/packages/babel-plugin-react-compiler/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"watch": "yarn build --watch"
2121
},
2222
"dependencies": {
23-
"@babel/types": "^7.19.0"
23+
"@babel/types": "^7.26.0"
2424
},
2525
"devDependencies": {
2626
"@babel/core": "^7.2.0",

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

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1909,16 +1909,31 @@ function lowerExpression(
19091909

19101910
if (operator === '=') {
19111911
const left = expr.get('left');
1912-
return lowerAssignment(
1913-
builder,
1914-
left.node.loc ?? GeneratedSource,
1915-
InstructionKind.Reassign,
1916-
left,
1917-
lowerExpressionToTemporary(builder, expr.get('right')),
1918-
left.isArrayPattern() || left.isObjectPattern()
1919-
? 'Destructure'
1920-
: 'Assignment',
1921-
);
1912+
if (left.isLVal()) {
1913+
return lowerAssignment(
1914+
builder,
1915+
left.node.loc ?? GeneratedSource,
1916+
InstructionKind.Reassign,
1917+
left,
1918+
lowerExpressionToTemporary(builder, expr.get('right')),
1919+
left.isArrayPattern() || left.isObjectPattern()
1920+
? 'Destructure'
1921+
: 'Assignment',
1922+
);
1923+
} else {
1924+
/**
1925+
* OptionalMemberExpressions as the left side of an AssignmentExpression are Stage 1 and
1926+
* not supported by React Compiler yet.
1927+
*/
1928+
builder.errors.push({
1929+
reason: `(BuildHIR::lowerExpression) Unsupported syntax on the left side of an AssignmentExpression`,
1930+
description: `Expected an LVal, got: ${left.type}`,
1931+
severity: ErrorSeverity.Todo,
1932+
loc: left.node.loc ?? null,
1933+
suggestions: null,
1934+
});
1935+
return {kind: 'UnsupportedNode', node: exprNode, loc: exprLoc};
1936+
}
19221937
}
19231938

19241939
const operators: {
@@ -2091,7 +2106,7 @@ function lowerExpression(
20912106
propName = namePath.node.name;
20922107
if (propName.indexOf(':') !== -1) {
20932108
builder.errors.push({
2094-
reason: `(BuildHIR::lowerExpression) Unexpected colon in attribute name \`${name}\``,
2109+
reason: `(BuildHIR::lowerExpression) Unexpected colon in attribute name \`${namePath.node.name}\``,
20952110
severity: ErrorSeverity.Todo,
20962111
loc: namePath.node.loc ?? null,
20972112
suggestions: null,

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,20 @@ export function findContextIdentifiers(
6363
state: FindContextIdentifierState,
6464
): void {
6565
const left = path.get('left');
66-
const currentFn = state.currentFn.at(-1) ?? null;
67-
handleAssignment(currentFn, state.identifiers, left);
66+
if (left.isLVal()) {
67+
const currentFn = state.currentFn.at(-1) ?? null;
68+
handleAssignment(currentFn, state.identifiers, left);
69+
} else {
70+
/**
71+
* OptionalMemberExpressions as the left side of an AssignmentExpression are Stage 1 and
72+
* not supported by React Compiler yet.
73+
*/
74+
CompilerError.throwTodo({
75+
reason: `Unsupported syntax on the left side of an AssignmentExpression`,
76+
description: `Expected an LVal, got: ${left.type}`,
77+
loc: left.node.loc ?? null,
78+
});
79+
}
6880
},
6981
UpdateExpression(
7082
path: NodePath<t.UpdateExpression>,

compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/InferReactiveScopeVariables.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,15 @@ function mergeLocation(l: SourceLocation, r: SourceLocation): SourceLocation {
178178
return l;
179179
} else {
180180
return {
181+
filename: l.filename,
182+
identifierName: l.identifierName,
181183
start: {
184+
index: Math.min(l.start.index, r.start.index),
182185
line: Math.min(l.start.line, r.start.line),
183186
column: Math.min(l.start.column, r.start.column),
184187
},
185188
end: {
189+
index: Math.max(l.end.index, r.end.index),
186190
line: Math.max(l.end.line, r.end.line),
187191
column: Math.max(l.end.column, r.end.column),
188192
},
@@ -202,7 +206,7 @@ export function inRange(
202206
return id >= range.start && id < range.end;
203207
}
204208

205-
function mayAllocate(env: Environment, instruction: Instruction): boolean {
209+
function mayAllocate(_env: Environment, instruction: Instruction): boolean {
206210
const {value} = instruction;
207211
switch (value.kind) {
208212
case 'Destructure': {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ export {
2222
findDirectiveEnablingMemoization,
2323
findDirectiveDisablingMemoization,
2424
type CompilerPipelineValue,
25+
type Logger,
26+
type LoggerEvent,
2527
type PluginOptions,
2628
} from './Entrypoint';
2729
export {

compiler/packages/eslint-plugin-react-compiler/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"devDependencies": {
2323
"@babel/preset-env": "^7.22.4",
2424
"@babel/preset-typescript": "^7.18.6",
25-
"@babel/types": "^7.19.0",
25+
"@babel/types": "^7.26.0",
2626
"@types/eslint": "^8.56.12",
2727
"@types/node": "^20.2.5",
2828
"babel-jest": "^29.0.3",

compiler/packages/snap/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"@babel/code-frame": "^7.22.5",
2424
"@babel/plugin-syntax-jsx": "^7.18.6",
2525
"@babel/preset-flow": "^7.7.4",
26-
"@babel/preset-typescript": "^7.18.6",
26+
"@babel/preset-typescript": "^7.26.0",
2727
"@parcel/watcher": "^2.1.0",
2828
"@testing-library/react": "^13.4.0",
2929
"babel-plugin-idx": "^3.0.3",

0 commit comments

Comments
 (0)