Skip to content

Fix jsdoc typedef symbol scope + avoid bind twice if the type has a namespace prefix #14014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ internal/
!tests/cases/projects/NodeModulesSearch/**/*
!tests/baselines/reference/project/nodeModules*/**/*
.idea
yarn.lock
59 changes: 59 additions & 0 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,12 @@ namespace ts {
case SyntaxKind.CallExpression:
bindCallExpressionFlow(<CallExpression>node);
break;
case SyntaxKind.JSDocComment:
bindJSDocComment(<JSDoc>node);
break;
case SyntaxKind.JSDocTypedefTag:
bindJSDocTypedefTag(<JSDocTypedefTag>node);
break;
default:
bindEachChild(node);
break;
Expand Down Expand Up @@ -1298,6 +1304,26 @@ namespace ts {
}
}

function bindJSDocComment(node: JSDoc) {
forEachChild(node, n => {
if (n.kind !== SyntaxKind.JSDocTypedefTag) {
bind(n);
}
});
}

function bindJSDocTypedefTag(node: JSDocTypedefTag) {
forEachChild(node, n => {
// if the node has a fullName "A.B.C", that means symbol "C" was already bound
// when we visit "fullName"; so when we visit the name "C" as the next child of
// the jsDocTypedefTag, we should skip binding it.
if (node.fullName && n === node.name && node.fullName.kind !== SyntaxKind.Identifier) {
return;
}
bind(n);
});
}

function bindCallExpressionFlow(node: CallExpression) {
// If the target of the call expression is a function expression or arrow function we have
// an immediately invoked function expression (IIFE). Initialize the flowNode property to
Expand Down Expand Up @@ -1827,6 +1853,18 @@ namespace ts {
}
node.parent = parent;
const saveInStrictMode = inStrictMode;

// Even though in the AST the jsdoc @typedef node belongs to the current node,
// its symbol might be in the same scope with the current node's symbol. Consider:
//
// /** @typedef {string | number} MyType */
// function foo();
//
// Here the current node is "foo", which is a container, but the scope of "MyType" should
// not be inside "foo". Therefore we always bind @typedef before bind the parent node,
// and skip binding this tag later when binding all the other jsdoc tags.
bindJSDocTypedefTagIfAny(node);

// First we bind declaration nodes to a symbol if possible. We'll both create a symbol
// and then potentially add the symbol to an appropriate symbol table. Possible
// destination symbol tables are:
Expand Down Expand Up @@ -1861,6 +1899,27 @@ namespace ts {
inStrictMode = saveInStrictMode;
}

function bindJSDocTypedefTagIfAny(node: Node) {
if (!node.jsDoc) {
return;
}

for (const jsDoc of node.jsDoc) {
if (!jsDoc.tags) {
continue;
}

for (const tag of jsDoc.tags) {
if (tag.kind === SyntaxKind.JSDocTypedefTag) {
const savedParent = parent;
parent = jsDoc;
bind(tag);
parent = savedParent;
}
}
}
}

function updateStrictModeStatementList(statements: NodeArray<Statement>) {
if (!inStrictMode) {
for (const statement of statements) {
Expand Down
20 changes: 20 additions & 0 deletions tests/cases/fourslash/server/jsdocTypedefTag1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/// <reference path="../fourslash.ts"/>

// @allowNonTsExtensions: true
// @Filename: jsdocCompletion_typedef.js

//// /**
//// * @typedef {Object} MyType
//// * @property {string} yes
//// */
//// function foo() { }

//// /**
//// * @param {MyType} my
//// */
//// function a(my) {
//// my.yes./*1*/
//// }

goTo.marker('1');
verify.completionListContains('charAt');
30 changes: 30 additions & 0 deletions tests/cases/fourslash/server/jsdocTypedefTag2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/// <reference path="../fourslash.ts"/>

// @allowNonTsExtensions: true
// @Filename: jsdocCompletion_typedef.js

//// /**
//// * @typedef {Object} A.B.MyType
//// * @property {string} yes
//// */
//// function foo() {}

//// /**
//// * @param {A.B.MyType} my2
//// */
//// function a(my2) {
//// my2.yes./*1*/
//// }

//// /**
//// * @param {MyType} my2
//// */
//// function b(my2) {
//// my2.yes./*2*/
//// }


goTo.marker('1');
verify.completionListContains('charAt');
goTo.marker('2');
verify.not.completionListContains('charAt');