From 7b07d3ce27e75c165b60b4dc06a84352941f793c Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Wed, 13 Apr 2016 11:59:29 -0700 Subject: [PATCH 1/3] Fix the trailing comment emit for module declaration Fixes #8045 Fixes: - tests\cases\compiler\augmentedTypesClass3.ts - tests\cases\compiler\augmentedTypesModules.ts - tests\cases\compiler\commentsModules.ts --- src/compiler/comments.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/compiler/comments.ts b/src/compiler/comments.ts index 1baf1847f04..4ecf2dd6935 100644 --- a/src/compiler/comments.ts +++ b/src/compiler/comments.ts @@ -117,6 +117,25 @@ namespace ts { return undefined; } + const node = range as Node; + if (node.kind === SyntaxKind.VariableStatement && + node.original && + node.original.kind === SyntaxKind.ModuleDeclaration) { + // Trailing comments for module declaration should be emitted with function closure instead of variable statement + // /** Module comment*/ + // module m1 { + // function foo4Export() { + // } + // } // trailing comment module + // Should emit + // /** Module comment*/ + // var m1; + // (function (m1) { + // function foo4Export() { + // } + // })(m1 || (m1 = {})); // trailing comment module + return undefined; + } return getTrailingCommentsOfPosition(range.end); } From 27adb8c363c6a172a5a489929251f569666bd2d2 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Wed, 13 Apr 2016 12:05:21 -0700 Subject: [PATCH 2/3] Fix the trailing comments for enum declaration Fixes #8045 Tests fixed: - tests\cases\compiler\augmentedTypesClass.ts - tests\cases\compiler\augmentedTypesEnum.ts - tests\cases\compiler\augmentedTypesEnum2.ts - tests\cases\compiler\augmentedTypesFunction.ts - tests\cases\compiler\augmentedTypesVar.ts - tests\cases\compiler\commentsEnums.ts --- src/compiler/comments.ts | 2 +- src/compiler/transformers/ts.ts | 26 ++++---------------------- 2 files changed, 5 insertions(+), 23 deletions(-) diff --git a/src/compiler/comments.ts b/src/compiler/comments.ts index 4ecf2dd6935..f5f8549dd28 100644 --- a/src/compiler/comments.ts +++ b/src/compiler/comments.ts @@ -120,7 +120,7 @@ namespace ts { const node = range as Node; if (node.kind === SyntaxKind.VariableStatement && node.original && - node.original.kind === SyntaxKind.ModuleDeclaration) { + (node.original.kind === SyntaxKind.ModuleDeclaration || node.original.kind === SyntaxKind.EnumDeclaration)) { // Trailing comments for module declaration should be emitted with function closure instead of variable statement // /** Module comment*/ // module m1 { diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index 0b8c11a66e2..8a915484a79 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -2213,24 +2213,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. */ @@ -2261,7 +2243,7 @@ namespace ts { const statements: Statement[] = []; if (shouldEmitVarForEnumDeclaration(node)) { - addVarForEnumDeclaration(statements, node); + addVarForEnumOrModuleDeclaration(statements, node); } const localName = getGeneratedNameForNode(node); @@ -2395,9 +2377,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( @@ -2433,7 +2415,7 @@ namespace ts { const statements: Statement[] = []; if (shouldEmitVarForModuleDeclaration(node)) { - addVarForModuleDeclaration(statements, node); + addVarForEnumOrModuleDeclaration(statements, node); } const localName = getGeneratedNameForNode(node); From 2e47f22fcc75cc0989dfb5f44ec372896bb38447 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Wed, 13 Apr 2016 14:14:00 -0700 Subject: [PATCH 3/3] Set the end position of variable statement as -1 so the trailing comments are not emitted --- src/compiler/comments.ts | 19 ------------------- src/compiler/transformers/ts.ts | 16 +++++++++++++++- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/compiler/comments.ts b/src/compiler/comments.ts index f5f8549dd28..1baf1847f04 100644 --- a/src/compiler/comments.ts +++ b/src/compiler/comments.ts @@ -117,25 +117,6 @@ namespace ts { return undefined; } - const node = range as Node; - if (node.kind === SyntaxKind.VariableStatement && - node.original && - (node.original.kind === SyntaxKind.ModuleDeclaration || node.original.kind === SyntaxKind.EnumDeclaration)) { - // Trailing comments for module declaration should be emitted with function closure instead of variable statement - // /** Module comment*/ - // module m1 { - // function foo4Export() { - // } - // } // trailing comment module - // Should emit - // /** Module comment*/ - // var m1; - // (function (m1) { - // function foo4Export() { - // } - // })(m1 || (m1 = {})); // trailing comment module - return undefined; - } return getTrailingCommentsOfPosition(range.end); } diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index 8a915484a79..539061dd481 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -2390,7 +2390,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 )