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);
}

View File

@ -225,7 +225,7 @@ var c1 = (function () {
function c1() {
}
/** sum with property*/
c1.prototype.p2 = function (b) {
c1.prototype.p2 = function (/** number to add*/ b) {
return this.p1 + b;
}; /* trailing comment of method*/
Object.defineProperty(c1.prototype, "p3", {
@ -235,7 +235,7 @@ var c1 = (function () {
} // trailing comment Getter
,
/** setter property*/
set: function (value) {
set: function (/** this is value*/ value) {
this.p1 = this.p2(value);
} // trailing comment Setter
,
@ -243,7 +243,7 @@ var c1 = (function () {
configurable: true
});
/** sum with property*/
c1.prototype.pp2 = function (b) {
c1.prototype.pp2 = function (/** number to add*/ b) {
return this.p1 + b;
}; // trailing comment of method
Object.defineProperty(c1.prototype, "pp3", {
@ -252,14 +252,14 @@ var c1 = (function () {
return this.pp2(this.pp1);
},
/** setter property*/
set: function (value) {
set: function (/** this is value*/ value) {
this.pp1 = this.pp2(value);
},
enumerable: true,
configurable: true
});
/** static sum with property*/
c1.s2 = function (b) {
c1.s2 = function (/** number to add*/ b) {
return c1.s1 + b;
};
Object.defineProperty(c1, "s3", {
@ -268,7 +268,7 @@ var c1 = (function () {
return c1.s2(c1.s1);
} /*trailing comment 1 getter*/,
/** setter property*/
set: function (value) {
set: function (/** this is value*/ value) {
c1.s1 = c1.s2(value);
} /*trailing comment 2 */ /*setter*/,
enumerable: true,

View File

@ -280,7 +280,7 @@ function divide(a, b) {
*@param a it is first parameter
*@param c it is third parameter
*/
function jsDocParamTest(a, b, c, d) {
function jsDocParamTest(/** this is inline comment for a */ a, /** this is inline comment for b*/ b, c, d) {
return a + b + c + d;
}
/**/

View File

@ -38,7 +38,10 @@ function blah3(a: string // trailing commen single line
lambdaFoo = (a, b) => a * b; // This is trailing comment
/*leading comment*/() => 0; // Needs to be wrapped in parens to be a valid expression (not declaration)
/*leading comment*/(() => 0); //trailing comment
/*leading comment*/(() => 0); //trailing comment
function blah4(/*1*/a: string/*2*/,/*3*/b: string/*4*/) {
}
//// [commentsFunction.js]
/** This comment should appear for foo*/
@ -46,7 +49,7 @@ function foo() {
} /* trailing comment of function */
foo();
/** This is comment for function signature*/
function fooWithParameters(a,
function fooWithParameters(/** this is comment about a*/ a,
/** this is comment for b*/
b) {
var d = a;
@ -55,12 +58,12 @@ fooWithParameters("a", 10);
/** fooFunc
* comment
*/
var fooFunc = function FooFunctionValue(b) {
var fooFunc = function FooFunctionValue(/** fooFunctionValue param */ b) {
return b;
};
/// lamdaFoo var comment
var lambdaFoo = function (a, b) { return a + b; };
var lambddaNoVarComment = function (a, b) { return a * b; };
var lambdaFoo = function (/**param a*/ a, /**param b*/ b) { return a + b; };
var lambddaNoVarComment = function (/**param a*/ a, /**param b*/ b) { return a * b; };
lambdaFoo(10, 20);
lambddaNoVarComment(10, 20);
function blah(a /* multiline trailing comment
@ -73,45 +76,9 @@ function blah3(a // trailing commen single line
}
lambdaFoo = function (a, b) { return a * b; }; // This is trailing comment
/*leading comment*/ (function () { return 0; }); // Needs to be wrapped in parens to be a valid expression (not declaration)
/** This comment should appear for foo*/
function foo() {
} /* trailing comment of function */
foo();
/** This is comment for function signature*/
function fooWithParameters(/** this is comment about a*/a: string,
/** this is comment for b*/
b: number) {
var d = a;
} // trailing comment of function
fooWithParameters("a", 10);
/** fooFunc
* comment
*/
var fooFunc = function FooFunctionValue(/** fooFunctionValue param */ b: string) {
return b;
}
/// lamdaFoo var comment
var lambdaFoo = /** this is lambda comment*/ (/**param a*/a: number, /**param b*/b: number) => a + b;
var lambddaNoVarComment = /** this is lambda multiplication*/ (/**param a*/a: number, /**param b*/b: number) => a * b;
lambdaFoo(10, 20);
lambddaNoVarComment(10, 20);
function blah(a: string /* multiline trailing comment
multiline */) {
}
function blah2(a: string /* single line multiple trailing comments */ /* second */) {
}
function blah3(a: string // trailing commen single line
) {
}
lambdaFoo = (a, b) => a * b; // This is trailing comment
/*leading comment*/() => 0; // Needs to be wrapped in parens to be a valid expression (not declaration)
(function () { return 0; }); //trailing comment
/*leading comment*/ (function () { return 0; }); //trailing comment
function blah4(/*1*/ a /*2*/, /*3*/ b /*4*/) {
}
//// [commentsFunction.d.ts]
@ -130,3 +97,4 @@ declare var lambddaNoVarComment: (a: number, b: number) => number;
declare function blah(a: string): void;
declare function blah2(a: string): void;
declare function blah3(a: string): void;
declare function blah4(a: string, b: string): void;

View File

@ -89,7 +89,7 @@ var i2_i_nc_fnfoo = i2_i.nc_fnfoo;
var i2_i_nc_fnfoo_r = i2_i.nc_fnfoo(10);
var i3_i;
i3_i = {
f: function (a) { return "Hello" + a; },
f: function (/**i3_i a*/ a) { return "Hello" + a; },
l: this.f,
/** own x*/
x: this.f(10),

View File

@ -127,7 +127,7 @@ var m1;
}
m1.fooExport = fooExport;
// shouldn't appear
function foo2Export(a) {
function foo2Export(/**hm*/ a) {
}
m1.foo2Export = foo2Export;
/** foo3Export

View File

@ -92,7 +92,7 @@ declare var x;
/** Variable comments*/
var myVariable = 10;
/** function comments*/
function foo(p) {
function foo(/** parameter comment*/ p) {
}
/** variable with function type comment*/
var fooVar;

View File

@ -112,7 +112,7 @@ var c1 = (function () {
return 10;
},
/** setter property*/
set: function (value) {
set: function (/** this is value*/ value) {
},
enumerable: true,
configurable: true
@ -123,7 +123,7 @@ var c1 = (function () {
return 10;
},
/** private setter property*/
set: function (value) {
set: function (/** this is value*/ value) {
},
enumerable: true,
configurable: true
@ -134,7 +134,7 @@ var c1 = (function () {
return 10;
},
/** setter property*/
set: function (value) {
set: function (/** this is value*/ value) {
},
enumerable: true,
configurable: true
@ -195,7 +195,7 @@ var c2 = (function () {
return 10;
},
/** setter property*/
set: function (value) {
set: function (/** this is value*/ value) {
},
enumerable: true,
configurable: true
@ -206,7 +206,7 @@ var c2 = (function () {
return 10;
},
/** private setter property*/
set: function (value) {
set: function (/** this is value*/ value) {
},
enumerable: true,
configurable: true
@ -217,7 +217,7 @@ var c2 = (function () {
return 10;
},
/** setter property*/
set: function (value) {
set: function (/** this is value*/ value) {
},
enumerable: true,
configurable: true

View File

@ -107,7 +107,7 @@ var SimpleConstructor = (function () {
exports.SimpleConstructor = SimpleConstructor;
var ConstructorWithParameters = (function () {
/** This is comment for function signature*/
function ConstructorWithParameters(a,
function ConstructorWithParameters(/** this is comment about a*/ a,
/** this is comment for b*/
b) {
var d = a;
@ -170,7 +170,7 @@ var GlobalSimpleConstructor = (function () {
})();
var GlobalConstructorWithParameters = (function () {
/** This is comment for function signature*/
function GlobalConstructorWithParameters(a,
function GlobalConstructorWithParameters(/** this is comment about a*/ a,
/** this is comment for b*/
b) {
var d = a;

View File

@ -65,7 +65,7 @@ function foo() {
}
exports.foo = foo;
/** This is comment for function signature*/
function fooWithParameters(a,
function fooWithParameters(/** this is comment about a*/ a,
/** this is comment for b*/
b) {
var d = a;
@ -87,7 +87,7 @@ exports.fooWithOverloads = fooWithOverloads;
function nonExportedFoo() {
}
/** This is comment for function signature*/
function nonExportedFooWithParameters(a,
function nonExportedFooWithParameters(/** this is comment about a*/ a,
/** this is comment for b*/
b) {
var d = a;
@ -107,7 +107,7 @@ function nonExportedFooWithOverloads(a) {
function globalfoo() {
}
/** This is comment for function signature*/
function globalfooWithParameters(a,
function globalfooWithParameters(/** this is comment about a*/ a,
/** this is comment for b*/
b) {
var d = a;

View File

@ -198,7 +198,7 @@ var c1 = (function () {
c1.prototype.foo = function () {
};
/** This is comment for function signature*/
c1.prototype.fooWithParameters = function (a,
c1.prototype.fooWithParameters = function (/** this is comment about a*/ a,
/** this is comment for b*/
b) {
var d = a;
@ -217,7 +217,7 @@ var c1 = (function () {
c1.prototype.privateFoo = function () {
};
/** This is comment for function signature*/
c1.prototype.privateFooWithParameters = function (a,
c1.prototype.privateFooWithParameters = function (/** this is comment about a*/ a,
/** this is comment for b*/
b) {
var d = a;
@ -236,7 +236,7 @@ var c1 = (function () {
c1.staticFoo = function () {
};
/** This is comment for function signature*/
c1.staticFooWithParameters = function (a,
c1.staticFooWithParameters = function (/** this is comment about a*/ a,
/** this is comment for b*/
b) {
var d = a;
@ -255,7 +255,7 @@ var c1 = (function () {
c1.privateStaticFoo = function () {
};
/** This is comment for function signature*/
c1.privateStaticFooWithParameters = function (a,
c1.privateStaticFooWithParameters = function (/** this is comment about a*/ a,
/** this is comment for b*/
b) {
var d = a;
@ -281,7 +281,7 @@ var c2 = (function () {
c2.prototype.foo = function () {
};
/** This is comment for function signature*/
c2.prototype.fooWithParameters = function (a,
c2.prototype.fooWithParameters = function (/** this is comment about a*/ a,
/** this is comment for b*/
b) {
var d = a;
@ -300,7 +300,7 @@ var c2 = (function () {
c2.prototype.privateFoo = function () {
};
/** This is comment for function signature*/
c2.prototype.privateFooWithParameters = function (a,
c2.prototype.privateFooWithParameters = function (/** this is comment about a*/ a,
/** this is comment for b*/
b) {
var d = a;
@ -319,7 +319,7 @@ var c2 = (function () {
c2.staticFoo = function () {
};
/** This is comment for function signature*/
c2.staticFooWithParameters = function (a,
c2.staticFooWithParameters = function (/** this is comment about a*/ a,
/** this is comment for b*/
b) {
var d = a;
@ -338,7 +338,7 @@ var c2 = (function () {
c2.privateStaticFoo = function () {
};
/** This is comment for function signature*/
c2.privateStaticFooWithParameters = function (a,
c2.privateStaticFooWithParameters = function (/** this is comment about a*/ a,
/** this is comment for b*/
b) {
var d = a;

View File

@ -40,4 +40,7 @@ function blah3(a: string // trailing commen single line
lambdaFoo = (a, b) => a * b; // This is trailing comment
/*leading comment*/() => 0; // Needs to be wrapped in parens to be a valid expression (not declaration)
/*leading comment*/(() => 0); //trailing comment
/*leading comment*/(() => 0); //trailing comment
function blah4(/*1*/a: string/*2*/,/*3*/b: string/*4*/) {
}