From 8af2160922aeadd7c2e024438fff88fda078cf05 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Thu, 3 Dec 2015 16:41:58 -0800 Subject: [PATCH] Make nested object literal destructuring pattern better --- src/compiler/emitter.ts | 9 +- src/compiler/sourcemap.ts | 9 ++ src/compiler/utilities.ts | 7 ++ ...StatementNestedObjectBindingPattern.js.map | 2 +- ...ntNestedObjectBindingPattern.sourcemap.txt | 113 ++++++++++-------- 5 files changed, 85 insertions(+), 55 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index ab5e6bfb774..d46d9d47210 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -1979,16 +1979,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi } function createPropertyAccessExpression(expression: Expression, name: Identifier): PropertyAccessExpression { - const result = createSynthesizedNode(SyntaxKind.PropertyAccessExpression); + const result = createSourceMappedSynthesizedNode(SyntaxKind.PropertyAccessExpression, name); result.expression = parenthesizeForAccess(expression); result.dotToken = createSynthesizedNode(SyntaxKind.DotToken); result.name = name; - return result; } function createElementAccessExpression(expression: Expression, argumentExpression: Expression): ElementAccessExpression { - const result = createSynthesizedNode(SyntaxKind.ElementAccessExpression); + const result = createSourceMappedSynthesizedNode(SyntaxKind.ElementAccessExpression, argumentExpression); result.expression = parenthesizeForAccess(expression); result.argumentExpression = argumentExpression; @@ -2016,7 +2015,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi return expr; } - const node = createSynthesizedNode(SyntaxKind.ParenthesizedExpression); + const node = createSourceMappedSynthesizedNode(SyntaxKind.ParenthesizedExpression, expr); node.expression = expr; return node; } @@ -3862,7 +3861,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi else { // We create a synthetic copy of the identifier in order to avoid the rewriting that might // otherwise occur when the identifier is emitted. - index = createSynthesizedNode(propName.kind); + index = createSourceMappedSynthesizedNode(propName.kind, propName); (index).text = (propName).text; } diff --git a/src/compiler/sourcemap.ts b/src/compiler/sourcemap.ts index 982c37e3fe8..680c48cfaa4 100644 --- a/src/compiler/sourcemap.ts +++ b/src/compiler/sourcemap.ts @@ -232,12 +232,21 @@ namespace ts { } } + function getSourceMapRange(range: TextRange) { + while ((range as SynthesizedNode).sourceMapNode) { + range = (range as SynthesizedNode).sourceMapNode; + } + return range; + } + function emitStart(range: TextRange) { + range = getSourceMapRange(range); const rangeHasDecorators = !!(range as Node).decorators; emitPos(range.pos !== -1 ? skipTrivia(currentSourceFile.text, rangeHasDecorators ? (range as Node).decorators.end : range.pos) : -1); } function emitEnd(range: TextRange, stopOverridingEnd?: boolean) { + range = getSourceMapRange(range); emitPos(range.end); stopOverridingSpan = stopOverridingEnd; } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 0f0f50719c1..03cdf26a4ad 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -12,6 +12,7 @@ namespace ts { leadingCommentRanges?: CommentRange[]; trailingCommentRanges?: CommentRange[]; startsOnNewLine: boolean; + sourceMapNode?: Node; } export function getDeclarationOfKind(symbol: Symbol, kind: SyntaxKind): Declaration { @@ -1626,6 +1627,12 @@ namespace ts { return node; } + export function createSourceMappedSynthesizedNode(kind: SyntaxKind, sourceMapNode: Node, startsOnNewLine?: boolean): Node { + const synthesizedNode = createSynthesizedNode(kind, startsOnNewLine); + synthesizedNode.sourceMapNode = sourceMapNode; + return synthesizedNode; + } + export function createSynthesizedNodeArray(): NodeArray { const array = >[]; array.pos = -1; diff --git a/tests/baselines/reference/sourceMapValidationDestructuringVariableStatementNestedObjectBindingPattern.js.map b/tests/baselines/reference/sourceMapValidationDestructuringVariableStatementNestedObjectBindingPattern.js.map index 19d3843a9dc..6723a44d68b 100644 --- a/tests/baselines/reference/sourceMapValidationDestructuringVariableStatementNestedObjectBindingPattern.js.map +++ b/tests/baselines/reference/sourceMapValidationDestructuringVariableStatementNestedObjectBindingPattern.js.map @@ -1,2 +1,2 @@ //// [sourceMapValidationDestructuringVariableStatementNestedObjectBindingPattern.js.map] -{"version":3,"file":"sourceMapValidationDestructuringVariableStatementNestedObjectBindingPattern.js","sourceRoot":"","sources":["sourceMapValidationDestructuringVariableStatementNestedObjectBindingPattern.ts"],"names":[],"mappings":"AAUA,IAAI,MAAM,GAAU,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,EAAE,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,EAAE,EAAE,CAAC;AACxF,IAAI,MAAM,GAAU,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,EAAE,EAAE,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,QAAQ,EAAE,EAAE,CAAC;AAE9F,IAAI,oBAAY,qBAAiB,EAAE,yBAAqB,CAAc;AACtE,IAAM,mBAAW,sBAAY,qBAAiB,EAAE,yBAAqB,CAAc;AACnF,IAA4E,mFAA8E,EAApJ,eAAW,kBAAY,qBAAiB,EAAE,yBAAqB,CAAsF;AAE3J,EAAE,CAAC,CAAC,KAAK,IAAI,KAAK,CAAC,CAAC,CAAC;IACjB,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;AACvB,CAAC;AACD,IAAI,CAAC,CAAC;IACF,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;AACvB,CAAC"} \ No newline at end of file +{"version":3,"file":"sourceMapValidationDestructuringVariableStatementNestedObjectBindingPattern.js","sourceRoot":"","sources":["sourceMapValidationDestructuringVariableStatementNestedObjectBindingPattern.ts"],"names":[],"mappings":"AAUA,IAAI,MAAM,GAAU,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,EAAE,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,EAAE,EAAE,CAAC;AACxF,IAAI,MAAM,GAAU,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,EAAE,EAAE,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,QAAQ,EAAE,EAAE,CAAC;AAE9F,IAAM,kBAAM,EAAI,qBAAiB,EAAE,yBAAqB,CAAc;AACtE,IAAM,mBAAW,EAAE,kBAAM,EAAI,qBAAiB,EAAE,yBAAqB,CAAc;AACnF,IAA4E,mFAA8E,EAApJ,eAAW,EAAE,cAAM,EAAI,qBAAiB,EAAE,yBAAqB,CAAsF;AAE3J,EAAE,CAAC,CAAC,KAAK,IAAI,KAAK,CAAC,CAAC,CAAC;IACjB,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;AACvB,CAAC;AACD,IAAI,CAAC,CAAC;IACF,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;AACvB,CAAC"} \ No newline at end of file diff --git a/tests/baselines/reference/sourceMapValidationDestructuringVariableStatementNestedObjectBindingPattern.sourcemap.txt b/tests/baselines/reference/sourceMapValidationDestructuringVariableStatementNestedObjectBindingPattern.sourcemap.txt index 15045ba9e71..085104bad32 100644 --- a/tests/baselines/reference/sourceMapValidationDestructuringVariableStatementNestedObjectBindingPattern.sourcemap.txt +++ b/tests/baselines/reference/sourceMapValidationDestructuringVariableStatementNestedObjectBindingPattern.sourcemap.txt @@ -159,56 +159,65 @@ sourceFile:sourceMapValidationDestructuringVariableStatementNestedObjectBindingP >>>var _a = robotA.skills, primaryA = _a.primary, secondaryA = _a.secondary; 1 > 2 >^^^^ -3 > ^^^^^^^^^^^^^^^^^^^^ -4 > ^^^^^^^^^^^^^^^^^^^^^ -5 > ^^ -6 > ^^^^^^^^^^^^^^^^^^^^^^^^^ -7 > ^ -8 > ^^^^^^^^^^^^^^^^^^^^^^-> +3 > ^^^^^^^^^^^^^^^^^^ +4 > ^^ +5 > ^^^^^^^^^^^^^^^^^^^^^ +6 > ^^ +7 > ^^^^^^^^^^^^^^^^^^^^^^^^^ +8 > ^ +9 > ^^^^^^^^^^^^^^^^^^^^^^-> 1 > > > -2 >var -3 > { skills: { -4 > primary: primaryA -5 > , -6 > secondary: secondaryA -7 > } } = robotA; +2 >var { +3 > skills +4 > : { +5 > primary: primaryA +6 > , +7 > secondary: secondaryA +8 > } } = robotA; 1 >Emitted(3, 1) Source(14, 1) + SourceIndex(0) -2 >Emitted(3, 5) Source(14, 5) + SourceIndex(0) -3 >Emitted(3, 25) Source(14, 17) + SourceIndex(0) -4 >Emitted(3, 46) Source(14, 34) + SourceIndex(0) -5 >Emitted(3, 48) Source(14, 36) + SourceIndex(0) -6 >Emitted(3, 73) Source(14, 57) + SourceIndex(0) -7 >Emitted(3, 74) Source(14, 71) + SourceIndex(0) +2 >Emitted(3, 5) Source(14, 7) + SourceIndex(0) +3 >Emitted(3, 23) Source(14, 13) + SourceIndex(0) +4 >Emitted(3, 25) Source(14, 17) + SourceIndex(0) +5 >Emitted(3, 46) Source(14, 34) + SourceIndex(0) +6 >Emitted(3, 48) Source(14, 36) + SourceIndex(0) +7 >Emitted(3, 73) Source(14, 57) + SourceIndex(0) +8 >Emitted(3, 74) Source(14, 71) + SourceIndex(0) --- >>>var nameB = robotB.name, _b = robotB.skills, primaryB = _b.primary, secondaryB = _b.secondary; 1-> 2 >^^^^ 3 > ^^^^^^^^^^^^^^^^^^^ -4 > ^^^^^^^^^^^^^^^^^^^^^^ -5 > ^^^^^^^^^^^^^^^^^^^^^ -6 > ^^ -7 > ^^^^^^^^^^^^^^^^^^^^^^^^^ -8 > ^ -9 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-> +4 > ^^ +5 > ^^^^^^^^^^^^^^^^^^ +6 > ^^ +7 > ^^^^^^^^^^^^^^^^^^^^^ +8 > ^^ +9 > ^^^^^^^^^^^^^^^^^^^^^^^^^ +10> ^ +11> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-> 1-> > 2 >var { 3 > name: nameB -4 > , skills: { -5 > primary: primaryB -6 > , -7 > secondary: secondaryB -8 > } } = robotB; +4 > , +5 > skills +6 > : { +7 > primary: primaryB +8 > , +9 > secondary: secondaryB +10> } } = robotB; 1->Emitted(4, 1) Source(15, 1) + SourceIndex(0) 2 >Emitted(4, 5) Source(15, 7) + SourceIndex(0) 3 >Emitted(4, 24) Source(15, 18) + SourceIndex(0) -4 >Emitted(4, 46) Source(15, 30) + SourceIndex(0) -5 >Emitted(4, 67) Source(15, 47) + SourceIndex(0) -6 >Emitted(4, 69) Source(15, 49) + SourceIndex(0) -7 >Emitted(4, 94) Source(15, 70) + SourceIndex(0) -8 >Emitted(4, 95) Source(15, 84) + SourceIndex(0) +4 >Emitted(4, 26) Source(15, 20) + SourceIndex(0) +5 >Emitted(4, 44) Source(15, 26) + SourceIndex(0) +6 >Emitted(4, 46) Source(15, 30) + SourceIndex(0) +7 >Emitted(4, 67) Source(15, 47) + SourceIndex(0) +8 >Emitted(4, 69) Source(15, 49) + SourceIndex(0) +9 >Emitted(4, 94) Source(15, 70) + SourceIndex(0) +10>Emitted(4, 95) Source(15, 84) + SourceIndex(0) --- >>>var _c = { name: "Edger", skills: { primary: "edging", secondary: "branch trimming" } }, nameC = _c.name, _d = _c.skills, primaryB = _d.primary, secondaryB = _d.secondary; 1-> @@ -216,32 +225,38 @@ sourceFile:sourceMapValidationDestructuringVariableStatementNestedObjectBindingP 3 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 4 > ^^ 5 > ^^^^^^^^^^^^^^^ -6 > ^^^^^^^^^^^^^^^^^^ -7 > ^^^^^^^^^^^^^^^^^^^^^ -8 > ^^ -9 > ^^^^^^^^^^^^^^^^^^^^^^^^^ -10> ^ +6 > ^^ +7 > ^^^^^^^^^^^^^^ +8 > ^^ +9 > ^^^^^^^^^^^^^^^^^^^^^ +10> ^^ +11> ^^^^^^^^^^^^^^^^^^^^^^^^^ +12> ^ 1-> > 2 >var { name: nameC, skills: { primary: primaryB, secondary: secondaryB } } = 3 > { name: "Edger", skills: { primary: "edging", secondary: "branch trimming" } } 4 > 5 > name: nameC -6 > , skills: { -7 > primary: primaryB -8 > , -9 > secondary: secondaryB -10> } } = { name: "Edger", skills: { primary: "edging", secondary: "branch trimming" } }; +6 > , +7 > skills +8 > : { +9 > primary: primaryB +10> , +11> secondary: secondaryB +12> } } = { name: "Edger", skills: { primary: "edging", secondary: "branch trimming" } }; 1->Emitted(5, 1) Source(16, 1) + SourceIndex(0) 2 >Emitted(5, 5) Source(16, 77) + SourceIndex(0) 3 >Emitted(5, 88) Source(16, 155) + SourceIndex(0) 4 >Emitted(5, 90) Source(16, 7) + SourceIndex(0) 5 >Emitted(5, 105) Source(16, 18) + SourceIndex(0) -6 >Emitted(5, 123) Source(16, 30) + SourceIndex(0) -7 >Emitted(5, 144) Source(16, 47) + SourceIndex(0) -8 >Emitted(5, 146) Source(16, 49) + SourceIndex(0) -9 >Emitted(5, 171) Source(16, 70) + SourceIndex(0) -10>Emitted(5, 172) Source(16, 156) + SourceIndex(0) +6 >Emitted(5, 107) Source(16, 20) + SourceIndex(0) +7 >Emitted(5, 121) Source(16, 26) + SourceIndex(0) +8 >Emitted(5, 123) Source(16, 30) + SourceIndex(0) +9 >Emitted(5, 144) Source(16, 47) + SourceIndex(0) +10>Emitted(5, 146) Source(16, 49) + SourceIndex(0) +11>Emitted(5, 171) Source(16, 70) + SourceIndex(0) +12>Emitted(5, 172) Source(16, 156) + SourceIndex(0) --- >>>if (nameB == nameB) { 1 >