Skip to content

Commit a49ce91

Browse files
author
Kanchalai Tanglertsampan
committed
Only emit comment only once in module declaration with identifier path name
1 parent 874846a commit a49ce91

File tree

5 files changed

+72
-1
lines changed

5 files changed

+72
-1
lines changed

src/compiler/transformers/ts.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2859,7 +2859,6 @@ namespace ts {
28592859
const moduleBlock = <ModuleBlock>getInnerMostModuleDeclarationFromDottedModule(node).body;
28602860
statementsLocation = moveRangePos(moduleBlock.statements, -1);
28612861
}
2862-
28632862
addRange(statements, endLexicalEnvironment());
28642863

28652864
currentNamespaceContainerName = savedCurrentNamespaceContainerName;
@@ -2872,6 +2871,30 @@ namespace ts {
28722871
/*location*/ blockLocation,
28732872
/*multiLine*/ true
28742873
);
2874+
2875+
// namespace hello.hi.world {
2876+
// function foo() {}
2877+
//
2878+
// // TODO, blah
2879+
// }
2880+
//
2881+
// should be emitted as
2882+
//
2883+
// var hello;
2884+
// (function (hello) {
2885+
// var hi;
2886+
// (function (hi) {
2887+
// var world;
2888+
// (function (world) {
2889+
// function foo() { }
2890+
// // TODO, blah
2891+
// })(world = hi.world || (hi.world = {}));
2892+
// })(hi = hello.hi || (hello.hi = {}));
2893+
// })(hello || (hello = {}));
2894+
// so if the block is a transformed module declaration, turn off the comment emit
2895+
if (body.kind !== SyntaxKind.ModuleBlock) {
2896+
setNodeEmitFlags(block, block.emitFlags | NodeEmitFlags.NoComments);
2897+
}
28752898
return block;
28762899
}
28772900

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//// [commentInNamespaceDeclarationWithIdentifierPathName.ts]
2+
namespace hello.hi.world
3+
{
4+
function foo() {}
5+
6+
// TODO, blah
7+
}
8+
9+
//// [commentInNamespaceDeclarationWithIdentifierPathName.js]
10+
var hello;
11+
(function (hello) {
12+
var hi;
13+
(function (hi) {
14+
var world;
15+
(function (world) {
16+
function foo() { }
17+
// TODO, blah
18+
})(world = hi.world || (hi.world = {}));
19+
})(hi = hello.hi || (hello.hi = {}));
20+
})(hello || (hello = {}));
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=== tests/cases/compiler/commentInNamespaceDeclarationWithIdentifierPathName.ts ===
2+
namespace hello.hi.world
3+
>hello : Symbol(hello, Decl(commentInNamespaceDeclarationWithIdentifierPathName.ts, 0, 0))
4+
>hi : Symbol(hi, Decl(commentInNamespaceDeclarationWithIdentifierPathName.ts, 0, 16))
5+
>world : Symbol(world, Decl(commentInNamespaceDeclarationWithIdentifierPathName.ts, 0, 19))
6+
{
7+
function foo() {}
8+
>foo : Symbol(foo, Decl(commentInNamespaceDeclarationWithIdentifierPathName.ts, 1, 1))
9+
10+
// TODO, blah
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=== tests/cases/compiler/commentInNamespaceDeclarationWithIdentifierPathName.ts ===
2+
namespace hello.hi.world
3+
>hello : typeof hello
4+
>hi : typeof hi
5+
>world : typeof world
6+
{
7+
function foo() {}
8+
>foo : () => void
9+
10+
// TODO, blah
11+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace hello.hi.world
2+
{
3+
function foo() {}
4+
5+
// TODO, blah
6+
}

0 commit comments

Comments
 (0)