Skip to content

Fix expando handling in getTypeReferenceType #34712

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 2 commits into from
Oct 24, 2019
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
13 changes: 8 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2629,7 +2629,12 @@ namespace ts {
return undefined;
}
const init = isVariableDeclaration(decl) ? getDeclaredExpandoInitializer(decl) : getAssignedExpandoInitializer(decl);
return init && getSymbolOfNode(init) || undefined;
if (init) {
const initSymbol = getSymbolOfNode(init);
if (initSymbol) {
return mergeJSSymbols(initSymbol, symbol);
}
}
}


Expand Down Expand Up @@ -10766,9 +10771,7 @@ namespace ts {
&& isCallExpression(decl.initializer)
&& isRequireCall(decl.initializer, /*requireStringLiteralLikeArgument*/ true)
&& valueType.symbol;
const isImportType = node.kind === SyntaxKind.ImportType;
const isDelayedMergeClass = symbol !== valueType.symbol && getMergedSymbol(symbol) === valueType.symbol;
if (isRequireAlias || isImportType || isDelayedMergeClass) {
if (isRequireAlias || node.kind === SyntaxKind.ImportType) {
typeType = getTypeReferenceType(node, valueType.symbol);
}
}
Expand Down Expand Up @@ -24997,7 +25000,7 @@ namespace ts {
}

function mergeJSSymbols(target: Symbol, source: Symbol | undefined) {
if (source && (hasEntries(source.exports) || hasEntries(source.members))) {
Copy link
Member Author

Choose a reason for hiding this comment

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

turns out we need to merge just for the flags sometimes.

if (source) {
const links = getSymbolLinks(source);
if (!links.inferredClassSymbol || !links.inferredClassSymbol.has("" + getSymbolId(target))) {
const inferred = isTransientSymbol(target) ? target : cloneSymbol(target) as TransientSymbol;
Expand Down
45 changes: 21 additions & 24 deletions tests/baselines/reference/jsdocTypeReferenceToMergedClass.symbols
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
=== tests/cases/conformance/jsdoc/test.js ===
=== tests/cases/conformance/jsdoc/jsdocTypeReferenceToMergedClass.js ===
// https://github.com/microsoft/TypeScript/issues/34685

/** @param {Workspace.Project} p */
function demo(p) {
>demo : Symbol(demo, Decl(test.js, 0, 0))
>p : Symbol(p, Decl(test.js, 3, 14))

p.isServiceProject()
>p.isServiceProject : Symbol(isServiceProject, Decl(mod1.js, 3, 31))
>p : Symbol(p, Decl(test.js, 3, 14))
>isServiceProject : Symbol(isServiceProject, Decl(mod1.js, 3, 31))
}
=== tests/cases/conformance/jsdoc/mod1.js ===
// Note: mod1.js needs to appear second to trigger the bug
var Workspace = {}
>Workspace : Symbol(Workspace, Decl(mod1.js, 1, 3), Decl(mod1.js, 1, 18), Decl(mod1.js, 2, 37))
>Workspace : Symbol(Workspace, Decl(jsdocTypeReferenceToMergedClass.js, 2, 3), Decl(jsdocTypeReferenceToMergedClass.js, 5, 20), Decl(jsdocTypeReferenceToMergedClass.js, 7, 37))

/** @type {Workspace.Project} */
var p;
>p : Symbol(p, Decl(jsdocTypeReferenceToMergedClass.js, 4, 3))

p.isServiceProject()
>p.isServiceProject : Symbol(isServiceProject, Decl(jsdocTypeReferenceToMergedClass.js, 8, 31))
>p : Symbol(p, Decl(jsdocTypeReferenceToMergedClass.js, 4, 3))
>isServiceProject : Symbol(isServiceProject, Decl(jsdocTypeReferenceToMergedClass.js, 8, 31))

Workspace.Project = function wp() { }
>Workspace.Project : Symbol(Workspace.Project, Decl(mod1.js, 1, 18), Decl(mod1.js, 3, 10))
>Workspace : Symbol(Workspace, Decl(mod1.js, 1, 3), Decl(mod1.js, 1, 18), Decl(mod1.js, 2, 37))
>Project : Symbol(Workspace.Project, Decl(mod1.js, 1, 18), Decl(mod1.js, 3, 10))
>wp : Symbol(wp, Decl(mod1.js, 2, 19))
>Workspace.Project : Symbol(Workspace.Project, Decl(jsdocTypeReferenceToMergedClass.js, 5, 20), Decl(jsdocTypeReferenceToMergedClass.js, 8, 10))
>Workspace : Symbol(Workspace, Decl(jsdocTypeReferenceToMergedClass.js, 2, 3), Decl(jsdocTypeReferenceToMergedClass.js, 5, 20), Decl(jsdocTypeReferenceToMergedClass.js, 7, 37))
>Project : Symbol(Workspace.Project, Decl(jsdocTypeReferenceToMergedClass.js, 5, 20), Decl(jsdocTypeReferenceToMergedClass.js, 8, 10))
>wp : Symbol(wp, Decl(jsdocTypeReferenceToMergedClass.js, 7, 19))

Workspace.Project.prototype = {
>Workspace.Project.prototype : Symbol(Workspace.Project.prototype, Decl(mod1.js, 2, 37))
>Workspace.Project : Symbol(Workspace.Project, Decl(mod1.js, 1, 18), Decl(mod1.js, 3, 10))
>Workspace : Symbol(Workspace, Decl(mod1.js, 1, 3), Decl(mod1.js, 1, 18), Decl(mod1.js, 2, 37))
>Project : Symbol(Workspace.Project, Decl(mod1.js, 1, 18), Decl(mod1.js, 3, 10))
>prototype : Symbol(Workspace.Project.prototype, Decl(mod1.js, 2, 37))
>Workspace.Project.prototype : Symbol(Workspace.Project.prototype, Decl(jsdocTypeReferenceToMergedClass.js, 7, 37))
>Workspace.Project : Symbol(Workspace.Project, Decl(jsdocTypeReferenceToMergedClass.js, 5, 20), Decl(jsdocTypeReferenceToMergedClass.js, 8, 10))
>Workspace : Symbol(Workspace, Decl(jsdocTypeReferenceToMergedClass.js, 2, 3), Decl(jsdocTypeReferenceToMergedClass.js, 5, 20), Decl(jsdocTypeReferenceToMergedClass.js, 7, 37))
>Project : Symbol(Workspace.Project, Decl(jsdocTypeReferenceToMergedClass.js, 5, 20), Decl(jsdocTypeReferenceToMergedClass.js, 8, 10))
>prototype : Symbol(Workspace.Project.prototype, Decl(jsdocTypeReferenceToMergedClass.js, 7, 37))

isServiceProject() {}
>isServiceProject : Symbol(isServiceProject, Decl(mod1.js, 3, 31))
>isServiceProject : Symbol(isServiceProject, Decl(jsdocTypeReferenceToMergedClass.js, 8, 31))
}

19 changes: 8 additions & 11 deletions tests/baselines/reference/jsdocTypeReferenceToMergedClass.types
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
=== tests/cases/conformance/jsdoc/test.js ===
=== tests/cases/conformance/jsdoc/jsdocTypeReferenceToMergedClass.js ===
// https://github.com/microsoft/TypeScript/issues/34685

/** @param {Workspace.Project} p */
function demo(p) {
>demo : (p: wp) => void
var Workspace = {}
>Workspace : typeof Workspace
>{} : {}

/** @type {Workspace.Project} */
var p;
>p : wp

p.isServiceProject()
p.isServiceProject()
>p.isServiceProject() : void
>p.isServiceProject : () => void
>p : wp
>isServiceProject : () => void
}
=== tests/cases/conformance/jsdoc/mod1.js ===
// Note: mod1.js needs to appear second to trigger the bug
var Workspace = {}
>Workspace : typeof Workspace
>{} : {}

Workspace.Project = function wp() { }
>Workspace.Project = function wp() { } : typeof wp
Expand Down
12 changes: 5 additions & 7 deletions tests/cases/conformance/jsdoc/jsdocTypeReferenceToMergedClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
// @allowJs: true
// @checkJs: true

// @Filename: test.js
Copy link
Member Author

Choose a reason for hiding this comment

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

this is now a single-file repro. It is still written in such a way that function wp is checked after p.isServiceProject.

/** @param {Workspace.Project} p */
function demo(p) {
p.isServiceProject()
}
// @Filename: mod1.js
// Note: mod1.js needs to appear second to trigger the bug
// @Filename: jsdocTypeReferenceToMergedClass.js
var Workspace = {}
/** @type {Workspace.Project} */
var p;
p.isServiceProject()

Workspace.Project = function wp() { }
Workspace.Project.prototype = {
isServiceProject() {}
Expand Down