Support @param tag on property declaration initializer (#21907)

* Support @param tag on property declaration initializer

* Update test

* Finish updating test
This commit is contained in:
Andy 2018-02-14 10:12:38 -08:00 committed by GitHub
parent 47d84f87ce
commit 2ee92948d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 57 additions and 17 deletions

View file

@ -1620,17 +1620,19 @@ namespace ts {
node.expression.right;
}
function getSingleInitializerOfVariableStatement(node: Node, child?: Node): Node {
return isVariableStatement(node) &&
node.declarationList.declarations.length > 0 &&
(!child || node.declarationList.declarations[0].initializer === child) &&
node.declarationList.declarations[0].initializer;
function getSingleInitializerOfVariableStatementOrPropertyDeclaration(node: Node): Expression | undefined {
switch (node.kind) {
case ts.SyntaxKind.VariableStatement:
const v = getSingleVariableOfVariableStatement(node);
return v && v.initializer;
case ts.SyntaxKind.PropertyDeclaration:
return (node as PropertyDeclaration).initializer;
}
}
function getSingleVariableOfVariableStatement(node: Node, child?: Node): Node {
function getSingleVariableOfVariableStatement(node: Node): VariableDeclaration | undefined {
return isVariableStatement(node) &&
node.declarationList.declarations.length > 0 &&
(!child || node.declarationList.declarations[0] === child) &&
node.declarationList.declarations[0];
}
@ -1648,7 +1650,7 @@ namespace ts {
function getJSDocCommentsAndTagsWorker(node: Node): void {
const parent = node.parent;
if (parent && (parent.kind === SyntaxKind.PropertyAssignment || getNestedModuleDeclaration(parent))) {
if (parent && (parent.kind === SyntaxKind.PropertyAssignment || parent.kind === SyntaxKind.PropertyDeclaration || getNestedModuleDeclaration(parent))) {
getJSDocCommentsAndTagsWorker(parent);
}
// Try to recognize this pattern when node is initializer of variable declaration and JSDoc comments are on containing variable statement.
@ -1658,10 +1660,10 @@ namespace ts {
// */
// var x = function(name) { return name.length; }
if (parent && parent.parent &&
(getSingleVariableOfVariableStatement(parent.parent, node) || getSourceOfAssignment(parent.parent))) {
(getSingleVariableOfVariableStatement(parent.parent) === node || getSourceOfAssignment(parent.parent))) {
getJSDocCommentsAndTagsWorker(parent.parent);
}
if (parent && parent.parent && parent.parent.parent && getSingleInitializerOfVariableStatement(parent.parent.parent, node)) {
if (parent && parent.parent && parent.parent.parent && getSingleInitializerOfVariableStatementOrPropertyDeclaration(parent.parent.parent) === node) {
getJSDocCommentsAndTagsWorker(parent.parent.parent);
}
if (isBinaryExpression(node) && getSpecialPropertyAssignmentKind(node) !== SpecialPropertyAssignmentKind.None ||
@ -1704,7 +1706,7 @@ namespace ts {
export function getHostSignatureFromJSDoc(node: JSDocParameterTag): FunctionLike | undefined {
const host = getJSDocHost(node);
const decl = getSourceOfAssignment(host) ||
getSingleInitializerOfVariableStatement(host) ||
getSingleInitializerOfVariableStatementOrPropertyDeclaration(host) ||
getSingleVariableOfVariableStatement(host) ||
getNestedModuleDeclaration(host) ||
host;

View file

@ -0,0 +1,13 @@
=== /a.js ===
class Foo {
>Foo : Symbol(Foo, Decl(a.js, 0, 0))
/**@param {string} x */
m = x => x.toLowerCase();
>m : Symbol(Foo.m, Decl(a.js, 0, 11))
>x : Symbol(x, Decl(a.js, 2, 7))
>x.toLowerCase : Symbol(String.toLowerCase, Decl(lib.d.ts, --, --))
>x : Symbol(x, Decl(a.js, 2, 7))
>toLowerCase : Symbol(String.toLowerCase, Decl(lib.d.ts, --, --))
}

View file

@ -0,0 +1,15 @@
=== /a.js ===
class Foo {
>Foo : Foo
/**@param {string} x */
m = x => x.toLowerCase();
>m : (x: string) => string
>x => x.toLowerCase() : (x: string) => string
>x : string
>x.toLowerCase() : string
>x.toLowerCase : () => string
>x : string
>toLowerCase : () => string
}

View file

@ -0,0 +1,10 @@
// @allowJs: true
// @checkJs: true
// @noEmit: true
// @noImplicitAny: true
// @Filename: /a.js
class Foo {
/**@param {string} x */
m = x => x.toLowerCase();
}

View file

@ -271,10 +271,10 @@ verify.completionListContains("i1_nc_f1", "(method) c1.i1_nc_f1(): void", "");
verify.completionListContains("i1_nc_l1", "(property) c1.i1_nc_l1: () => void", "");
verify.completionListContains("p1", "(property) c1.p1: number", "c1_p1");
verify.completionListContains("f1", "(method) c1.f1(): void", "c1_f1");
verify.completionListContains("l1", "(property) c1.l1: () => void", "");
verify.completionListContains("l1", "(property) c1.l1: () => void", "c1_l1");
verify.completionListContains("nc_p1", "(property) c1.nc_p1: number", "c1_nc_p1");
verify.completionListContains("nc_f1", "(method) c1.nc_f1(): void", "c1_nc_f1");
verify.completionListContains("nc_l1", "(property) c1.nc_l1: () => void", "");
verify.completionListContains("nc_l1", "(property) c1.nc_l1: () => void", "c1_nc_l1");
goTo.marker('7');
verify.currentSignatureHelpDocCommentIs("i1_f1");
goTo.marker('8');
@ -288,9 +288,9 @@ verify.currentSignatureHelpDocCommentIs("");
goTo.marker('l8');
verify.currentSignatureHelpDocCommentIs("");
goTo.marker('l9');
verify.currentSignatureHelpDocCommentIs("");
verify.currentSignatureHelpDocCommentIs("c1_l1");
goTo.marker('l10');
verify.currentSignatureHelpDocCommentIs("");
verify.currentSignatureHelpDocCommentIs("c1_nc_l1");
verify.quickInfos({
"6iq": "var c1_i: c1",
@ -300,8 +300,8 @@ verify.quickInfos({
"10q": ["(method) c1.nc_f1(): void", "c1_nc_f1"],
l7q: "(property) c1.i1_l1: () => void",
l8q: "(property) c1.i1_nc_l1: () => void",
l9q: "(property) c1.l1: () => void",
l10q: "(property) c1.nc_l1: () => void"
l9q: ["(property) c1.l1: () => void", "c1_l1"],
l10q: ["(property) c1.nc_l1: () => void", "c1_nc_l1"],
});
goTo.marker('11');