Skip to content

Commit 8af2160

Browse files
committed
Make nested object literal destructuring pattern better
1 parent b497cbc commit 8af2160

5 files changed

Lines changed: 85 additions & 55 deletions

src/compiler/emitter.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,16 +1979,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
19791979
}
19801980

19811981
function createPropertyAccessExpression(expression: Expression, name: Identifier): PropertyAccessExpression {
1982-
const result = <PropertyAccessExpression>createSynthesizedNode(SyntaxKind.PropertyAccessExpression);
1982+
const result = <PropertyAccessExpression>createSourceMappedSynthesizedNode(SyntaxKind.PropertyAccessExpression, name);
19831983
result.expression = parenthesizeForAccess(expression);
19841984
result.dotToken = createSynthesizedNode(SyntaxKind.DotToken);
19851985
result.name = name;
1986-
19871986
return result;
19881987
}
19891988

19901989
function createElementAccessExpression(expression: Expression, argumentExpression: Expression): ElementAccessExpression {
1991-
const result = <ElementAccessExpression>createSynthesizedNode(SyntaxKind.ElementAccessExpression);
1990+
const result = <ElementAccessExpression>createSourceMappedSynthesizedNode(SyntaxKind.ElementAccessExpression, argumentExpression);
19921991
result.expression = parenthesizeForAccess(expression);
19931992
result.argumentExpression = argumentExpression;
19941993

@@ -2016,7 +2015,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
20162015

20172016
return <LeftHandSideExpression>expr;
20182017
}
2019-
const node = <ParenthesizedExpression>createSynthesizedNode(SyntaxKind.ParenthesizedExpression);
2018+
const node = <ParenthesizedExpression>createSourceMappedSynthesizedNode(SyntaxKind.ParenthesizedExpression, expr);
20202019
node.expression = expr;
20212020
return node;
20222021
}
@@ -3862,7 +3861,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
38623861
else {
38633862
// We create a synthetic copy of the identifier in order to avoid the rewriting that might
38643863
// otherwise occur when the identifier is emitted.
3865-
index = <Identifier | LiteralExpression>createSynthesizedNode(propName.kind);
3864+
index = <Identifier | LiteralExpression>createSourceMappedSynthesizedNode(propName.kind, propName);
38663865
(<Identifier | LiteralExpression>index).text = (<Identifier | LiteralExpression>propName).text;
38673866
}
38683867

src/compiler/sourcemap.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,21 @@ namespace ts {
232232
}
233233
}
234234

235+
function getSourceMapRange(range: TextRange) {
236+
while ((range as SynthesizedNode).sourceMapNode) {
237+
range = (range as SynthesizedNode).sourceMapNode;
238+
}
239+
return range;
240+
}
241+
235242
function emitStart(range: TextRange) {
243+
range = getSourceMapRange(range);
236244
const rangeHasDecorators = !!(range as Node).decorators;
237245
emitPos(range.pos !== -1 ? skipTrivia(currentSourceFile.text, rangeHasDecorators ? (range as Node).decorators.end : range.pos) : -1);
238246
}
239247

240248
function emitEnd(range: TextRange, stopOverridingEnd?: boolean) {
249+
range = getSourceMapRange(range);
241250
emitPos(range.end);
242251
stopOverridingSpan = stopOverridingEnd;
243252
}

src/compiler/utilities.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace ts {
1212
leadingCommentRanges?: CommentRange[];
1313
trailingCommentRanges?: CommentRange[];
1414
startsOnNewLine: boolean;
15+
sourceMapNode?: Node;
1516
}
1617

1718
export function getDeclarationOfKind(symbol: Symbol, kind: SyntaxKind): Declaration {
@@ -1626,6 +1627,12 @@ namespace ts {
16261627
return node;
16271628
}
16281629

1630+
export function createSourceMappedSynthesizedNode(kind: SyntaxKind, sourceMapNode: Node, startsOnNewLine?: boolean): Node {
1631+
const synthesizedNode = <SynthesizedNode>createSynthesizedNode(kind, startsOnNewLine);
1632+
synthesizedNode.sourceMapNode = sourceMapNode;
1633+
return synthesizedNode;
1634+
}
1635+
16291636
export function createSynthesizedNodeArray(): NodeArray<any> {
16301637
const array = <NodeArray<any>>[];
16311638
array.pos = -1;

tests/baselines/reference/sourceMapValidationDestructuringVariableStatementNestedObjectBindingPattern.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/baselines/reference/sourceMapValidationDestructuringVariableStatementNestedObjectBindingPattern.sourcemap.txt

Lines changed: 64 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -159,89 +159,104 @@ sourceFile:sourceMapValidationDestructuringVariableStatementNestedObjectBindingP
159159
>>>var _a = robotA.skills, primaryA = _a.primary, secondaryA = _a.secondary;
160160
1 >
161161
2 >^^^^
162-
3 > ^^^^^^^^^^^^^^^^^^^^
163-
4 > ^^^^^^^^^^^^^^^^^^^^^
164-
5 > ^^
165-
6 > ^^^^^^^^^^^^^^^^^^^^^^^^^
166-
7 > ^
167-
8 > ^^^^^^^^^^^^^^^^^^^^^^->
162+
3 > ^^^^^^^^^^^^^^^^^^
163+
4 > ^^
164+
5 > ^^^^^^^^^^^^^^^^^^^^^
165+
6 > ^^
166+
7 > ^^^^^^^^^^^^^^^^^^^^^^^^^
167+
8 > ^
168+
9 > ^^^^^^^^^^^^^^^^^^^^^^->
168169
1 >
169170
>
170171
>
171-
2 >var
172-
3 > { skills: {
173-
4 > primary: primaryA
174-
5 > ,
175-
6 > secondary: secondaryA
176-
7 > } } = robotA;
172+
2 >var {
173+
3 > skills
174+
4 > : {
175+
5 > primary: primaryA
176+
6 > ,
177+
7 > secondary: secondaryA
178+
8 > } } = robotA;
177179
1 >Emitted(3, 1) Source(14, 1) + SourceIndex(0)
178-
2 >Emitted(3, 5) Source(14, 5) + SourceIndex(0)
179-
3 >Emitted(3, 25) Source(14, 17) + SourceIndex(0)
180-
4 >Emitted(3, 46) Source(14, 34) + SourceIndex(0)
181-
5 >Emitted(3, 48) Source(14, 36) + SourceIndex(0)
182-
6 >Emitted(3, 73) Source(14, 57) + SourceIndex(0)
183-
7 >Emitted(3, 74) Source(14, 71) + SourceIndex(0)
180+
2 >Emitted(3, 5) Source(14, 7) + SourceIndex(0)
181+
3 >Emitted(3, 23) Source(14, 13) + SourceIndex(0)
182+
4 >Emitted(3, 25) Source(14, 17) + SourceIndex(0)
183+
5 >Emitted(3, 46) Source(14, 34) + SourceIndex(0)
184+
6 >Emitted(3, 48) Source(14, 36) + SourceIndex(0)
185+
7 >Emitted(3, 73) Source(14, 57) + SourceIndex(0)
186+
8 >Emitted(3, 74) Source(14, 71) + SourceIndex(0)
184187
---
185188
>>>var nameB = robotB.name, _b = robotB.skills, primaryB = _b.primary, secondaryB = _b.secondary;
186189
1->
187190
2 >^^^^
188191
3 > ^^^^^^^^^^^^^^^^^^^
189-
4 > ^^^^^^^^^^^^^^^^^^^^^^
190-
5 > ^^^^^^^^^^^^^^^^^^^^^
191-
6 > ^^
192-
7 > ^^^^^^^^^^^^^^^^^^^^^^^^^
193-
8 > ^
194-
9 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
192+
4 > ^^
193+
5 > ^^^^^^^^^^^^^^^^^^
194+
6 > ^^
195+
7 > ^^^^^^^^^^^^^^^^^^^^^
196+
8 > ^^
197+
9 > ^^^^^^^^^^^^^^^^^^^^^^^^^
198+
10> ^
199+
11> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
195200
1->
196201
>
197202
2 >var {
198203
3 > name: nameB
199-
4 > , skills: {
200-
5 > primary: primaryB
201-
6 > ,
202-
7 > secondary: secondaryB
203-
8 > } } = robotB;
204+
4 > ,
205+
5 > skills
206+
6 > : {
207+
7 > primary: primaryB
208+
8 > ,
209+
9 > secondary: secondaryB
210+
10> } } = robotB;
204211
1->Emitted(4, 1) Source(15, 1) + SourceIndex(0)
205212
2 >Emitted(4, 5) Source(15, 7) + SourceIndex(0)
206213
3 >Emitted(4, 24) Source(15, 18) + SourceIndex(0)
207-
4 >Emitted(4, 46) Source(15, 30) + SourceIndex(0)
208-
5 >Emitted(4, 67) Source(15, 47) + SourceIndex(0)
209-
6 >Emitted(4, 69) Source(15, 49) + SourceIndex(0)
210-
7 >Emitted(4, 94) Source(15, 70) + SourceIndex(0)
211-
8 >Emitted(4, 95) Source(15, 84) + SourceIndex(0)
214+
4 >Emitted(4, 26) Source(15, 20) + SourceIndex(0)
215+
5 >Emitted(4, 44) Source(15, 26) + SourceIndex(0)
216+
6 >Emitted(4, 46) Source(15, 30) + SourceIndex(0)
217+
7 >Emitted(4, 67) Source(15, 47) + SourceIndex(0)
218+
8 >Emitted(4, 69) Source(15, 49) + SourceIndex(0)
219+
9 >Emitted(4, 94) Source(15, 70) + SourceIndex(0)
220+
10>Emitted(4, 95) Source(15, 84) + SourceIndex(0)
212221
---
213222
>>>var _c = { name: "Edger", skills: { primary: "edging", secondary: "branch trimming" } }, nameC = _c.name, _d = _c.skills, primaryB = _d.primary, secondaryB = _d.secondary;
214223
1->
215224
2 >^^^^
216225
3 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
217226
4 > ^^
218227
5 > ^^^^^^^^^^^^^^^
219-
6 > ^^^^^^^^^^^^^^^^^^
220-
7 > ^^^^^^^^^^^^^^^^^^^^^
221-
8 > ^^
222-
9 > ^^^^^^^^^^^^^^^^^^^^^^^^^
223-
10> ^
228+
6 > ^^
229+
7 > ^^^^^^^^^^^^^^
230+
8 > ^^
231+
9 > ^^^^^^^^^^^^^^^^^^^^^
232+
10> ^^
233+
11> ^^^^^^^^^^^^^^^^^^^^^^^^^
234+
12> ^
224235
1->
225236
>
226237
2 >var { name: nameC, skills: { primary: primaryB, secondary: secondaryB } } =
227238
3 > { name: "Edger", skills: { primary: "edging", secondary: "branch trimming" } }
228239
4 >
229240
5 > name: nameC
230-
6 > , skills: {
231-
7 > primary: primaryB
232-
8 > ,
233-
9 > secondary: secondaryB
234-
10> } } = { name: "Edger", skills: { primary: "edging", secondary: "branch trimming" } };
241+
6 > ,
242+
7 > skills
243+
8 > : {
244+
9 > primary: primaryB
245+
10> ,
246+
11> secondary: secondaryB
247+
12> } } = { name: "Edger", skills: { primary: "edging", secondary: "branch trimming" } };
235248
1->Emitted(5, 1) Source(16, 1) + SourceIndex(0)
236249
2 >Emitted(5, 5) Source(16, 77) + SourceIndex(0)
237250
3 >Emitted(5, 88) Source(16, 155) + SourceIndex(0)
238251
4 >Emitted(5, 90) Source(16, 7) + SourceIndex(0)
239252
5 >Emitted(5, 105) Source(16, 18) + SourceIndex(0)
240-
6 >Emitted(5, 123) Source(16, 30) + SourceIndex(0)
241-
7 >Emitted(5, 144) Source(16, 47) + SourceIndex(0)
242-
8 >Emitted(5, 146) Source(16, 49) + SourceIndex(0)
243-
9 >Emitted(5, 171) Source(16, 70) + SourceIndex(0)
244-
10>Emitted(5, 172) Source(16, 156) + SourceIndex(0)
253+
6 >Emitted(5, 107) Source(16, 20) + SourceIndex(0)
254+
7 >Emitted(5, 121) Source(16, 26) + SourceIndex(0)
255+
8 >Emitted(5, 123) Source(16, 30) + SourceIndex(0)
256+
9 >Emitted(5, 144) Source(16, 47) + SourceIndex(0)
257+
10>Emitted(5, 146) Source(16, 49) + SourceIndex(0)
258+
11>Emitted(5, 171) Source(16, 70) + SourceIndex(0)
259+
12>Emitted(5, 172) Source(16, 156) + SourceIndex(0)
245260
---
246261
>>>if (nameB == nameB) {
247262
1 >

0 commit comments

Comments
 (0)