Skip to content

Commit f5e96b9

Browse files
authored
[compiler] Add missing source locations to statements, expressions (#34406)
Adds missing locations to all the statement kinds that we produce in codegenInstruction(), and adds generic handling of source locations for the nodes produced by codegenInstructionValue(). There are definitely some places where we are still missing a location, but this should address some of the known issues we've seen such as missing location on `throw`. --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/34406). * #34394 * __->__ #34406 * #34346
1 parent 7899252 commit f5e96b9

File tree

92 files changed

+164
-59
lines changed

Some content is hidden

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

92 files changed

+164
-59
lines changed

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

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,8 @@ function codegenTerminal(
982982
if (terminal.targetKind === 'implicit') {
983983
return null;
984984
}
985-
return t.breakStatement(
985+
return createBreakStatement(
986+
terminal.loc,
986987
terminal.targetKind === 'labeled'
987988
? t.identifier(codegenLabel(terminal.target))
988989
: null,
@@ -992,14 +993,16 @@ function codegenTerminal(
992993
if (terminal.targetKind === 'implicit') {
993994
return null;
994995
}
995-
return t.continueStatement(
996+
return createContinueStatement(
997+
terminal.loc,
996998
terminal.targetKind === 'labeled'
997999
? t.identifier(codegenLabel(terminal.target))
9981000
: null,
9991001
);
10001002
}
10011003
case 'for': {
1002-
return t.forStatement(
1004+
return createForStatement(
1005+
terminal.loc,
10031006
codegenForInit(cx, terminal.init),
10041007
codegenInstructionValueToExpression(cx, terminal.test),
10051008
terminal.update !== null
@@ -1108,7 +1111,8 @@ function codegenTerminal(
11081111
`Unhandled lvalue kind: ${iterableItem.value.lvalue.kind}`,
11091112
);
11101113
}
1111-
return t.forInStatement(
1114+
return createForInStatement(
1115+
terminal.loc,
11121116
/*
11131117
* Special handling here since we only want the VariableDeclarators without any inits
11141118
* This needs to be updated when we handle non-trivial ForOf inits
@@ -1225,7 +1229,8 @@ function codegenTerminal(
12251229
`Unhandled lvalue kind: ${iterableItem.value.lvalue.kind}`,
12261230
);
12271231
}
1228-
return t.forOfStatement(
1232+
return createForOfStatement(
1233+
terminal.loc,
12291234
/*
12301235
* Special handling here since we only want the VariableDeclarators without any inits
12311236
* This needs to be updated when we handle non-trivial ForOf inits
@@ -1247,7 +1252,7 @@ function codegenTerminal(
12471252
alternate = block;
12481253
}
12491254
}
1250-
return t.ifStatement(test, consequent, alternate);
1255+
return createIfStatement(terminal.loc, test, consequent, alternate);
12511256
}
12521257
case 'return': {
12531258
const value = codegenPlaceToExpression(cx, terminal.value);
@@ -1258,7 +1263,8 @@ function codegenTerminal(
12581263
return t.returnStatement(value);
12591264
}
12601265
case 'switch': {
1261-
return t.switchStatement(
1266+
return createSwitchStatement(
1267+
terminal.loc,
12621268
codegenPlaceToExpression(cx, terminal.test),
12631269
terminal.cases.map(case_ => {
12641270
const test =
@@ -1271,15 +1277,26 @@ function codegenTerminal(
12711277
);
12721278
}
12731279
case 'throw': {
1274-
return t.throwStatement(codegenPlaceToExpression(cx, terminal.value));
1280+
return createThrowStatement(
1281+
terminal.loc,
1282+
codegenPlaceToExpression(cx, terminal.value),
1283+
);
12751284
}
12761285
case 'do-while': {
12771286
const test = codegenInstructionValueToExpression(cx, terminal.test);
1278-
return t.doWhileStatement(test, codegenBlock(cx, terminal.loop));
1287+
return createDoWhileStatement(
1288+
terminal.loc,
1289+
test,
1290+
codegenBlock(cx, terminal.loop),
1291+
);
12791292
}
12801293
case 'while': {
12811294
const test = codegenInstructionValueToExpression(cx, terminal.test);
1282-
return t.whileStatement(test, codegenBlock(cx, terminal.loop));
1295+
return createWhileStatement(
1296+
terminal.loc,
1297+
test,
1298+
codegenBlock(cx, terminal.loop),
1299+
);
12831300
}
12841301
case 'label': {
12851302
return codegenBlock(cx, terminal.block);
@@ -1290,7 +1307,8 @@ function codegenTerminal(
12901307
catchParam = convertIdentifier(terminal.handlerBinding.identifier);
12911308
cx.temp.set(terminal.handlerBinding.identifier.declarationId, null);
12921309
}
1293-
return t.tryStatement(
1310+
return createTryStatement(
1311+
terminal.loc,
12941312
codegenBlock(cx, terminal.block),
12951313
t.catchClause(catchParam, codegenBlock(cx, terminal.handler)),
12961314
);
@@ -1695,7 +1713,13 @@ const createExpressionStatement = withLoc(t.expressionStatement);
16951713
const _createLabelledStatement = withLoc(t.labeledStatement);
16961714
const createVariableDeclaration = withLoc(t.variableDeclaration);
16971715
const createFunctionDeclaration = withLoc(t.functionDeclaration);
1698-
const _createWhileStatement = withLoc(t.whileStatement);
1716+
const createWhileStatement = withLoc(t.whileStatement);
1717+
const createDoWhileStatement = withLoc(t.doWhileStatement);
1718+
const createSwitchStatement = withLoc(t.switchStatement);
1719+
const createIfStatement = withLoc(t.ifStatement);
1720+
const createForStatement = withLoc(t.forStatement);
1721+
const createForOfStatement = withLoc(t.forOfStatement);
1722+
const createForInStatement = withLoc(t.forInStatement);
16991723
const createTaggedTemplateExpression = withLoc(t.taggedTemplateExpression);
17001724
const createLogicalExpression = withLoc(t.logicalExpression);
17011725
const createSequenceExpression = withLoc(t.sequenceExpression);
@@ -1710,6 +1734,10 @@ const createJsxText = withLoc(t.jsxText);
17101734
const createJsxClosingElement = withLoc(t.jsxClosingElement);
17111735
const createJsxOpeningElement = withLoc(t.jsxOpeningElement);
17121736
const createStringLiteral = withLoc(t.stringLiteral);
1737+
const createThrowStatement = withLoc(t.throwStatement);
1738+
const createTryStatement = withLoc(t.tryStatement);
1739+
const createBreakStatement = withLoc(t.breakStatement);
1740+
const createContinueStatement = withLoc(t.continueStatement);
17131741

17141742
function createHookGuard(
17151743
guard: ExternalFunction,
@@ -2524,6 +2552,9 @@ function codegenInstructionValue(
25242552
);
25252553
}
25262554
}
2555+
if (instrValue.loc != null && instrValue.loc != GeneratedSource) {
2556+
value.loc = instrValue.loc;
2557+
}
25272558
return value;
25282559
}
25292560

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ function useFoo(t0) {
129129
t1 = null;
130130
break bb0;
131131
}
132+
132133
if (cond2) {
133134
mutate(s);
134135
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ function useFoo(t0) {
4343
if ($[0] !== cond || $[1] !== value) {
4444
bb0: {
4545
items = [];
46+
4647
if (cond) {
4748
break bb0;
4849
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ function Foo() {
3939
if (cond) {
4040
thing = makeObject_Primitives();
4141
}
42+
4243
if (CONST_TRUE) {
4344
mutate(thing);
4445
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { c as _c } from "react/compiler-runtime";
2323
function useBar(props) {
2424
const $ = _c(1);
2525
let z;
26+
2627
if (props.a) {
2728
if (props.b) {
2829
let t0;

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capture-param-mutate.expect.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ function getNativeLogFunction(level) {
6767
) {
6868
logLevel = LOG_LEVELS.warn;
6969
}
70+
7071
if (global.__inspectorLog) {
7172
global.__inspectorLog(
7273
INSPECTOR_LEVELS[logLevel],
@@ -75,6 +76,7 @@ function getNativeLogFunction(level) {
7576
INSPECTOR_FRAMES_TO_SKIP,
7677
);
7778
}
79+
7880
if (groupStack.length) {
7981
str = groupFormat("", str);
8082
}

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capture-ref-for-later-mutation.expect.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ function useKeyCommand() {
4747
};
4848

4949
const moveLeft = { handler: handleKey("left") };
50-
5150
const moveRight = { handler: handleKey("right") };
52-
5351
t0 = [moveLeft, moveRight];
5452
$[0] = t0;
5553
} else {

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/chained-assignment-context-variable.expect.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ function Component() {
3333
let y;
3434
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
3535
y = x = {};
36-
3736
const foo = () => {
3837
x = makeArray();
3938
};

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/conditional-on-mutable.expect.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ function ComponentA(props) {
4444
if (b) {
4545
a.push(props.p0);
4646
}
47+
4748
if (props.p1) {
4849
b.push(props.p2);
4950
}
@@ -68,6 +69,7 @@ function ComponentB(props) {
6869
if (mayMutate(b)) {
6970
a.push(props.p0);
7071
}
72+
7173
if (props.p1) {
7274
b.push(props.p2);
7375
}

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/conditional-set-state-in-render.expect.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ function Component(props) {
3434
const foo = () => {
3535
setX(1);
3636
};
37+
3738
if (props.cond) {
3839
setX(2);
3940
foo();

0 commit comments

Comments
 (0)