Skip to content

Commit d9559d5

Browse files
Merge branch 'grabParamsFromInitializers'
2 parents 0ab9536 + 2692cde commit d9559d5

3 files changed

Lines changed: 163 additions & 11 deletions

File tree

src/services/services.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7059,7 +7059,7 @@ namespace ts {
70597059
return undefined;
70607060
}
70617061

7062-
let parameters = isFunctionLike(commentOwner) ? commentOwner.parameters : emptyArray;
7062+
let parameters = getParametersForJsDocOwningNode(commentOwner);
70637063
let posLineAndChar = sourceFile.getLineAndCharacterOfPosition(position);
70647064
let lineStart = sourceFile.getLineStarts()[posLineAndChar.line];
70657065

@@ -7096,6 +7096,52 @@ namespace ts {
70967096
return { newText: result, caretOffset: preamble.length };
70977097
}
70987098

7099+
function getParametersForJsDocOwningNode(commentOwner: Node): ParameterDeclaration[] {
7100+
if (isFunctionLike(commentOwner)) {
7101+
return commentOwner.parameters;
7102+
}
7103+
7104+
if (commentOwner.kind === SyntaxKind.VariableStatement) {
7105+
const varStatement = <VariableStatement>commentOwner;
7106+
const varDeclarations = varStatement.declarationList.declarations;
7107+
7108+
if (varDeclarations.length === 1 && varDeclarations[0].initializer) {
7109+
return getParametersFromRightHandSideOfAssignment(varDeclarations[0].initializer);
7110+
}
7111+
}
7112+
7113+
return emptyArray;
7114+
}
7115+
7116+
/**
7117+
* Digs into an an initializer or RHS operand of an assignment operation
7118+
* to get the parameters of an apt signature corresponding to a
7119+
* function expression or a class expression.
7120+
*
7121+
* @param rightHandSide the expression which may contain an appropriate set of parameters
7122+
* @returns the parameters of a signature found on the RHS if one exists; otherwise 'emptyArray'.
7123+
*/
7124+
function getParametersFromRightHandSideOfAssignment(rightHandSide: Expression): ParameterDeclaration[] {
7125+
while (rightHandSide.kind === SyntaxKind.ParenthesizedExpression) {
7126+
rightHandSide = (<ParenthesizedExpression>rightHandSide).expression;
7127+
}
7128+
7129+
switch (rightHandSide.kind) {
7130+
case SyntaxKind.FunctionExpression:
7131+
case SyntaxKind.ArrowFunction:
7132+
return (<FunctionExpression>rightHandSide).parameters;
7133+
case SyntaxKind.ClassExpression:
7134+
for (let member of (<ClassExpression>rightHandSide).members) {
7135+
if (member.kind === SyntaxKind.Constructor) {
7136+
return (<ConstructorDeclaration>member).parameters;
7137+
}
7138+
}
7139+
break;
7140+
}
7141+
7142+
return emptyArray;
7143+
}
7144+
70997145
function getTodoComments(fileName: string, descriptors: TodoCommentDescriptor[]): TodoComment[] {
71007146
// Note: while getting todo comments seems like a syntactic operation, we actually
71017147
// treat it as a semantic operation here. This is because we expect our host to call

tests/cases/fourslash/docCommentTemplateVariableStatements01.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,43 @@ function confirmNormalizedJsDoc(markerName: string, newTextOffset: number, templ
2828
////const c = 30;
2929
////
3030
/////*d*/
31-
////let d = function d(x, y, z) {
32-
//// return +(x + y + z);
31+
////let d = {
32+
//// foo: 10,
33+
//// bar: "20"
3334
////};
3435
////
3536
/////*e*/
36-
////let e = class E {
37+
////let e = function e(x, y, z) {
38+
//// return +(x + y + z);
39+
////};
40+
////
41+
/////*f*/
42+
////let f = class F {
3743
//// constructor(a, b, c) {
3844
//// this.a = a;
3945
//// this.b = b || (this.c = c);
4046
//// }
4147
////}
42-
////
43-
/////*f*/
44-
////let f = {
45-
//// foo: 10,
46-
//// bar: "20"
47-
////};
4848

49-
for (const varName of "abcdef".split("")) {
49+
for (const varName of "abcd".split("")) {
5050
confirmNormalizedJsDoc(varName, /*newTextOffset*/ 8, `
5151
/**
5252
*
5353
*/`);
5454
}
55+
56+
confirmNormalizedJsDoc("e", /*newTextOffset*/ 8, `
57+
/**
58+
*
59+
* @param x
60+
* @param y
61+
* @param z
62+
*/`);
63+
64+
confirmNormalizedJsDoc("f", /*newTextOffset*/ 8, `
65+
/**
66+
*
67+
* @param a
68+
* @param b
69+
* @param c
70+
*/`);
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
const CRLF = "\r\n";
4+
/**
5+
* @returns the given value with '\n' normalized to '\r\n' and with no leading newline
6+
*/
7+
function useCRLFAndStripLeadingNewline(str: string): string {
8+
str = str.replace(/\r?\n/g, CRLF);
9+
if (str.indexOf(CRLF) === 0) {
10+
str = str.slice(CRLF.length);
11+
}
12+
return str;
13+
}
14+
15+
function confirmNormalizedJsDoc(markerName: string, newTextOffset: number, template: string): void {
16+
goTo.marker(markerName);
17+
const normalized = useCRLFAndStripLeadingNewline(template);
18+
verify.DocCommentTemplate(normalized, newTextOffset);
19+
}
20+
21+
/////*a*/
22+
////var a = x => x
23+
////
24+
/////*b*/
25+
////let b = (x,y,z) => x + y + z;
26+
////
27+
/////*c*/
28+
////const c = ((x => +x))
29+
////
30+
/////*d*/
31+
////let d = (function () { })
32+
////
33+
/////*e*/
34+
////let e = function e([a,b,c]) {
35+
//// return "hello"
36+
////};
37+
////
38+
/////*f*/
39+
////let f = class {
40+
////}
41+
////
42+
/////*g*/
43+
////const g = ((class G {
44+
//// constructor(private x);
45+
//// constructor(x,y,z);
46+
//// constructor(x,y,z, ...okayThatsEnough) {
47+
//// }
48+
////}))
49+
50+
confirmNormalizedJsDoc("a", /*newTextOffset*/ 8, `
51+
/**
52+
*
53+
* @param x
54+
*/`);
55+
56+
confirmNormalizedJsDoc("b", /*newTextOffset*/ 8, `
57+
/**
58+
*
59+
* @param x
60+
* @param y
61+
* @param z
62+
*/`);
63+
64+
confirmNormalizedJsDoc("c", /*newTextOffset*/ 8, `
65+
/**
66+
*
67+
* @param x
68+
*/`);
69+
70+
confirmNormalizedJsDoc("d", /*newTextOffset*/ 8, `
71+
/**
72+
*
73+
*/`);
74+
75+
confirmNormalizedJsDoc("e", /*newTextOffset*/ 8, `
76+
/**
77+
*
78+
* @param param0
79+
*/`);
80+
81+
confirmNormalizedJsDoc("f", /*newTextOffset*/ 8, `
82+
/**
83+
*
84+
*/`);
85+
86+
confirmNormalizedJsDoc("g", /*newTextOffset*/ 8, `
87+
/**
88+
*
89+
* @param x
90+
*/`);

0 commit comments

Comments
 (0)