Skip to content

Commit f4e4dc0

Browse files
committed
Fix variable declarator source locations
1 parent 093b324 commit f4e4dc0

File tree

5 files changed

+41
-103
lines changed

5 files changed

+41
-103
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4026,6 +4026,7 @@ function lowerAssignment(
40264026
pattern: {
40274027
kind: 'ArrayPattern',
40284028
items,
4029+
loc: lvalue.node.loc ?? GeneratedSource,
40294030
},
40304031
},
40314032
value,
@@ -4203,6 +4204,7 @@ function lowerAssignment(
42034204
pattern: {
42044205
kind: 'ObjectPattern',
42054206
properties,
4207+
loc: lvalue.node.loc ?? GeneratedSource,
42064208
},
42074209
},
42084210
value,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,11 +694,13 @@ export type SpreadPattern = {
694694
export type ArrayPattern = {
695695
kind: 'ArrayPattern';
696696
items: Array<Place | SpreadPattern | Hole>;
697+
loc: SourceLocation;
697698
};
698699

699700
export type ObjectPattern = {
700701
kind: 'ObjectPattern';
701702
properties: Array<ObjectProperty | SpreadPattern>;
703+
loc: SourceLocation;
702704
};
703705

704706
export type ObjectPropertyKey =

compiler/packages/babel-plugin-react-compiler/src/Optimization/OutlineJsx.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@ function emitDestructureProps(
515515
pattern: {
516516
kind: 'ObjectPattern',
517517
properties,
518+
loc: GeneratedSource,
518519
},
519520
kind: InstructionKind.Let,
520521
},

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

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,7 +1406,7 @@ function codegenInstructionNullable(
14061406
suggestions: null,
14071407
});
14081408
return createVariableDeclaration(instr.loc, 'const', [
1409-
t.variableDeclarator(codegenLValue(cx, lvalue), value),
1409+
createVariableDeclarator(codegenLValue(cx, lvalue), value),
14101410
]);
14111411
}
14121412
case InstructionKind.Function: {
@@ -1470,7 +1470,7 @@ function codegenInstructionNullable(
14701470
suggestions: null,
14711471
});
14721472
return createVariableDeclaration(instr.loc, 'let', [
1473-
t.variableDeclarator(codegenLValue(cx, lvalue), value),
1473+
createVariableDeclarator(codegenLValue(cx, lvalue), value),
14741474
]);
14751475
}
14761476
case InstructionKind.Reassign: {
@@ -1710,6 +1710,9 @@ function withLoc<T extends (...args: Array<any>) => t.Node>(
17101710
};
17111711
}
17121712

1713+
const createIdentifier = withLoc(t.identifier);
1714+
const createArrayPattern = withLoc(t.arrayPattern);
1715+
const createObjectPattern = withLoc(t.objectPattern);
17131716
const createBinaryExpression = withLoc(t.binaryExpression);
17141717
const createExpressionStatement = withLoc(t.expressionStatement);
17151718
const _createLabelledStatement = withLoc(t.labeledStatement);
@@ -1741,6 +1744,29 @@ const createTryStatement = withLoc(t.tryStatement);
17411744
const createBreakStatement = withLoc(t.breakStatement);
17421745
const createContinueStatement = withLoc(t.continueStatement);
17431746

1747+
function createVariableDeclarator(
1748+
id: t.LVal,
1749+
init?: t.Expression | null,
1750+
): t.VariableDeclarator {
1751+
const node = t.variableDeclarator(id, init);
1752+
1753+
/*
1754+
* The variable declarator location is not preserved in HIR, however, we can use the
1755+
* start location of the id and the end location of the init to recreate the
1756+
* exact original variable declarator location.
1757+
*/
1758+
if (id.loc && init?.loc) {
1759+
node.loc = {
1760+
start: id.loc.start,
1761+
end: init.loc.end,
1762+
filename: id.loc.filename,
1763+
identifierName: undefined,
1764+
};
1765+
}
1766+
1767+
return node;
1768+
}
1769+
17441770
function createHookGuard(
17451771
guard: ExternalFunction,
17461772
context: ProgramContext,
@@ -1848,7 +1874,7 @@ function codegenInstruction(
18481874
);
18491875
} else {
18501876
return createVariableDeclaration(instr.loc, 'const', [
1851-
t.variableDeclarator(
1877+
createVariableDeclarator(
18521878
convertIdentifier(instr.lvalue.identifier),
18531879
expressionValue,
18541880
),
@@ -2775,7 +2801,7 @@ function codegenArrayPattern(
27752801
): t.ArrayPattern {
27762802
const hasHoles = !pattern.items.every(e => e.kind !== 'Hole');
27772803
if (hasHoles) {
2778-
const result = t.arrayPattern([]);
2804+
const result = createArrayPattern(pattern.loc, []);
27792805
/*
27802806
* Older versions of babel have a validation bug fixed by
27812807
* https://github.com/babel/babel/pull/10917
@@ -2796,7 +2822,8 @@ function codegenArrayPattern(
27962822
}
27972823
return result;
27982824
} else {
2799-
return t.arrayPattern(
2825+
return createArrayPattern(
2826+
pattern.loc,
28002827
pattern.items.map(item => {
28012828
if (item.kind === 'Hole') {
28022829
return null;
@@ -2816,7 +2843,8 @@ function codegenLValue(
28162843
return codegenArrayPattern(cx, pattern);
28172844
}
28182845
case 'ObjectPattern': {
2819-
return t.objectPattern(
2846+
return createObjectPattern(
2847+
pattern.loc,
28202848
pattern.properties.map(property => {
28212849
if (property.kind === 'ObjectProperty') {
28222850
const key = codegenObjectPropertyKey(cx, property.key);
@@ -2935,7 +2963,7 @@ function convertIdentifier(identifier: Identifier): t.Identifier {
29352963
suggestions: null,
29362964
},
29372965
);
2938-
return t.identifier(identifier.name.value);
2966+
return createIdentifier(identifier.loc, identifier.name.value);
29392967
}
29402968

29412969
function compareScopeDependency(

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-missing-source-locations.expect.md

Lines changed: 1 addition & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -38,85 +38,7 @@ function Component({prop1, prop2}) {
3838
## Error
3939

4040
```
41-
Found 13 errors:
42-
43-
Todo: Important source location missing in generated code
44-
45-
Source location for VariableDeclarator is missing in the generated output. This can cause coverage instrumentation to fail to track this code properly, resulting in inaccurate coverage reports..
46-
47-
error.todo-missing-source-locations.ts:5:8
48-
3 |
49-
4 | function Component({prop1, prop2}) {
50-
> 5 | const x = prop1 + prop2;
51-
| ^^^^^^^^^^^^^^^^^
52-
6 | const y = x * 2;
53-
7 | const arr = [x, y];
54-
8 | const obj = {x, y};
55-
56-
Todo: Important source location missing in generated code
57-
58-
Source location for VariableDeclarator is missing in the generated output. This can cause coverage instrumentation to fail to track this code properly, resulting in inaccurate coverage reports..
59-
60-
error.todo-missing-source-locations.ts:6:8
61-
4 | function Component({prop1, prop2}) {
62-
5 | const x = prop1 + prop2;
63-
> 6 | const y = x * 2;
64-
| ^^^^^^^^^
65-
7 | const arr = [x, y];
66-
8 | const obj = {x, y};
67-
9 | const [a, b] = arr;
68-
69-
Todo: Important source location missing in generated code
70-
71-
Source location for VariableDeclarator is missing in the generated output. This can cause coverage instrumentation to fail to track this code properly, resulting in inaccurate coverage reports..
72-
73-
error.todo-missing-source-locations.ts:7:8
74-
5 | const x = prop1 + prop2;
75-
6 | const y = x * 2;
76-
> 7 | const arr = [x, y];
77-
| ^^^^^^^^^^^^
78-
8 | const obj = {x, y};
79-
9 | const [a, b] = arr;
80-
10 | const {x: c, y: d} = obj;
81-
82-
Todo: Important source location missing in generated code
83-
84-
Source location for VariableDeclarator is missing in the generated output. This can cause coverage instrumentation to fail to track this code properly, resulting in inaccurate coverage reports..
85-
86-
error.todo-missing-source-locations.ts:8:8
87-
6 | const y = x * 2;
88-
7 | const arr = [x, y];
89-
> 8 | const obj = {x, y};
90-
| ^^^^^^^^^^^^
91-
9 | const [a, b] = arr;
92-
10 | const {x: c, y: d} = obj;
93-
11 |
94-
95-
Todo: Important source location missing in generated code
96-
97-
Source location for VariableDeclarator is missing in the generated output. This can cause coverage instrumentation to fail to track this code properly, resulting in inaccurate coverage reports..
98-
99-
error.todo-missing-source-locations.ts:9:8
100-
7 | const arr = [x, y];
101-
8 | const obj = {x, y};
102-
> 9 | const [a, b] = arr;
103-
| ^^^^^^^^^^^^
104-
10 | const {x: c, y: d} = obj;
105-
11 |
106-
12 | useEffect(() => {
107-
108-
Todo: Important source location missing in generated code
109-
110-
Source location for VariableDeclarator is missing in the generated output. This can cause coverage instrumentation to fail to track this code properly, resulting in inaccurate coverage reports..
111-
112-
error.todo-missing-source-locations.ts:10:8
113-
8 | const obj = {x, y};
114-
9 | const [a, b] = arr;
115-
> 10 | const {x: c, y: d} = obj;
116-
| ^^^^^^^^^^^^^^^^^^
117-
11 |
118-
12 | useEffect(() => {
119-
13 | if (a > 10) {
41+
Found 6 errors:
12042
12143
Todo: Important source location missing in generated code
12244
@@ -154,23 +76,6 @@ error.todo-missing-source-locations.ts:14:6
15476
15577
Todo: Important source location missing in generated code
15678
157-
Source location for VariableDeclarator is missing in the generated output. This can cause coverage instrumentation to fail to track this code properly, resulting in inaccurate coverage reports..
158-
159-
error.todo-missing-source-locations.ts:18:8
160-
16 | }, [a]);
161-
17 |
162-
> 18 | const foo = useCallback(() => {
163-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
164-
> 19 | return a + b;
165-
| ^^^^^^^^^^^^^^^^^
166-
> 20 | }, [a, b]);
167-
| ^^^^^^^^^^^^^
168-
21 |
169-
22 | function bar() {
170-
23 | return (c + d) * 2;
171-
172-
Todo: Important source location missing in generated code
173-
17479
Source location for ReturnStatement is missing in the generated output. This can cause coverage instrumentation to fail to track this code properly, resulting in inaccurate coverage reports..
17580
17681
error.todo-missing-source-locations.ts:19:4

0 commit comments

Comments
 (0)