Skip to content

Bind object type literals in JSDoc #51066

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

Closed
wants to merge 4 commits into from
Closed
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
14 changes: 12 additions & 2 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2456,13 +2456,23 @@ namespace ts {
}
else {
for (const j of node.jsDoc!) {
setParent(j, node);
setParentRecursive(j, /*incremental*/ false);
setParentsAndBindAnonymousJSDocTypes(j);
}
}
}
}

function setParentsAndBindAnonymousJSDocTypes(node: Node) {
setParent(node, parent);
const saveParent = parent;
parent = node;
if (node.kind === SyntaxKind.TypeLiteral) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

won't this also bind type literals inside a @typedef? Maybe it won't matter because there's some required intermediate binding needed for the checker to check the type.

bindWorker(node);
}
forEachChild(node, setParentsAndBindAnonymousJSDocTypes);
parent = saveParent;
}

function updateStrictModeStatementList(statements: NodeArray<Statement>) {
if (!inStrictMode) {
for (const statement of statements) {
Expand Down
2 changes: 0 additions & 2 deletions src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -790,8 +790,6 @@ namespace ts {
}

export function getContextualTypeFromParentOrAncestorTypeNode(node: Expression, checker: TypeChecker): Type | undefined {
if (node.flags & (NodeFlags.JSDoc & ~NodeFlags.JavaScriptFile)) return undefined;

const contextualType = getContextualTypeFromParent(node, checker);
if (contextualType) return contextualType;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// === /a.ts ===
// /**
// * @type {"[|foo|]" | "bar"}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think find-all-refs should ignore @type in ts files and not offer completions either.

// */
// let x = "[|foo|]"/*FIND ALL REFS*/

[
{
"definition": {
"containerKind": "",
"containerName": "",
"fileName": "/a.ts",
"kind": "var",
"name": "foo",
"textSpan": {
"start": 42,
"length": 3
},
"displayParts": [
{
"text": "\"foo\"",
"kind": "stringLiteral"
}
]
},
"references": [
{
"textSpan": {
"start": 15,
"length": 3
},
"fileName": "/a.ts",
"isWriteAccess": false,
"isInString": true
},
{
"textSpan": {
"start": 42,
"length": 3
},
"fileName": "/a.ts",
"isWriteAccess": false,
"isInString": true
}
]
}
]
15 changes: 15 additions & 0 deletions tests/cases/fourslash/completionsAtJsDoc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// <reference path="fourslash.ts" />

////interface Foo<T> { }
/////**
//// * @type {Foo<{/**/}>}
//// */

verify.completions({
marker: "",
exact: {
name: "readonly",
sortText: completion.SortText.GlobalsOrKeywords
},
isNewIdentifierLocation: true,
});
9 changes: 9 additions & 0 deletions tests/cases/fourslash/findAllRefsForStringLiteral2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// <reference path="fourslash.ts" />

// @filename: /a.ts
/////**
//// * @type {"foo" | "bar"}
//// */
////let x = "foo"/**/

verify.baselineFindAllReferences("");