Skip to content

Commit c344a3e

Browse files
authored
Fix bogus use before def in jsdoc (microsoft#25532)
Block scoped variables, classes and enums would issue a bogus use-before-def error in jsdoc because name resolution always adds Value, even when resolving a type. Fixes microsoft#25097
1 parent 60c0dfe commit c344a3e

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

src/compiler/checker.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,7 @@ namespace ts {
10591059
// 5. inside a TS export= declaration (since we will move the export statement during emit to avoid TDZ)
10601060
// or if usage is in a type context:
10611061
// 1. inside a type query (typeof in type position)
1062+
// 2. inside a jsdoc comment
10621063
if (usage.parent.kind === SyntaxKind.ExportSpecifier || (usage.parent.kind === SyntaxKind.ExportAssignment && (usage.parent as ExportAssignment).isExportEquals)) {
10631064
// export specifiers do not use the variable, they only make it available for use
10641065
return true;
@@ -1069,7 +1070,7 @@ namespace ts {
10691070
}
10701071

10711072
const container = getEnclosingBlockScopeContainer(declaration);
1072-
return isInTypeQuery(usage) || isUsedInFunctionOrInstanceProperty(usage, declaration, container);
1073+
return !!(usage.flags & NodeFlags.JSDoc) || isInTypeQuery(usage) || isUsedInFunctionOrInstanceProperty(usage, declaration, container);
10731074

10741075
function isImmediatelyUsedInInitializerOfBlockScopedVariable(declaration: VariableDeclaration, usage: Node): boolean {
10751076
const container = getEnclosingBlockScopeContainer(declaration);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
=== tests/cases/conformance/jsdoc/bug25097.js ===
2+
/** @type {C | null} */
3+
const c = null
4+
>c : Symbol(c, Decl(bug25097.js, 1, 5))
5+
6+
class C {
7+
>C : Symbol(C, Decl(bug25097.js, 1, 14))
8+
}
9+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=== tests/cases/conformance/jsdoc/bug25097.js ===
2+
/** @type {C | null} */
3+
const c = null
4+
>c : C
5+
>null : null
6+
7+
class C {
8+
>C : C
9+
}
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @noEmit: true
2+
// @allowJs: true
3+
// @checkJs: true
4+
// @Filename: bug25097.js
5+
/** @type {C | null} */
6+
const c = null
7+
class C {
8+
}

0 commit comments

Comments
 (0)