diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index d46d9d4721..1170ed7325 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -4007,7 +4007,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi else { let nodeForSourceMap: Node; // If binding element is part of binding pattern with single element, use binding pattern - if (target.kind === SyntaxKind.BindingElement && (target.parent).elements.length === 1) { + if (target.kind === SyntaxKind.BindingElement && hasSingleBindingElement(target.parent)) { nodeForSourceMap = (target.parent.parent.kind === SyntaxKind.VariableDeclaration || target.parent.parent.kind === SyntaxKind.Parameter) ? target.parent.parent : // Set sourcemap as whole variable declaration target.parent; // Only binding Pattern @@ -4018,6 +4018,26 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi emitAssignment(target.name, value, /*shouldEmitCommaBeforeAssignment*/ emitCount > 0, nodeForSourceMap); emitCount++; } + + function hasSingleBindingElement(pattern: BindingPattern) { + if (pattern.kind === SyntaxKind.ObjectBindingPattern) { + return pattern.elements.length === 1; + } + + let hasFoundEmittingElement = false; + for (const element of pattern.elements) { + if (element.kind !== SyntaxKind.OmittedExpression) { + if (hasFoundEmittingElement) { + // More than one elements are going to be emitted + return false; + } + hasFoundEmittingElement = true; + } + } + + // If we found exactly one emitting element + return hasFoundEmittingElement; + } } } diff --git a/tests/baselines/reference/sourceMapValidationDestructuringVariableStatementArrayBindingPattern.js.map b/tests/baselines/reference/sourceMapValidationDestructuringVariableStatementArrayBindingPattern.js.map index e391282221..ff3efc9707 100644 --- a/tests/baselines/reference/sourceMapValidationDestructuringVariableStatementArrayBindingPattern.js.map +++ b/tests/baselines/reference/sourceMapValidationDestructuringVariableStatementArrayBindingPattern.js.map @@ -1,2 +1,2 @@ //// [sourceMapValidationDestructuringVariableStatementArrayBindingPattern.js.map] -{"version":3,"file":"sourceMapValidationDestructuringVariableStatementArrayBindingPattern.js","sourceRoot":"","sources":["sourceMapValidationDestructuringVariableStatementArrayBindingPattern.ts"],"names":[],"mappings":"AAIA,IAAI,MAAM,GAAU,CAAC,CAAC,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC;AAC3C,IAAI,MAAM,GAAU,CAAC,CAAC,EAAE,SAAS,EAAE,UAAU,CAAC,CAAC;AAG/C,IAAO,iBAAK,CAAW;AACvB,IAAI,mBAAkB,CAAC;AACvB,IAAK,oBAAQ,EAAE,kBAAM,EAAE,mBAAO,CAAW;AAEzC,IAAI,6CAA4C,CAAC;AACjD,IAA+B,oCAA+B,EAAzD,eAAO,EAAE,aAAK,EAAE,cAAM,CAAoC;AAE/D,IAAK,oBAAQ,EAAE,4BAAa,CAAW;AAEvC,EAAE,CAAC,CAAC,KAAK,IAAI,MAAM,CAAC,CAAC,CAAC;IAClB,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;AACzB,CAAC"} \ No newline at end of file +{"version":3,"file":"sourceMapValidationDestructuringVariableStatementArrayBindingPattern.js","sourceRoot":"","sources":["sourceMapValidationDestructuringVariableStatementArrayBindingPattern.ts"],"names":[],"mappings":"AAIA,IAAI,MAAM,GAAU,CAAC,CAAC,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC;AAC3C,IAAI,MAAM,GAAU,CAAC,CAAC,EAAE,SAAS,EAAE,UAAU,CAAC,CAAC;AAG/C,IAAI,iBAAkB,CAAC;AACvB,IAAI,mBAAkB,CAAC;AACvB,IAAK,oBAAQ,EAAE,kBAAM,EAAE,mBAAO,CAAW;AAEzC,IAAI,6CAA4C,CAAC;AACjD,IAA+B,oCAA+B,EAAzD,eAAO,EAAE,aAAK,EAAE,cAAM,CAAoC;AAE/D,IAAK,oBAAQ,EAAE,4BAAa,CAAW;AAEvC,EAAE,CAAC,CAAC,KAAK,IAAI,MAAM,CAAC,CAAC,CAAC;IAClB,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;AACzB,CAAC"} \ No newline at end of file diff --git a/tests/baselines/reference/sourceMapValidationDestructuringVariableStatementArrayBindingPattern.sourcemap.txt b/tests/baselines/reference/sourceMapValidationDestructuringVariableStatementArrayBindingPattern.sourcemap.txt index 519fcb1c33..557043581c 100644 --- a/tests/baselines/reference/sourceMapValidationDestructuringVariableStatementArrayBindingPattern.sourcemap.txt +++ b/tests/baselines/reference/sourceMapValidationDestructuringVariableStatementArrayBindingPattern.sourcemap.txt @@ -100,12 +100,12 @@ sourceFile:sourceMapValidationDestructuringVariableStatementArrayBindingPattern. > > > -2 >let [, -3 > nameA -4 > ] = robotA; +2 >let +3 > [, nameA] = robotA +4 > ; 1 >Emitted(3, 1) Source(9, 1) + SourceIndex(0) -2 >Emitted(3, 5) Source(9, 8) + SourceIndex(0) -3 >Emitted(3, 22) Source(9, 13) + SourceIndex(0) +2 >Emitted(3, 5) Source(9, 5) + SourceIndex(0) +3 >Emitted(3, 22) Source(9, 23) + SourceIndex(0) 4 >Emitted(3, 23) Source(9, 24) + SourceIndex(0) --- >>>var numberB = robotB[0];