Skip to content

Commit 64b4605

Browse files
authored
[compiler] fix source location for return statements (facebook#35660)
Fixes missing source locations for ReturnStatement nodes in generated ast. Simple change using existing pattern, only required changes to the codegen step, no other pipeline changes. **Most file changes are new lines in generated code.** [First commit](facebook@d15e90e) has the relevant changes, second commit has the noisy snap updates. I added an exception to the validator to not report an error when a return statement will be optimized to an implicit return by codegen, as there's no separate return statement to instrument anyways in the final ast. An edge case when it comes to preserving source locations for instrumentation that is likely not as common for most babel transforms since they are not doing optimizations.
1 parent da64117 commit 64b4605

229 files changed

Lines changed: 250 additions & 41 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,9 +1160,9 @@ function codegenTerminal(
11601160
const value = codegenPlaceToExpression(cx, terminal.value);
11611161
if (value.type === 'Identifier' && value.name === 'undefined') {
11621162
// Use implicit undefined
1163-
return t.returnStatement();
1163+
return createReturnStatement(terminal.loc);
11641164
}
1165-
return t.returnStatement(value);
1165+
return createReturnStatement(terminal.loc, value);
11661166
}
11671167
case 'switch': {
11681168
return createSwitchStatement(
@@ -1545,6 +1545,7 @@ const createThrowStatement = withLoc(t.throwStatement);
15451545
const createTryStatement = withLoc(t.tryStatement);
15461546
const createBreakStatement = withLoc(t.breakStatement);
15471547
const createContinueStatement = withLoc(t.continueStatement);
1548+
const createReturnStatement = withLoc(t.returnStatement);
15481549

15491550
function createVariableDeclarator(
15501551
id: t.LVal,

compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateSourceLocations.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,23 @@ export function validateSourceLocations(
149149
return;
150150
}
151151

152+
/*
153+
* Skip return statements inside arrow functions that will be simplified to expression body.
154+
* The compiler transforms `() => { return expr }` to `() => expr` in CodegenReactiveFunction
155+
*/
156+
if (t.isReturnStatement(node) && node.argument != null) {
157+
const parentBody = path.parentPath;
158+
const parentFunc = parentBody?.parentPath;
159+
if (
160+
parentBody?.isBlockStatement() &&
161+
parentFunc?.isArrowFunctionExpression() &&
162+
parentBody.node.body.length === 1 &&
163+
parentBody.node.directives.length === 0
164+
) {
165+
return;
166+
}
167+
}
168+
152169
// Collect the location if it exists
153170
if (node.loc) {
154171
const key = locationKey(node.loc);

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/alias-while.expect.md

Lines changed: 1 addition & 0 deletions

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/align-scopes-nested-block-structure.expect.md

Lines changed: 1 addition & 0 deletions

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/align-scopes-reactive-scope-overlaps-if.expect.md

Lines changed: 1 addition & 0 deletions

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/align-scopes-trycatch-nested-overlapping-range.expect.md

Lines changed: 1 addition & 0 deletions

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/array-at-mutate-after-capture.expect.md

Lines changed: 1 addition & 0 deletions

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/arrow-function-one-line-directive.expect.md

Lines changed: 1 addition & 0 deletions

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/assignment-in-nested-if.expect.md

Lines changed: 1 addition & 0 deletions

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/block-scoping-switch-variable-scoping.expect.md

Lines changed: 1 addition & 0 deletions

0 commit comments

Comments
 (0)