Skip to content

Commit 056a656

Browse files
committed
Restore delayed merge check to getTypeFromJSDocValueReference
This is needed when a function merges with a prototype assignment. The resulting *merged* symbol is a constructor function marked with SymbolFlags.Class. However, the merge doesn't happen until getTypeOfFuncClassEnumModule is called, which, in the getTypeReferenceType code path, doesn't happen until getTypeFromJSDocValueReference. That means the check for SymbolFlags.Class is missed. Previously, getTypeFromJSDocValueReference had a weird check `symbol !== getTypeOfSymbol(symbol).symbol`, which, if true, ran getTypeReferenceType again on `getTypeOfSymbol(symbol).symbol`. For JS "aliases", this had the effect of dereferencing the alias, and for function-prototype merges, this had the effect of ... just trying again after the merge had happened. This is a confusing way to run things. getTypeReferenceType should instead detect a function-prototype merge, cause it to happen, and *then* run the rest of its code instead of relying on try-again logic at the very end. However, for the RC, I want to fix this code by restoring the old check, with an additional check to make sure that #33106 doesn't break again: ```ts const valueType = getTypeOfSymbol(symbol) symbol !== valueType.symbol && getMergedSymbol(symbol) === valueType.symbol ``` I'll work on the real fix afterwards and put it into 3.8.
1 parent 07d3a2e commit 056a656

File tree

4 files changed

+91
-1
lines changed

4 files changed

+91
-1
lines changed

src/compiler/checker.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -10766,7 +10766,9 @@ namespace ts {
1076610766
&& isCallExpression(decl.initializer)
1076710767
&& isRequireCall(decl.initializer, /*requireStringLiteralLikeArgument*/ true)
1076810768
&& valueType.symbol;
10769-
if (isRequireAlias || node.kind === SyntaxKind.ImportType) {
10769+
const isImportType = node.kind === SyntaxKind.ImportType;
10770+
const isDelayedMergeClass = symbol !== valueType.symbol && getMergedSymbol(symbol) === valueType.symbol;
10771+
if (isRequireAlias || isImportType || isDelayedMergeClass) {
1077010772
typeType = getTypeReferenceType(node, valueType.symbol);
1077110773
}
1077210774
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
=== tests/cases/conformance/jsdoc/test.js ===
2+
/** @param {Workspace.Project} p */
3+
function demo(p) {
4+
>demo : Symbol(demo, Decl(test.js, 0, 0))
5+
>p : Symbol(p, Decl(test.js, 1, 14))
6+
7+
p.isServiceProject()
8+
>p.isServiceProject : Symbol(isServiceProject, Decl(mod1.js, 3, 31))
9+
>p : Symbol(p, Decl(test.js, 1, 14))
10+
>isServiceProject : Symbol(isServiceProject, Decl(mod1.js, 3, 31))
11+
}
12+
=== tests/cases/conformance/jsdoc/mod1.js ===
13+
// Note: mod1.js needs to appear second to trigger the bug
14+
var Workspace = {}
15+
>Workspace : Symbol(Workspace, Decl(mod1.js, 1, 3), Decl(mod1.js, 1, 18), Decl(mod1.js, 2, 37))
16+
17+
Workspace.Project = function wp() { }
18+
>Workspace.Project : Symbol(Workspace.Project, Decl(mod1.js, 1, 18), Decl(mod1.js, 3, 10))
19+
>Workspace : Symbol(Workspace, Decl(mod1.js, 1, 3), Decl(mod1.js, 1, 18), Decl(mod1.js, 2, 37))
20+
>Project : Symbol(Workspace.Project, Decl(mod1.js, 1, 18), Decl(mod1.js, 3, 10))
21+
>wp : Symbol(wp, Decl(mod1.js, 2, 19))
22+
23+
Workspace.Project.prototype = {
24+
>Workspace.Project.prototype : Symbol(Workspace.Project.prototype, Decl(mod1.js, 2, 37))
25+
>Workspace.Project : Symbol(Workspace.Project, Decl(mod1.js, 1, 18), Decl(mod1.js, 3, 10))
26+
>Workspace : Symbol(Workspace, Decl(mod1.js, 1, 3), Decl(mod1.js, 1, 18), Decl(mod1.js, 2, 37))
27+
>Project : Symbol(Workspace.Project, Decl(mod1.js, 1, 18), Decl(mod1.js, 3, 10))
28+
>prototype : Symbol(Workspace.Project.prototype, Decl(mod1.js, 2, 37))
29+
30+
isServiceProject() {}
31+
>isServiceProject : Symbol(isServiceProject, Decl(mod1.js, 3, 31))
32+
}
33+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
=== tests/cases/conformance/jsdoc/test.js ===
2+
/** @param {Workspace.Project} p */
3+
function demo(p) {
4+
>demo : (p: wp) => void
5+
>p : wp
6+
7+
p.isServiceProject()
8+
>p.isServiceProject() : void
9+
>p.isServiceProject : () => void
10+
>p : wp
11+
>isServiceProject : () => void
12+
}
13+
=== tests/cases/conformance/jsdoc/mod1.js ===
14+
// Note: mod1.js needs to appear second to trigger the bug
15+
var Workspace = {}
16+
>Workspace : typeof Workspace
17+
>{} : {}
18+
19+
Workspace.Project = function wp() { }
20+
>Workspace.Project = function wp() { } : typeof wp
21+
>Workspace.Project : typeof wp
22+
>Workspace : typeof Workspace
23+
>Project : typeof wp
24+
>function wp() { } : typeof wp
25+
>wp : typeof wp
26+
27+
Workspace.Project.prototype = {
28+
>Workspace.Project.prototype = { isServiceProject() {}} : { isServiceProject(): void; }
29+
>Workspace.Project.prototype : { isServiceProject(): void; }
30+
>Workspace.Project : typeof wp
31+
>Workspace : typeof Workspace
32+
>Project : typeof wp
33+
>prototype : { isServiceProject(): void; }
34+
>{ isServiceProject() {}} : { isServiceProject(): void; }
35+
36+
isServiceProject() {}
37+
>isServiceProject : () => void
38+
}
39+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// @noEmit: true
2+
// @allowJs: true
3+
// @checkJs: true
4+
5+
// @Filename: test.js
6+
/** @param {Workspace.Project} p */
7+
function demo(p) {
8+
p.isServiceProject()
9+
}
10+
// @Filename: mod1.js
11+
// Note: mod1.js needs to appear second to trigger the bug
12+
var Workspace = {}
13+
Workspace.Project = function wp() { }
14+
Workspace.Project.prototype = {
15+
isServiceProject() {}
16+
}

0 commit comments

Comments
 (0)