Merge pull request #13916 from Microsoft/master-fix13602

[Master] Fix 13602 preserve comment following element in node list
This commit is contained in:
Yui
2017-02-09 12:51:22 -08:00
committed by GitHub
46 changed files with 496 additions and 20 deletions

View File

@@ -9,6 +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;
emitLeadingCommentsOfPosition(pos: number): void;
}
export function createCommentWriter(printerOptions: PrinterOptions, emitPos: ((pos: number) => void) | undefined): CommentWriter {
@@ -32,6 +33,7 @@ namespace ts {
emitNodeWithComments,
emitBodyWithDetachedComments,
emitTrailingCommentsOfPosition,
emitLeadingCommentsOfPosition,
};
function emitNodeWithComments(hint: EmitHint, node: Node, emitCallback: (hint: EmitHint, node: Node) => void) {
@@ -210,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);
}

View File

@@ -1,4 +1,4 @@
/// <reference path="checker.ts" />
/// <reference path="checker.ts" />
/// <reference path="transformer.ts" />
/// <reference path="declarationEmitter.ts" />
/// <reference path="sourcemap.ts" />
@@ -211,6 +211,7 @@ namespace ts {
emitNodeWithComments,
emitBodyWithDetachedComments,
emitTrailingCommentsOfPosition,
emitLeadingCommentsOfPosition,
} = comments;
let currentSourceFile: SourceFile;
@@ -1346,6 +1347,10 @@ namespace ts {
else {
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
increaseIndent();
emitLeadingCommentsOfPosition(node.statements.end);
decreaseIndent();
writeToken(SyntaxKind.CloseBraceToken, node.statements.end, /*contextNode*/ node);
}
}
@@ -2228,6 +2233,15 @@ namespace ts {
// Write the delimiter if this is not the first node.
if (previousSibling) {
// i.e
// function commentedParameters(
// /* Parameter a */
// a
// /* End of parameter a */ -> this comment isn't considered to be trailing comment of parameter "a" due to newline
// ,
if (delimiter && previousSibling.end !== parentNode.end) {
emitLeadingCommentsOfPosition(previousSibling.end);
}
write(delimiter);
// Write either a line terminator or whitespace to separate the elements.
@@ -2274,6 +2288,17 @@ namespace ts {
write(",");
}
// Emit any trailing comment of the last element in the list
// i.e
// var array = [...
// 2
// /* end of element 2 */
// ];
if (previousSibling && delimiter && previousSibling.end !== parentNode.end) {
emitLeadingCommentsOfPosition(previousSibling.end);
}
// Decrease the indent, if requested.
if (format & ListFormat.Indented) {
decreaseIndent();