From ce89da227d72abd11809d0a3f7100d0adfa2353d Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Tue, 19 Aug 2014 10:00:38 -0700 Subject: [PATCH] Include previous token trailing comments for parameters and type parameters as part of leading comments of them --- src/compiler/core.ts | 4 +- src/compiler/emitter.ts | 2 +- src/compiler/parser.ts | 27 ++++----- src/compiler/scanner.ts | 4 +- .../reference/commentsClassMembers.js | 12 ++-- .../reference/commentsCommentParsing.js | 2 +- tests/baselines/reference/commentsFunction.js | 56 ++++--------------- .../baselines/reference/commentsInterface.js | 2 +- tests/baselines/reference/commentsModules.js | 2 +- .../reference/commentsemitComments.js | 2 +- .../baselines/reference/declFileAccessors.js | 12 ++-- .../reference/declFileConstructors.js | 4 +- .../baselines/reference/declFileFunctions.js | 6 +- tests/baselines/reference/declFileMethods.js | 16 +++--- tests/cases/compiler/commentsFunction.ts | 5 +- 15 files changed, 62 insertions(+), 94 deletions(-) diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 8f6ac1ddfc8..39a18167878 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -68,8 +68,8 @@ module ts { } export function concatenate(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); } diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index f03a0c14f49..97552f64a2b 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -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); diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 1649145495f..3065f5646b0 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -139,26 +139,23 @@ module ts { return ((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 /**/ diff --git a/src/compiler/scanner.ts b/src/compiler/scanner.ts index 96efd066fc5..ced0e031ba8 100644 --- a/src/compiler/scanner.ts +++ b/src/compiler/scanner.ts @@ -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); } diff --git a/tests/baselines/reference/commentsClassMembers.js b/tests/baselines/reference/commentsClassMembers.js index 90f1b4e5a84..298991c2eef 100644 --- a/tests/baselines/reference/commentsClassMembers.js +++ b/tests/baselines/reference/commentsClassMembers.js @@ -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, diff --git a/tests/baselines/reference/commentsCommentParsing.js b/tests/baselines/reference/commentsCommentParsing.js index de99dfffb41..2f2746145e5 100644 --- a/tests/baselines/reference/commentsCommentParsing.js +++ b/tests/baselines/reference/commentsCommentParsing.js @@ -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; } /**/ diff --git a/tests/baselines/reference/commentsFunction.js b/tests/baselines/reference/commentsFunction.js index a8fbfbd10ba..69061278feb 100644 --- a/tests/baselines/reference/commentsFunction.js +++ b/tests/baselines/reference/commentsFunction.js @@ -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; diff --git a/tests/baselines/reference/commentsInterface.js b/tests/baselines/reference/commentsInterface.js index 00255f74129..1951137b048 100644 --- a/tests/baselines/reference/commentsInterface.js +++ b/tests/baselines/reference/commentsInterface.js @@ -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), diff --git a/tests/baselines/reference/commentsModules.js b/tests/baselines/reference/commentsModules.js index 60fb6ee40d9..f964c45f843 100644 --- a/tests/baselines/reference/commentsModules.js +++ b/tests/baselines/reference/commentsModules.js @@ -127,7 +127,7 @@ var m1; } m1.fooExport = fooExport; // shouldn't appear - function foo2Export(a) { + function foo2Export(/**hm*/ a) { } m1.foo2Export = foo2Export; /** foo3Export diff --git a/tests/baselines/reference/commentsemitComments.js b/tests/baselines/reference/commentsemitComments.js index fdb6b45d704..173d9e8fd47 100644 --- a/tests/baselines/reference/commentsemitComments.js +++ b/tests/baselines/reference/commentsemitComments.js @@ -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; diff --git a/tests/baselines/reference/declFileAccessors.js b/tests/baselines/reference/declFileAccessors.js index fa45a545003..f39cd13fdd0 100644 --- a/tests/baselines/reference/declFileAccessors.js +++ b/tests/baselines/reference/declFileAccessors.js @@ -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 diff --git a/tests/baselines/reference/declFileConstructors.js b/tests/baselines/reference/declFileConstructors.js index f6c059710b1..327221debf8 100644 --- a/tests/baselines/reference/declFileConstructors.js +++ b/tests/baselines/reference/declFileConstructors.js @@ -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; diff --git a/tests/baselines/reference/declFileFunctions.js b/tests/baselines/reference/declFileFunctions.js index c1bb726c9ff..b5018e1fb1d 100644 --- a/tests/baselines/reference/declFileFunctions.js +++ b/tests/baselines/reference/declFileFunctions.js @@ -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; diff --git a/tests/baselines/reference/declFileMethods.js b/tests/baselines/reference/declFileMethods.js index 57853fedd13..2b82a4b71b5 100644 --- a/tests/baselines/reference/declFileMethods.js +++ b/tests/baselines/reference/declFileMethods.js @@ -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; diff --git a/tests/cases/compiler/commentsFunction.ts b/tests/cases/compiler/commentsFunction.ts index 8c8d7ae0841..06d3c1bbf98 100644 --- a/tests/cases/compiler/commentsFunction.ts +++ b/tests/cases/compiler/commentsFunction.ts @@ -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 \ No newline at end of file +/*leading comment*/(() => 0); //trailing comment + +function blah4(/*1*/a: string/*2*/,/*3*/b: string/*4*/) { +} \ No newline at end of file