diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index b436c254ff0..ea0bc6d992e 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -2223,24 +2223,6 @@ namespace ts { || (isES6ExportedDeclaration(node) && isFirstDeclarationOfKind(node, node.kind)); } - /** - * Adds a leading VariableStatement for an enum or module declaration. - */ - function addVarForEnumDeclaration(statements: Statement[], node: EnumDeclaration) { - // Emit a variable statement for the enum. - statements.push( - createVariableStatement( - isES6ExportedDeclaration(node) - ? visitNodes(node.modifiers, visitor, isModifier) - : undefined, - [createVariableDeclaration( - getDeclarationName(node) - )], - /*location*/ node - ) - ); - } - /** * Adds a trailing VariableStatement for an enum or module declaration. */ @@ -2271,7 +2253,7 @@ namespace ts { const statements: Statement[] = []; if (shouldEmitVarForEnumDeclaration(node)) { - addVarForEnumDeclaration(statements, node); + addVarForEnumOrModuleDeclaration(statements, node); } const localName = getGeneratedNameForNode(node); @@ -2408,9 +2390,9 @@ namespace ts { } /** - * Adds a leading VariableStatement for a module declaration. + * Adds a leading VariableStatement for a enum or module declaration. */ - function addVarForModuleDeclaration(statements: Statement[], node: ModuleDeclaration) { + function addVarForEnumOrModuleDeclaration(statements: Statement[], node: ModuleDeclaration | EnumDeclaration) { // Emit a variable statement for the module. statements.push( setOriginalNode( @@ -2421,7 +2403,21 @@ namespace ts { [createVariableDeclaration( getDeclarationName(node) )], - /*location*/ node + // Trailing comments for module declaration should be emitted with function closure instead of variable statement + // So do not set the end position for the variable statement node + // /** Module comment*/ + // module m1 { + // function foo4Export() { + // } + // } // trailing comment module + // Should emit + // /** Module comment*/ + // var m1; + // (function (m1) { + // function foo4Export() { + // } + // })(m1 || (m1 = {})); // trailing comment module + /*location*/ { pos: node.pos, end: -1 } ), node ) @@ -2446,7 +2442,7 @@ namespace ts { const statements: Statement[] = []; if (shouldEmitVarForModuleDeclaration(node)) { - addVarForModuleDeclaration(statements, node); + addVarForEnumOrModuleDeclaration(statements, node); } const localName = getGeneratedNameForNode(node);