From 647a3efa4f0af0de778a3ac30aa87023b41edae4 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Tue, 10 May 2016 17:43:13 -0700 Subject: [PATCH] Fixes source map alignment for enums --- src/compiler/transformers/ts.ts | 56 +++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index c1d119b2487..6b56c38778d 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -2376,8 +2376,9 @@ namespace ts { addVarForEnumOrModuleDeclaration(statements, node); } - const localName = getGeneratedNameForNode(node); - const name = getDeclarationNameExpression(node); + const innerName = getNamespaceContainerName(node); + const paramName = getNamespaceParameterName(node); + const exportName = getExportName(node); // (function (x) { // x[x["y"] = 0] = "y"; @@ -2389,22 +2390,22 @@ namespace ts { createStatement( createCall( createFunctionExpression( - /*asteriskToken*/ undefined, - /*name*/ undefined, - [createParameter(localName)], - transformEnumBody(node, localName) + /*asteriskToken*/ undefined, + /*name*/ undefined, + [createParameter(paramName)], + transformEnumBody(node, innerName) ), [createLogicalOr( - name, + exportName, createAssignment( - name, + exportName, createObjectLiteral() ) )] ), - /*location*/ node + /*location*/ node ), - /*original*/ node + /*original*/ node ), NodeEmitFlags.AdviseOnEmitNode ) @@ -2432,7 +2433,11 @@ namespace ts { addNodes(statements, endLexicalEnvironment()); currentNamespaceContainerName = savedCurrentNamespaceLocalName; - return createBlock(statements, /*location*/ undefined, /*multiLine*/ true); + return createBlock( + createNodeArray(statements, /*location*/ node.members), + /*location*/ undefined, + /*multiLine*/ true + ); } /** @@ -2457,9 +2462,10 @@ namespace ts { transformEnumMemberDeclarationValue(member) ) ), - name + name, + /*location*/ member ), - member + /*location*/ member ); } @@ -2518,12 +2524,22 @@ namespace ts { isES6ExportedDeclaration(node) ? visitNodes(node.modifiers, visitor, isModifier) : undefined, - [createVariableDeclaration( - getDeclarationName(node, /*allowComments*/ false, /*allowSourceMaps*/ true) - )], - /*location*/ node + [ + createVariableDeclaration( + getDeclarationName(node, /*allowComments*/ false, /*allowSourceMaps*/ true), + /*initializer*/ undefined + ) + ] ); + // Adjust the source map emit to match the old emitter. + if (node.kind === SyntaxKind.EnumDeclaration) { + setSourceMapRange(statement.declarationList, node); + } + else { + setSourceMapRange(statement, node); + } + // Trailing comments for module declaration should be emitted after the function closure // instead of the variable statement: // @@ -2542,8 +2558,8 @@ namespace ts { // } // })(m1 || (m1 = {})); // trailing comment module // + setCommentRange(statement, node); setNodeEmitFlags(statement, NodeEmitFlags.NoTrailingComments); - setOriginalNode(statement, /*original*/ node); statements.push(statement); } @@ -2833,8 +2849,8 @@ namespace ts { * Gets the declaration name used inside of a namespace or enum. */ function getNamespaceParameterName(node: ModuleDeclaration | EnumDeclaration) { - const name = getGeneratedNameForNode(node, node.name); - setNodeEmitFlags(name, NodeEmitFlags.NoComments); + const name = getGeneratedNameForNode(node); + setSourceMapRange(name, node.name); return name; }