Skip to content

Commit 188e9df

Browse files
author
Kanchalai Tanglertsampan
committed
Emit missing trailing comment of an element in node list
1 parent 501084a commit 188e9df

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/compiler/comments.ts

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace ts {
99
emitNodeWithComments(hint: EmitHint, node: Node, emitCallback: (hint: EmitHint, node: Node) => void): void;
1010
emitBodyWithDetachedComments(node: Node, detachedRange: TextRange, emitCallback: (node: Node) => void): void;
1111
emitTrailingCommentsOfPosition(pos: number): void;
12+
emitLeadingComments(pos: number, isEmittedNode: boolean): void;
1213
}
1314

1415
export function createCommentWriter(printerOptions: PrinterOptions, emitPos: ((pos: number) => void) | undefined): CommentWriter {
@@ -32,6 +33,7 @@ namespace ts {
3233
emitNodeWithComments,
3334
emitBodyWithDetachedComments,
3435
emitTrailingCommentsOfPosition,
36+
emitLeadingComments,
3537
};
3638

3739
function emitNodeWithComments(hint: EmitHint, node: Node, emitCallback: (hint: EmitHint, node: Node) => void) {
@@ -167,6 +169,10 @@ namespace ts {
167169
}
168170

169171
function emitLeadingComments(pos: number, isEmittedNode: boolean) {
172+
if (disabled) {
173+
return;
174+
}
175+
170176
hasWrittenComment = false;
171177

172178
if (isEmittedNode) {

src/compiler/emitter.ts

+24
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ namespace ts {
211211
emitNodeWithComments,
212212
emitBodyWithDetachedComments,
213213
emitTrailingCommentsOfPosition,
214+
emitLeadingComments,
214215
} = comments;
215216

216217
let currentSourceFile: SourceFile;
@@ -2228,6 +2229,15 @@ namespace ts {
22282229

22292230
// Write the delimiter if this is not the first node.
22302231
if (previousSibling) {
2232+
// i.e
2233+
// function commentedParameters(
2234+
// /* Parameter a */
2235+
// a
2236+
// /* End of parameter a */ -> this comment doesn't consider to be trailing comment of parameter "a" due to newline
2237+
// ,
2238+
if (emitLeadingComments && delimiter && previousSibling.end !== parentNode.end) {
2239+
emitLeadingComments(previousSibling.end, /*isEmittedNode*/ previousSibling.kind !== SyntaxKind.NotEmittedStatement);
2240+
}
22312241
write(delimiter);
22322242

22332243
// Write either a line terminator or whitespace to separate the elements.
@@ -2274,6 +2284,20 @@ namespace ts {
22742284
write(",");
22752285
}
22762286

2287+
2288+
// Emit any trailing comment of the last element in the list
2289+
// i.e
2290+
// var array = [...
2291+
// 2
2292+
// /* end of element 2 */
2293+
// ];
2294+
if (previousSibling && delimiter && previousSibling.end !== parentNode.end) {
2295+
emitLeadingComments(previousSibling.end, /*isEmittedNode*/ previousSibling.kind !== SyntaxKind.NotEmittedStatement);
2296+
if (hasTrailingComma) {
2297+
emitLeadingComments(previousSibling.end, /*isEmittedNode*/ previousSibling.kind !== SyntaxKind.NotEmittedStatement);
2298+
}
2299+
}
2300+
22772301
// Decrease the indent, if requested.
22782302
if (format & ListFormat.Indented) {
22792303
decreaseIndent();

0 commit comments

Comments
 (0)