Skip to content

Commit ce89da2

Browse files
committed
Include previous token trailing comments for parameters and type parameters as part of leading comments of them
1 parent ecaf4ad commit ce89da2

15 files changed

+62
-94
lines changed

src/compiler/core.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ module ts {
6868
}
6969

7070
export function concatenate<T>(array1: T[], array2: T[]): T[] {
71-
if (!array2.length) return array1;
72-
if (!array1.length) return array2;
71+
if (!array2 || !array2.length) return array1;
72+
if (!array1 || !array1.length) return array2;
7373
return array1.concat(array2);
7474
}
7575

src/compiler/emitter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2064,7 +2064,7 @@ module ts {
20642064
function emitLeadingDeclarationComments(node: Node) {
20652065
// Emit the leading comments only if the parent's pos doesnt match because parent should take care of emitting these comments
20662066
if (node.parent.kind === SyntaxKind.SourceFile || node.pos !== node.parent.pos) {
2067-
var leadingComments = getLeadingComments(currentSourceFile.text, node.pos);
2067+
var leadingComments = getLeadingCommentsOfNode(node, currentSourceFile);
20682068
emitNewLineBeforeLeadingComments(node, leadingComments, writer);
20692069
// Leading comments are emitted at /*leading comment1 */space/*leading comment*/space
20702070
emitComments(leadingComments, /*trailingSeparator*/ true, writer, writeComment);

src/compiler/parser.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -139,26 +139,23 @@ module ts {
139139
return (<Identifier>(<ExpressionStatement>node).expression).text === "use strict";
140140
}
141141

142-
export function getJsDocComments(node: Declaration, sourceFileOfNode: SourceFile) {
143-
var comments: Comment[];
142+
export function getLeadingCommentsOfNode(node: Node, sourceFileOfNode: SourceFile) {
143+
// If parameter/type parameter, the prev token trailing comments are part of this node too
144144
if (node.kind === SyntaxKind.Parameter || node.kind === SyntaxKind.TypeParameter) {
145-
// First check if the parameter was written like so:
146-
// (
147-
// /** blah */ a,
148-
// /** blah */ b);
149-
comments = getLeadingComments(sourceFileOfNode.text, node.pos);
150-
if (!comments) {
151-
// Now check if it was written like so:
152-
// (/** blah */ a, /** blah */ b);
153-
// In this case, the comment will belong to the preceding token.
154-
comments = getTrailingComments(sourceFileOfNode.text, node.pos);
155-
}
145+
// eg (/** blah */ a, /** blah */ b);
146+
return concatenate(getTrailingComments(sourceFileOfNode.text, node.pos),
147+
// eg: (
148+
// /** blah */ a,
149+
// /** blah */ b);
150+
getLeadingComments(sourceFileOfNode.text, node.pos));
156151
}
157152
else {
158-
comments = getLeadingComments(sourceFileOfNode.text, node.pos);
153+
return getLeadingComments(sourceFileOfNode.text, node.pos);
159154
}
155+
}
160156

161-
return filter(comments, comment => isJsDocComment(comment));
157+
export function getJsDocComments(node: Declaration, sourceFileOfNode: SourceFile) {
158+
return filter(getLeadingCommentsOfNode(node, sourceFileOfNode), comment => isJsDocComment(comment));
162159

163160
function isJsDocComment(comment: Comment) {
164161
// js doc is if comment is starting with /** but not if it is /**/

src/compiler/scanner.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,11 +433,11 @@ module ts {
433433
}
434434
}
435435

436-
export function getLeadingComments(text: string, pos: number): TextRange[] {
436+
export function getLeadingComments(text: string, pos: number): Comment[] {
437437
return getCommentRanges(text, pos, /*trailing*/ false);
438438
}
439439

440-
export function getTrailingComments(text: string, pos: number): TextRange[] {
440+
export function getTrailingComments(text: string, pos: number): Comment[] {
441441
return getCommentRanges(text, pos, /*trailing*/ true);
442442
}
443443

tests/baselines/reference/commentsClassMembers.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ var c1 = (function () {
225225
function c1() {
226226
}
227227
/** sum with property*/
228-
c1.prototype.p2 = function (b) {
228+
c1.prototype.p2 = function (/** number to add*/ b) {
229229
return this.p1 + b;
230230
}; /* trailing comment of method*/
231231
Object.defineProperty(c1.prototype, "p3", {
@@ -235,15 +235,15 @@ var c1 = (function () {
235235
} // trailing comment Getter
236236
,
237237
/** setter property*/
238-
set: function (value) {
238+
set: function (/** this is value*/ value) {
239239
this.p1 = this.p2(value);
240240
} // trailing comment Setter
241241
,
242242
enumerable: true,
243243
configurable: true
244244
});
245245
/** sum with property*/
246-
c1.prototype.pp2 = function (b) {
246+
c1.prototype.pp2 = function (/** number to add*/ b) {
247247
return this.p1 + b;
248248
}; // trailing comment of method
249249
Object.defineProperty(c1.prototype, "pp3", {
@@ -252,14 +252,14 @@ var c1 = (function () {
252252
return this.pp2(this.pp1);
253253
},
254254
/** setter property*/
255-
set: function (value) {
255+
set: function (/** this is value*/ value) {
256256
this.pp1 = this.pp2(value);
257257
},
258258
enumerable: true,
259259
configurable: true
260260
});
261261
/** static sum with property*/
262-
c1.s2 = function (b) {
262+
c1.s2 = function (/** number to add*/ b) {
263263
return c1.s1 + b;
264264
};
265265
Object.defineProperty(c1, "s3", {
@@ -268,7 +268,7 @@ var c1 = (function () {
268268
return c1.s2(c1.s1);
269269
} /*trailing comment 1 getter*/,
270270
/** setter property*/
271-
set: function (value) {
271+
set: function (/** this is value*/ value) {
272272
c1.s1 = c1.s2(value);
273273
} /*trailing comment 2 */ /*setter*/,
274274
enumerable: true,

tests/baselines/reference/commentsCommentParsing.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ function divide(a, b) {
280280
*@param a it is first parameter
281281
*@param c it is third parameter
282282
*/
283-
function jsDocParamTest(a, b, c, d) {
283+
function jsDocParamTest(/** this is inline comment for a */ a, /** this is inline comment for b*/ b, c, d) {
284284
return a + b + c + d;
285285
}
286286
/**/

tests/baselines/reference/commentsFunction.js

Lines changed: 12 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,18 @@ function blah3(a: string // trailing commen single line
3838
lambdaFoo = (a, b) => a * b; // This is trailing comment
3939

4040
/*leading comment*/() => 0; // Needs to be wrapped in parens to be a valid expression (not declaration)
41-
/*leading comment*/(() => 0); //trailing comment
41+
/*leading comment*/(() => 0); //trailing comment
42+
43+
function blah4(/*1*/a: string/*2*/,/*3*/b: string/*4*/) {
44+
}
4245

4346
//// [commentsFunction.js]
4447
/** This comment should appear for foo*/
4548
function foo() {
4649
} /* trailing comment of function */
4750
foo();
4851
/** This is comment for function signature*/
49-
function fooWithParameters(a,
52+
function fooWithParameters(/** this is comment about a*/ a,
5053
/** this is comment for b*/
5154
b) {
5255
var d = a;
@@ -55,12 +58,12 @@ fooWithParameters("a", 10);
5558
/** fooFunc
5659
* comment
5760
*/
58-
var fooFunc = function FooFunctionValue(b) {
61+
var fooFunc = function FooFunctionValue(/** fooFunctionValue param */ b) {
5962
return b;
6063
};
6164
/// lamdaFoo var comment
62-
var lambdaFoo = function (a, b) { return a + b; };
63-
var lambddaNoVarComment = function (a, b) { return a * b; };
65+
var lambdaFoo = function (/**param a*/ a, /**param b*/ b) { return a + b; };
66+
var lambddaNoVarComment = function (/**param a*/ a, /**param b*/ b) { return a * b; };
6467
lambdaFoo(10, 20);
6568
lambddaNoVarComment(10, 20);
6669
function blah(a /* multiline trailing comment
@@ -73,45 +76,9 @@ function blah3(a // trailing commen single line
7376
}
7477
lambdaFoo = function (a, b) { return a * b; }; // This is trailing comment
7578
/*leading comment*/ (function () { return 0; }); // Needs to be wrapped in parens to be a valid expression (not declaration)
76-
/** This comment should appear for foo*/
77-
function foo() {
78-
} /* trailing comment of function */
79-
foo();
80-
/** This is comment for function signature*/
81-
function fooWithParameters(/** this is comment about a*/a: string,
82-
/** this is comment for b*/
83-
b: number) {
84-
var d = a;
85-
} // trailing comment of function
86-
fooWithParameters("a", 10);
87-
/** fooFunc
88-
* comment
89-
*/
90-
var fooFunc = function FooFunctionValue(/** fooFunctionValue param */ b: string) {
91-
return b;
92-
}
93-
94-
/// lamdaFoo var comment
95-
var lambdaFoo = /** this is lambda comment*/ (/**param a*/a: number, /**param b*/b: number) => a + b;
96-
var lambddaNoVarComment = /** this is lambda multiplication*/ (/**param a*/a: number, /**param b*/b: number) => a * b;
97-
lambdaFoo(10, 20);
98-
lambddaNoVarComment(10, 20);
99-
100-
function blah(a: string /* multiline trailing comment
101-
multiline */) {
102-
}
103-
104-
function blah2(a: string /* single line multiple trailing comments */ /* second */) {
105-
}
106-
107-
function blah3(a: string // trailing commen single line
108-
) {
109-
}
110-
111-
lambdaFoo = (a, b) => a * b; // This is trailing comment
112-
113-
/*leading comment*/() => 0; // Needs to be wrapped in parens to be a valid expression (not declaration)
114-
(function () { return 0; }); //trailing comment
79+
/*leading comment*/ (function () { return 0; }); //trailing comment
80+
function blah4(/*1*/ a /*2*/, /*3*/ b /*4*/) {
81+
}
11582

11683

11784
//// [commentsFunction.d.ts]
@@ -130,3 +97,4 @@ declare var lambddaNoVarComment: (a: number, b: number) => number;
13097
declare function blah(a: string): void;
13198
declare function blah2(a: string): void;
13299
declare function blah3(a: string): void;
100+
declare function blah4(a: string, b: string): void;

tests/baselines/reference/commentsInterface.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ var i2_i_nc_fnfoo = i2_i.nc_fnfoo;
8989
var i2_i_nc_fnfoo_r = i2_i.nc_fnfoo(10);
9090
var i3_i;
9191
i3_i = {
92-
f: function (a) { return "Hello" + a; },
92+
f: function (/**i3_i a*/ a) { return "Hello" + a; },
9393
l: this.f,
9494
/** own x*/
9595
x: this.f(10),

tests/baselines/reference/commentsModules.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ var m1;
127127
}
128128
m1.fooExport = fooExport;
129129
// shouldn't appear
130-
function foo2Export(a) {
130+
function foo2Export(/**hm*/ a) {
131131
}
132132
m1.foo2Export = foo2Export;
133133
/** foo3Export

tests/baselines/reference/commentsemitComments.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ declare var x;
9292
/** Variable comments*/
9393
var myVariable = 10;
9494
/** function comments*/
95-
function foo(p) {
95+
function foo(/** parameter comment*/ p) {
9696
}
9797
/** variable with function type comment*/
9898
var fooVar;

0 commit comments

Comments
 (0)