Include previous token trailing comments for parameters and type parameters as part of leading comments of them

This commit is contained in:
Sheetal Nandi
2014-08-19 10:00:38 -07:00
parent ecaf4ad221
commit ce89da227d
15 changed files with 62 additions and 94 deletions

View File

@@ -68,8 +68,8 @@ module ts {
}
export function concatenate<T>(array1: T[], array2: T[]): T[] {
if (!array2.length) return array1;
if (!array1.length) return array2;
if (!array2 || !array2.length) return array1;
if (!array1 || !array1.length) return array2;
return array1.concat(array2);
}

View File

@@ -2064,7 +2064,7 @@ module ts {
function emitLeadingDeclarationComments(node: Node) {
// Emit the leading comments only if the parent's pos doesnt match because parent should take care of emitting these comments
if (node.parent.kind === SyntaxKind.SourceFile || node.pos !== node.parent.pos) {
var leadingComments = getLeadingComments(currentSourceFile.text, node.pos);
var leadingComments = getLeadingCommentsOfNode(node, currentSourceFile);
emitNewLineBeforeLeadingComments(node, leadingComments, writer);
// Leading comments are emitted at /*leading comment1 */space/*leading comment*/space
emitComments(leadingComments, /*trailingSeparator*/ true, writer, writeComment);

View File

@@ -139,26 +139,23 @@ module ts {
return (<Identifier>(<ExpressionStatement>node).expression).text === "use strict";
}
export function getJsDocComments(node: Declaration, sourceFileOfNode: SourceFile) {
var comments: Comment[];
export function getLeadingCommentsOfNode(node: Node, sourceFileOfNode: SourceFile) {
// If parameter/type parameter, the prev token trailing comments are part of this node too
if (node.kind === SyntaxKind.Parameter || node.kind === SyntaxKind.TypeParameter) {
// First check if the parameter was written like so:
// (
// /** blah */ a,
// /** blah */ b);
comments = getLeadingComments(sourceFileOfNode.text, node.pos);
if (!comments) {
// Now check if it was written like so:
// (/** blah */ a, /** blah */ b);
// In this case, the comment will belong to the preceding token.
comments = getTrailingComments(sourceFileOfNode.text, node.pos);
}
// eg (/** blah */ a, /** blah */ b);
return concatenate(getTrailingComments(sourceFileOfNode.text, node.pos),
// eg: (
// /** blah */ a,
// /** blah */ b);
getLeadingComments(sourceFileOfNode.text, node.pos));
}
else {
comments = getLeadingComments(sourceFileOfNode.text, node.pos);
return getLeadingComments(sourceFileOfNode.text, node.pos);
}
}
return filter(comments, comment => isJsDocComment(comment));
export function getJsDocComments(node: Declaration, sourceFileOfNode: SourceFile) {
return filter(getLeadingCommentsOfNode(node, sourceFileOfNode), comment => isJsDocComment(comment));
function isJsDocComment(comment: Comment) {
// js doc is if comment is starting with /** but not if it is /**/

View File

@@ -433,11 +433,11 @@ module ts {
}
}
export function getLeadingComments(text: string, pos: number): TextRange[] {
export function getLeadingComments(text: string, pos: number): Comment[] {
return getCommentRanges(text, pos, /*trailing*/ false);
}
export function getTrailingComments(text: string, pos: number): TextRange[] {
export function getTrailingComments(text: string, pos: number): Comment[] {
return getCommentRanges(text, pos, /*trailing*/ true);
}