Skip to content

Commit 2142d82

Browse files
author
Joe Savona
committed
[compiler] Add missing source locations to statements, expressions
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`.
1 parent 0fba073 commit 2142d82

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
@@ -945,7 +945,8 @@ function codegenTerminal(
945945
if (terminal.targetKind === 'implicit') {
946946
return null;
947947
}
948-
return t.breakStatement(
948+
return createBreakStatement(
949+
terminal.loc,
949950
terminal.targetKind === 'labeled'
950951
? t.identifier(codegenLabel(terminal.target))
951952
: null,
@@ -955,14 +956,16 @@ function codegenTerminal(
955956
if (terminal.targetKind === 'implicit') {
956957
return null;
957958
}
958-
return t.continueStatement(
959+
return createContinueStatement(
960+
terminal.loc,
959961
terminal.targetKind === 'labeled'
960962
? t.identifier(codegenLabel(terminal.target))
961963
: null,
962964
);
963965
}
964966
case 'for': {
965-
return t.forStatement(
967+
return createForStatement(
968+
terminal.loc,
966969
codegenForInit(cx, terminal.init),
967970
codegenInstructionValueToExpression(cx, terminal.test),
968971
terminal.update !== null
@@ -1047,7 +1050,8 @@ function codegenTerminal(
10471050
`Unhandled lvalue kind: ${iterableItem.value.lvalue.kind}`,
10481051
);
10491052
}
1050-
return t.forInStatement(
1053+
return createForInStatement(
1054+
terminal.loc,
10511055
/*
10521056
* Special handling here since we only want the VariableDeclarators without any inits
10531057
* This needs to be updated when we handle non-trivial ForOf inits
@@ -1140,7 +1144,8 @@ function codegenTerminal(
11401144
`Unhandled lvalue kind: ${iterableItem.value.lvalue.kind}`,
11411145
);
11421146
}
1143-
return t.forOfStatement(
1147+
return createForOfStatement(
1148+
terminal.loc,
11441149
/*
11451150
* Special handling here since we only want the VariableDeclarators without any inits
11461151
* This needs to be updated when we handle non-trivial ForOf inits
@@ -1162,7 +1167,7 @@ function codegenTerminal(
11621167
alternate = block;
11631168
}
11641169
}
1165-
return t.ifStatement(test, consequent, alternate);
1170+
return createIfStatement(terminal.loc, test, consequent, alternate);
11661171
}
11671172
case 'return': {
11681173
const value = codegenPlaceToExpression(cx, terminal.value);
@@ -1173,7 +1178,8 @@ function codegenTerminal(
11731178
return t.returnStatement(value);
11741179
}
11751180
case 'switch': {
1176-
return t.switchStatement(
1181+
return createSwitchStatement(
1182+
terminal.loc,
11771183
codegenPlaceToExpression(cx, terminal.test),
11781184
terminal.cases.map(case_ => {
11791185
const test =
@@ -1186,15 +1192,26 @@ function codegenTerminal(
11861192
);
11871193
}
11881194
case 'throw': {
1189-
return t.throwStatement(codegenPlaceToExpression(cx, terminal.value));
1195+
return createThrowStatement(
1196+
terminal.loc,
1197+
codegenPlaceToExpression(cx, terminal.value),
1198+
);
11901199
}
11911200
case 'do-while': {
11921201
const test = codegenInstructionValueToExpression(cx, terminal.test);
1193-
return t.doWhileStatement(test, codegenBlock(cx, terminal.loop));
1202+
return createDoWhileStatement(
1203+
terminal.loc,
1204+
test,
1205+
codegenBlock(cx, terminal.loop),
1206+
);
11941207
}
11951208
case 'while': {
11961209
const test = codegenInstructionValueToExpression(cx, terminal.test);
1197-
return t.whileStatement(test, codegenBlock(cx, terminal.loop));
1210+
return createWhileStatement(
1211+
terminal.loc,
1212+
test,
1213+
codegenBlock(cx, terminal.loop),
1214+
);
11981215
}
11991216
case 'label': {
12001217
return codegenBlock(cx, terminal.block);
@@ -1205,7 +1222,8 @@ function codegenTerminal(
12051222
catchParam = convertIdentifier(terminal.handlerBinding.identifier);
12061223
cx.temp.set(terminal.handlerBinding.identifier.declarationId, null);
12071224
}
1208-
return t.tryStatement(
1225+
return createTryStatement(
1226+
terminal.loc,
12091227
codegenBlock(cx, terminal.block),
12101228
t.catchClause(catchParam, codegenBlock(cx, terminal.handler)),
12111229
);
@@ -1543,7 +1561,13 @@ const createExpressionStatement = withLoc(t.expressionStatement);
15431561
const _createLabelledStatement = withLoc(t.labeledStatement);
15441562
const createVariableDeclaration = withLoc(t.variableDeclaration);
15451563
const createFunctionDeclaration = withLoc(t.functionDeclaration);
1546-
const _createWhileStatement = withLoc(t.whileStatement);
1564+
const createWhileStatement = withLoc(t.whileStatement);
1565+
const createDoWhileStatement = withLoc(t.doWhileStatement);
1566+
const createSwitchStatement = withLoc(t.switchStatement);
1567+
const createIfStatement = withLoc(t.ifStatement);
1568+
const createForStatement = withLoc(t.forStatement);
1569+
const createForOfStatement = withLoc(t.forOfStatement);
1570+
const createForInStatement = withLoc(t.forInStatement);
15471571
const createTaggedTemplateExpression = withLoc(t.taggedTemplateExpression);
15481572
const createLogicalExpression = withLoc(t.logicalExpression);
15491573
const createSequenceExpression = withLoc(t.sequenceExpression);
@@ -1558,6 +1582,10 @@ const createJsxText = withLoc(t.jsxText);
15581582
const createJsxClosingElement = withLoc(t.jsxClosingElement);
15591583
const createJsxOpeningElement = withLoc(t.jsxOpeningElement);
15601584
const createStringLiteral = withLoc(t.stringLiteral);
1585+
const createThrowStatement = withLoc(t.throwStatement);
1586+
const createTryStatement = withLoc(t.tryStatement);
1587+
const createBreakStatement = withLoc(t.breakStatement);
1588+
const createContinueStatement = withLoc(t.continueStatement);
15611589

15621590
function createHookGuard(
15631591
guard: ExternalFunction,
@@ -2314,6 +2342,9 @@ function codegenInstructionValue(
23142342
);
23152343
}
23162344
}
2345+
if (instrValue.loc != null && instrValue.loc != GeneratedSource) {
2346+
value.loc = instrValue.loc;
2347+
}
23172348
return value;
23182349
}
23192350

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)