Skip to content

Commit 6bfc28f

Browse files
Try to grab parameters for single-declaration variable statements.
We only do this for a (parenthesized) function expression, arrow function, or class expression with a constructor. In the presence of a class expression, if there are multiple constructor declarations, the parameters are acquired from the first one.
1 parent 7b7e62c commit 6bfc28f

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

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 from an apst 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

0 commit comments

Comments
 (0)