Skip to content

Commit c50a6f7

Browse files
committed
Fix comment emit for namespaces & enums.
Keep emit flags set on the original node on a namespace or enum node. This prevents dropping flags like NoComments, which caused duplicated comment emits. Additionally, TypeScript would emit synthetic comments twice, once for the variable declaration, once for the module statement. This explicitly clears away synthetic comments on namespaces and enums if their synthetic comments have already been emitted on the corresponding variable statement.
1 parent a7224ec commit c50a6f7

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

src/compiler/transformers/ts.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2633,7 +2633,8 @@ namespace ts {
26332633
// If needed, we should emit a variable declaration for the enum. If we emit
26342634
// a leading variable declaration, we should not emit leading comments for the
26352635
// enum body.
2636-
if (addVarForEnumOrModuleDeclaration(statements, node)) {
2636+
const varAdded = addVarForEnumOrModuleDeclaration(statements, node);
2637+
if (varAdded) {
26372638
// We should still emit the comments if we are emitting a system module.
26382639
if (moduleKind !== ModuleKind.System || currentLexicalScope !== currentSourceFile) {
26392640
emitFlags |= EmitFlags.NoLeadingComments;
@@ -2691,8 +2692,13 @@ namespace ts {
26912692
);
26922693

26932694
setOriginalNode(enumStatement, node);
2695+
if (varAdded) {
2696+
// If a variable was added, synthetic comments are mitted on it, not on the moduleStatement.
2697+
setSyntheticLeadingComments(enumStatement, undefined);
2698+
setSyntheticTrailingComments(enumStatement, undefined);
2699+
}
26942700
setTextRange(enumStatement, node);
2695-
setEmitFlags(enumStatement, emitFlags);
2701+
addEmitFlags(enumStatement, emitFlags);
26962702
statements.push(enumStatement);
26972703

26982704
// Add a DeclarationMarker for the enum to preserve trailing comments and mark
@@ -2882,7 +2888,7 @@ namespace ts {
28822888
// })(m1 || (m1 = {})); // trailing comment module
28832889
//
28842890
setCommentRange(statement, node);
2885-
setEmitFlags(statement, EmitFlags.NoTrailingComments | EmitFlags.HasEndOfDeclarationMarker);
2891+
addEmitFlags(statement, EmitFlags.NoTrailingComments | EmitFlags.HasEndOfDeclarationMarker);
28862892
statements.push(statement);
28872893
return true;
28882894
}
@@ -2922,7 +2928,8 @@ namespace ts {
29222928
// If needed, we should emit a variable declaration for the module. If we emit
29232929
// a leading variable declaration, we should not emit leading comments for the
29242930
// module body.
2925-
if (addVarForEnumOrModuleDeclaration(statements, node)) {
2931+
const varAdded = addVarForEnumOrModuleDeclaration(statements, node);
2932+
if (varAdded) {
29262933
// We should still emit the comments if we are emitting a system module.
29272934
if (moduleKind !== ModuleKind.System || currentLexicalScope !== currentSourceFile) {
29282935
emitFlags |= EmitFlags.NoLeadingComments;
@@ -2979,8 +2986,13 @@ namespace ts {
29792986
);
29802987

29812988
setOriginalNode(moduleStatement, node);
2989+
if (varAdded) {
2990+
// If a variable was added, synthetic comments are mitted on it, not on the moduleStatement.
2991+
setSyntheticLeadingComments(moduleStatement, undefined);
2992+
setSyntheticTrailingComments(moduleStatement, undefined);
2993+
}
29822994
setTextRange(moduleStatement, node);
2983-
setEmitFlags(moduleStatement, emitFlags);
2995+
addEmitFlags(moduleStatement, emitFlags);
29842996
statements.push(moduleStatement);
29852997

29862998
// Add a DeclarationMarker for the namespace to preserve trailing comments and mark

src/testRunner/unittests/transform.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,27 @@ class Clazz {
357357
}
358358
}).outputText;
359359
});
360+
361+
testBaseline("transformAddCommentToNamespace", () => {
362+
return transpileModule(`
363+
// namespace comment.
364+
namespace Foo {
365+
export const x = 1;
366+
}
367+
// another comment.
368+
namespace Foo {
369+
export const y = 1;
370+
}
371+
`, {
372+
transformers: {
373+
before: [addSyntheticComment(n => isModuleDeclaration(n))],
374+
},
375+
compilerOptions: {
376+
target: ScriptTarget.ES2015,
377+
newLine: NewLineKind.CarriageReturnLineFeed,
378+
}
379+
}).outputText;
380+
});
360381
});
361382
}
362383

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*comment*/
2+
var Foo;
3+
(function (Foo) {
4+
Foo.x = 1;
5+
})(Foo || (Foo = {}));
6+
/*comment*/
7+
(function (Foo) {
8+
Foo.y = 1;
9+
})(Foo || (Foo = {}));

0 commit comments

Comments
 (0)