Skip to content
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

Use proper type parameter hosts in JS files #61013

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
32 changes: 20 additions & 12 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,6 @@ import {
getDirectoryPath,
getEffectiveBaseTypeNode,
getEffectiveConstraintOfTypeParameter,
getEffectiveContainerForJSDocTemplateTag,
getEffectiveImplementsTypeNodes,
getEffectiveInitializer,
getEffectiveJSDocHost,
Expand Down Expand Up @@ -16217,9 +16216,21 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return typeParameter.constraint === noConstraintType ? undefined : typeParameter.constraint;
}

function getEffectiveTypeParameterHost(typeParameter: TypeParameter) {
const declarations = typeParameter.symbol.declarations;
if (!declarations || declarations.length !== 1) {
Copy link
Member

Choose a reason for hiding this comment

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

This sort of makes me wonder when we really should be using getEffectiveJSDocHost where we are currently using getEffectiveContainerForJSDocTemplateTag...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, but to be fair - I had to use getEffectiveJSDocHost to fix this issue at hand and it didn't look to me like this older function would have to stick to the other one. There is a chance that it should not use this - but there is also a chance that this change fixed some other issue ;p

In the ideal world, I'd go through all of the callers to both and assess their needs but I just don't have time to do that right now 😢

return;
}
const tpDeclaration = getDeclarationOfKind<TypeParameterDeclaration>(typeParameter.symbol, SyntaxKind.TypeParameter);
if (!tpDeclaration) {
// Type parameter is the this type, and its declaration is the class declaration.
return typeParameter.isThisType ? declarations[0].parent : undefined;
}
return isJSDocTemplateTag(tpDeclaration.parent) ? getEffectiveJSDocHost(tpDeclaration.parent) : tpDeclaration.parent;
}

function getParentSymbolOfTypeParameter(typeParameter: TypeParameter): Symbol | undefined {
const tp = getDeclarationOfKind<TypeParameterDeclaration>(typeParameter.symbol, SyntaxKind.TypeParameter)!;
const host = isJSDocTemplateTag(tp.parent) ? getEffectiveContainerForJSDocTemplateTag(tp.parent) : tp.parent;
const host = getEffectiveTypeParameterHost(typeParameter);
return host && getSymbolOfNode(host);
}

Expand Down Expand Up @@ -20190,7 +20201,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// type parameter, or if the node contains type queries that we can't prove couldn't contain references to the type parameter,
// we consider the type parameter possibly referenced.
if (tp.symbol && tp.symbol.declarations && tp.symbol.declarations.length === 1) {
const container = tp.symbol.declarations[0].parent;
const container = getEffectiveTypeParameterHost(tp);
for (let n = node; n !== container; n = n.parent) {
if (!n || n.kind === SyntaxKind.Block || n.kind === SyntaxKind.ConditionalType && forEachChild((n as ConditionalTypeNode).extendsType, containsReference)) {
return true;
Expand All @@ -20211,12 +20222,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const firstIdentifier = getFirstIdentifier(entityName);
if (!isThisIdentifier(firstIdentifier)) { // Don't attempt to analyze typeof this.xxx
const firstIdentifierSymbol = getResolvedSymbol(firstIdentifier);
const tpDeclaration = tp.symbol.declarations![0]; // There is exactly one declaration, otherwise `containsReference` is not called
const tpScope = tpDeclaration.kind === SyntaxKind.TypeParameter ? tpDeclaration.parent : // Type parameter is a regular type parameter, e.g. foo<T>
tp.isThisType ? tpDeclaration : // Type parameter is the this type, and its declaration is the class declaration.
undefined; // Type parameter's declaration was unrecognized, e.g. comes from JSDoc annotation.
if (firstIdentifierSymbol.declarations && tpScope) {
return some(firstIdentifierSymbol.declarations, idDecl => isNodeDescendantOf(idDecl, tpScope)) ||
const tpHost = getEffectiveTypeParameterHost(tp);
if (firstIdentifierSymbol.declarations && tpHost) {
return some(firstIdentifierSymbol.declarations, idDecl => isNodeDescendantOf(idDecl, tpHost)) ||
some((node as TypeQueryNode).typeArguments, containsReference);
}
}
Expand Down Expand Up @@ -36138,8 +36146,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (candidate.typeParameters) {
// If we are *inside the body of candidate*, we need to create a clone of `candidate` with differing type parameter identities,
// so our inference results for this call doesn't pollute expression types referencing the outer type parameter!
const paramLocation = candidate.typeParameters[0].symbol.declarations?.[0]?.parent;
const candidateParameterContext = paramLocation || (candidate.declaration && isConstructorDeclaration(candidate.declaration) ? candidate.declaration.parent : candidate.declaration);
const paramHost = getEffectiveTypeParameterHost(candidate.typeParameters[0]);
const candidateParameterContext = paramHost || (candidate.declaration && isConstructorDeclaration(candidate.declaration) ? candidate.declaration.parent : candidate.declaration);
if (candidateParameterContext && findAncestor(node, a => a === candidateParameterContext)) {
candidate = getImplementationSignature(candidate);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//// [tests/cases/compiler/inferFromReturnOfContextSensitiveFnJsDoc1.ts] ////

=== index.js ===
/**
* @template S
* @param {(arg0: { observer: EO }) => S} callback
* @param {Options} [options]
* @returns {VC<S>}
*/
/*
* @type { <S>(fn: (arg0: { observer: EO; }) => S, options?: Options) => VC<S> }
*/
function define(callback, options) {
>define : Symbol(define, Decl(index.js, 0, 0))
>callback : Symbol(callback, Decl(index.js, 9, 16))
>options : Symbol(options, Decl(index.js, 9, 25))

const { name } = options ?? {};
>name : Symbol(name, Decl(index.js, 10, 9))
>options : Symbol(options, Decl(index.js, 9, 25))

const observer = new EO();
>observer : Symbol(observer, Decl(index.js, 11, 7))
>EO : Symbol(EO, Decl(index.js, 28, 1))

const state = callback({ observer });
>state : Symbol(state, Decl(index.js, 12, 7))
>callback : Symbol(callback, Decl(index.js, 9, 16))
>observer : Symbol(observer, Decl(index.js, 12, 26))

return new VC(state);
>VC : Symbol(VC, Decl(index.js, 14, 1))
>state : Symbol(state, Decl(index.js, 12, 7))
}

/**
* @template S
*/
class VC {
>VC : Symbol(VC, Decl(index.js, 14, 1))

/** @type {S} */
state;
>state : Symbol(VC.state, Decl(index.js, 19, 10))

/**
* @param {S} state
*/
constructor(state) {
>state : Symbol(state, Decl(index.js, 25, 14))

this.state = state;
>this.state : Symbol(VC.state, Decl(index.js, 19, 10))
>this : Symbol(VC, Decl(index.js, 14, 1))
>state : Symbol(VC.state, Decl(index.js, 19, 10))
>state : Symbol(state, Decl(index.js, 25, 14))
}
}

/** @typedef {{ name?: string }} Options */

class EO {}
>EO : Symbol(EO, Decl(index.js, 28, 1))

const v1 = define((arg0) => true, { name: "default" });
>v1 : Symbol(v1, Decl(index.js, 34, 5))
>define : Symbol(define, Decl(index.js, 0, 0))
>arg0 : Symbol(arg0, Decl(index.js, 34, 19))
>name : Symbol(name, Decl(index.js, 34, 35))

Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
//// [tests/cases/compiler/inferFromReturnOfContextSensitiveFnJsDoc1.ts] ////

=== index.js ===
/**
* @template S
* @param {(arg0: { observer: EO }) => S} callback
* @param {Options} [options]
* @returns {VC<S>}
*/
/*
* @type { <S>(fn: (arg0: { observer: EO; }) => S, options?: Options) => VC<S> }
*/
function define(callback, options) {
>define : <S>(callback: (arg0: { observer: EO; }) => S, options?: Options) => VC<S>
> : ^ ^^ ^^ ^^ ^^^ ^^^^^
>callback : (arg0: { observer: EO; }) => S
> : ^ ^^ ^^^^^
>options : Options | undefined
> : ^^^^^^^^^^^^^^^^^^^

const { name } = options ?? {};
>name : string | undefined
> : ^^^^^^^^^^^^^^^^^^
>options ?? {} : Options
> : ^^^^^^^
>options : Options | undefined
> : ^^^^^^^^^^^^^^^^^^^
>{} : {}
> : ^^

const observer = new EO();
>observer : EO
> : ^^
>new EO() : EO
> : ^^
>EO : typeof EO
> : ^^^^^^^^^

const state = callback({ observer });
>state : S
> : ^
>callback({ observer }) : S
> : ^
>callback : (arg0: { observer: EO; }) => S
> : ^ ^^ ^^^^^
>{ observer } : { observer: EO; }
> : ^^^^^^^^^^^^^^^^^
>observer : EO
> : ^^

return new VC(state);
>new VC(state) : VC<S>
> : ^^^^^
>VC : typeof VC
> : ^^^^^^^^^
>state : S
> : ^
}

/**
* @template S
*/
class VC {
>VC : VC<S>
> : ^^^^^

/** @type {S} */
state;
>state : S
> : ^

/**
* @param {S} state
*/
constructor(state) {
>state : S
> : ^

this.state = state;
>this.state = state : S
> : ^
>this.state : S
> : ^
>this : this
> : ^^^^
>state : S
> : ^
>state : S
> : ^
}
}

/** @typedef {{ name?: string }} Options */

class EO {}
>EO : EO
> : ^^

const v1 = define((arg0) => true, { name: "default" });
>v1 : VC<boolean>
> : ^^^^^^^^^^^
>define((arg0) => true, { name: "default" }) : VC<boolean>
> : ^^^^^^^^^^^
>define : <S>(callback: (arg0: { observer: EO; }) => S, options?: Options) => VC<S>
> : ^ ^^ ^^ ^^ ^^^ ^^^^^
>(arg0) => true : (arg0: { observer: EO; }) => boolean
> : ^ ^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^
>arg0 : { observer: EO; }
> : ^^^^^^^^^^^^ ^^^
>true : true
> : ^^^^
>{ name: "default" } : { name: string; }
> : ^^^^^^^^^^^^^^^^^
>name : string
> : ^^^^^^
>"default" : "default"
> : ^^^^^^^^^

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//// [tests/cases/compiler/inferenceOuterResultNotIncorrectlyInstantiatedWithInnerResult2.ts] ////

=== inferenceOuterResultNotIncorrectlyInstantiatedWithInnerResult2.ts ===
class S<T> {
>S : Symbol(S, Decl(inferenceOuterResultNotIncorrectlyInstantiatedWithInnerResult2.ts, 0, 0))
>T : Symbol(T, Decl(inferenceOuterResultNotIncorrectlyInstantiatedWithInnerResult2.ts, 0, 8))

set: Set<T>;
>set : Symbol(S.set, Decl(inferenceOuterResultNotIncorrectlyInstantiatedWithInnerResult2.ts, 0, 12))
>Set : Symbol(Set, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.esnext.collection.d.ts, --, --))
>T : Symbol(T, Decl(inferenceOuterResultNotIncorrectlyInstantiatedWithInnerResult2.ts, 0, 8))

constructor(set: Set<T>) {
>set : Symbol(set, Decl(inferenceOuterResultNotIncorrectlyInstantiatedWithInnerResult2.ts, 3, 14))
>Set : Symbol(Set, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.esnext.collection.d.ts, --, --))
>T : Symbol(T, Decl(inferenceOuterResultNotIncorrectlyInstantiatedWithInnerResult2.ts, 0, 8))

this.set = set;
>this.set : Symbol(S.set, Decl(inferenceOuterResultNotIncorrectlyInstantiatedWithInnerResult2.ts, 0, 12))
>this : Symbol(S, Decl(inferenceOuterResultNotIncorrectlyInstantiatedWithInnerResult2.ts, 0, 0))
>set : Symbol(S.set, Decl(inferenceOuterResultNotIncorrectlyInstantiatedWithInnerResult2.ts, 0, 12))
>set : Symbol(set, Decl(inferenceOuterResultNotIncorrectlyInstantiatedWithInnerResult2.ts, 3, 14))
}

array() {
>array : Symbol(S.array, Decl(inferenceOuterResultNotIncorrectlyInstantiatedWithInnerResult2.ts, 5, 3))

return new S(new Set([...this.set].map((item) => [item])));
>S : Symbol(S, Decl(inferenceOuterResultNotIncorrectlyInstantiatedWithInnerResult2.ts, 0, 0))
>Set : Symbol(Set, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.esnext.collection.d.ts, --, --))
>[...this.set].map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --))
>this.set : Symbol(S.set, Decl(inferenceOuterResultNotIncorrectlyInstantiatedWithInnerResult2.ts, 0, 12))
>this : Symbol(S, Decl(inferenceOuterResultNotIncorrectlyInstantiatedWithInnerResult2.ts, 0, 0))
>set : Symbol(S.set, Decl(inferenceOuterResultNotIncorrectlyInstantiatedWithInnerResult2.ts, 0, 12))
>map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --))
>item : Symbol(item, Decl(inferenceOuterResultNotIncorrectlyInstantiatedWithInnerResult2.ts, 8, 44))
>item : Symbol(item, Decl(inferenceOuterResultNotIncorrectlyInstantiatedWithInnerResult2.ts, 8, 44))
}
}

function sArray<T>(set: Set<T>) {
>sArray : Symbol(sArray, Decl(inferenceOuterResultNotIncorrectlyInstantiatedWithInnerResult2.ts, 10, 1))
>T : Symbol(T, Decl(inferenceOuterResultNotIncorrectlyInstantiatedWithInnerResult2.ts, 12, 16))
>set : Symbol(set, Decl(inferenceOuterResultNotIncorrectlyInstantiatedWithInnerResult2.ts, 12, 19))
>Set : Symbol(Set, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.esnext.collection.d.ts, --, --))
>T : Symbol(T, Decl(inferenceOuterResultNotIncorrectlyInstantiatedWithInnerResult2.ts, 12, 16))

return new S(new Set([...set].map((item) => [item])));
>S : Symbol(S, Decl(inferenceOuterResultNotIncorrectlyInstantiatedWithInnerResult2.ts, 0, 0))
>Set : Symbol(Set, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.esnext.collection.d.ts, --, --))
>[...set].map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --))
>set : Symbol(set, Decl(inferenceOuterResultNotIncorrectlyInstantiatedWithInnerResult2.ts, 12, 19))
>map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --))
>item : Symbol(item, Decl(inferenceOuterResultNotIncorrectlyInstantiatedWithInnerResult2.ts, 13, 37))
>item : Symbol(item, Decl(inferenceOuterResultNotIncorrectlyInstantiatedWithInnerResult2.ts, 13, 37))
}

Loading