Skip to content

Stop an infinite loop when using an exported JSDoc function as a type #38332

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 2 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
11 changes: 9 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11616,9 +11616,16 @@ namespace ts {
}
isRequireAlias = isCallExpression(expr) && isRequireCall(expr, /*requireStringLiteralLikeArgument*/ true) && !!valueType.symbol;
}
const isImportTypeWithQualifier = node.kind === SyntaxKind.ImportType && (node as ImportTypeNode).qualifier;

// We need to do an extra hop to find the correct type when type being referred is a class, because
// a class and its type are interwoven in the type, see jsdocImportTypeReferenceToClassAlias for
// an example of it in action
const isImportOfClassTypeWithQualifier = node.kind === SyntaxKind.ImportType
&& (node as ImportTypeNode).qualifier
&& valueType.symbol.flags & SymbolFlags.Class;
// valueType might not have a symbol, eg, {import('./b').STRING_LITERAL}
if (valueType.symbol && (isRequireAlias || isImportTypeWithQualifier)) {
if (valueType.symbol && (isRequireAlias || isImportOfClassTypeWithQualifier)) {
// follow fake alias (commonjs or class value->type)
typeType = getTypeReferenceType(node, valueType.symbol);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
=== tests/cases/compiler/lib/index.js ===
// 36830
//

/** @type {import('../folder/index').zzz} */
export function xxx() {
>xxx : Symbol(xxx, Decl(index.js, 0, 0))

return null;
}

=== tests/cases/compiler/folder/index.d.ts ===
export function zzz(): string;
>zzz : Symbol(zzz, Decl(index.d.ts, 0, 0))

Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ function demo(c) {
>c : Symbol(c, Decl(test.js, 2, 14))

c.s
>c.s : Symbol(C.s, Decl(mod1.js, 0, 9))
>c : Symbol(c, Decl(test.js, 2, 14))
>s : Symbol(C.s, Decl(mod1.js, 0, 9))
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ module.exports.C = C
/** @param {X} c */
function demo(c) {
>demo : (c: X) => void
>c : C
>c : typeof C

c.s
>c.s : () => void
>c : C
>s : () => void
>c.s : any
Copy link
Member

Choose a reason for hiding this comment

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

this seems wrong, which means that this fix may also need to be restricted to types that come through require

>c : typeof C
>s : any
}

14 changes: 14 additions & 0 deletions tests/cases/compiler/jsDocImplicitTypeOfForValueImport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 36830
//
// @allowJs: true
// @noEmit: true
// @checkJs: true

// @filename: lib/index.js
/** @type {import('../folder/index').zzz} */
export function xxx() {
return null;
}

// @filename: folder/index.d.ts
export function zzz(): string;
2 changes: 1 addition & 1 deletion tests/cases/fourslash/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

//---------------------------------------
// For API editors:
// When editting this file, and only while editing this file, enable the reference comments
// When editing this file, and only while editing this file, enable the reference comments
// and comment out the declarations in this section to get proper type information.
// Undo these changes before compiling/committing/editing any other fourslash tests.
// The test suite will likely crash if you try 'jake runtests' with reference comments enabled.
Expand Down