diff --git a/src/compiler/comments.ts b/src/compiler/comments.ts index 8e17bf2d91b..dcb471dcfcc 100644 --- a/src/compiler/comments.ts +++ b/src/compiler/comments.ts @@ -9,7 +9,7 @@ namespace ts { emitNodeWithComments(hint: EmitHint, node: Node, emitCallback: (hint: EmitHint, node: Node) => void): void; emitBodyWithDetachedComments(node: Node, detachedRange: TextRange, emitCallback: (node: Node) => void): void; emitTrailingCommentsOfPosition(pos: number): void; - emitLeadingComments(pos: number, isEmittedNode: boolean): void; + emitLeadingCommentsOfPosition(pos: number): void; } export function createCommentWriter(printerOptions: PrinterOptions, emitPos: ((pos: number) => void) | undefined): CommentWriter { @@ -33,7 +33,7 @@ namespace ts { emitNodeWithComments, emitBodyWithDetachedComments, emitTrailingCommentsOfPosition, - emitLeadingComments, + emitLeadingCommentsOfPosition, }; function emitNodeWithComments(hint: EmitHint, node: Node, emitCallback: (hint: EmitHint, node: Node) => void) { @@ -169,10 +169,6 @@ namespace ts { } function emitLeadingComments(pos: number, isEmittedNode: boolean) { - if (disabled) { - return; - } - hasWrittenComment = false; if (isEmittedNode) { @@ -216,6 +212,14 @@ namespace ts { } } + function emitLeadingCommentsOfPosition(pos: number) { + if (disabled || pos === -1) { + return; + } + + emitLeadingComments(pos, /*isEmittedNode*/ true); + } + function emitTrailingComments(pos: number) { forEachTrailingCommentToEmit(pos, emitTrailingComment); } diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 7adac106e8c..fcb3fb826c9 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -1,4 +1,4 @@ -/// +/// /// /// /// @@ -211,7 +211,7 @@ namespace ts { emitNodeWithComments, emitBodyWithDetachedComments, emitTrailingCommentsOfPosition, - emitLeadingComments, + emitLeadingCommentsOfPosition, } = comments; let currentSourceFile: SourceFile; @@ -1348,7 +1348,7 @@ namespace ts { writeToken(SyntaxKind.OpenBraceToken, node.pos, /*contextNode*/ node); emitBlockStatements(node); // We have to call emitLeadingComments explicitly here because otherwise leading comments of the close brace token will not be emitted - emitLeadingComments(node.statements.end, /*isEmittedNode*/true); + emitLeadingCommentsOfPosition(node.statements.end); writeToken(SyntaxKind.CloseBraceToken, node.statements.end, /*contextNode*/ node); } } @@ -2235,10 +2235,10 @@ namespace ts { // function commentedParameters( // /* Parameter a */ // a - // /* End of parameter a */ -> this comment doesn't consider to be trailing comment of parameter "a" due to newline + // /* End of parameter a */ -> this comment isn't considered to be trailing comment of parameter "a" due to newline // , - if (emitLeadingComments && delimiter && previousSibling.end !== parentNode.end) { - emitLeadingComments(previousSibling.end, /*isEmittedNode*/ previousSibling.kind !== SyntaxKind.NotEmittedStatement); + if (emitLeadingCommentsOfPosition && delimiter && previousSibling.end !== parentNode.end) { + emitLeadingCommentsOfPosition(previousSibling.end); } write(delimiter); @@ -2294,10 +2294,7 @@ namespace ts { // /* end of element 2 */ // ]; if (previousSibling && delimiter && previousSibling.end !== parentNode.end) { - emitLeadingComments(previousSibling.end, /*isEmittedNode*/ previousSibling.kind !== SyntaxKind.NotEmittedStatement); - if (hasTrailingComma) { - emitLeadingComments(previousSibling.end, /*isEmittedNode*/ previousSibling.kind !== SyntaxKind.NotEmittedStatement); - } + emitLeadingCommentsOfPosition(previousSibling.end); } // Decrease the indent, if requested.