Skip to content

Commit 02547fe

Browse files
authored
Merge pull request #10766 from Microsoft/fix10741_emitCommentOnlyOnce
Fix 10741: Only emit comment only once in module declaration with identifier pat…
2 parents 599d2b0 + b193426 commit 02547fe

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
@@ -2861,7 +2861,6 @@ namespace ts {
28612861
const moduleBlock = <ModuleBlock>getInnerMostModuleDeclarationFromDottedModule(node).body;
28622862
statementsLocation = moveRangePos(moduleBlock.statements, -1);
28632863
}
2864-
28652864
addRange(statements, endLexicalEnvironment());
28662865

28672866
currentNamespaceContainerName = savedCurrentNamespaceContainerName;
@@ -2874,6 +2873,30 @@ namespace ts {
28742873
/*location*/ blockLocation,
28752874
/*multiLine*/ true
28762875
);
2876+
2877+
// namespace hello.hi.world {
2878+
// function foo() {}
2879+
//
2880+
// // TODO, blah
2881+
// }
2882+
//
2883+
// should be emitted as
2884+
//
2885+
// var hello;
2886+
// (function (hello) {
2887+
// var hi;
2888+
// (function (hi) {
2889+
// var world;
2890+
// (function (world) {
2891+
// function foo() { }
2892+
// // TODO, blah
2893+
// })(world = hi.world || (hi.world = {}));
2894+
// })(hi = hello.hi || (hello.hi = {}));
2895+
// })(hello || (hello = {}));
2896+
// We only want to emit comment on the namespace which contains block body itself, not the containing namespaces.
2897+
if (body.kind !== SyntaxKind.ModuleBlock) {
2898+
setNodeEmitFlags(block, block.emitFlags | NodeEmitFlags.NoComments);
2899+
}
28772900
return block;
28782901
}
28792902

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)