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

Fixed instantiation expression type param leak in body-less arrows #61054

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
reuse some of the existing logic
Andarist committed Jan 27, 2025
commit 4069c6bdaf8bb8831d242d6e9bc21d5d7f0d11c9
36 changes: 22 additions & 14 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
@@ -20254,20 +20254,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
case SyntaxKind.Identifier:
return !tp.isThisType && isPartOfTypeNode(node) && maybeTypeParameterReference(node) &&
getTypeFromTypeNodeWorker(node as TypeNode) === tp; // use worker because we're looking for === equality
case SyntaxKind.TypeQuery:
const entityName = (node as TypeQueryNode).exprName;
const firstIdentifier = getFirstIdentifier(entityName);
if (!isThisIdentifier(firstIdentifier)) { // Don't attempt to analyze typeof this.xxx
const firstIdentifierSymbol = getResolvedSymbol(firstIdentifier);
const tpScope = getTypeParameterScope();
if (firstIdentifierSymbol.declarations && tpScope) {
return some(firstIdentifierSymbol.declarations, idDecl => isNodeDescendantOf(idDecl, tpScope)) ||
some((node as TypeQueryNode).typeArguments, containsReference);
}
}
return true;
case SyntaxKind.ExpressionWithTypeArguments:
return isNodeDescendantOf(node, getTypeParameterScope());
case SyntaxKind.TypeQuery: {
const { exprName, typeArguments } = node as TypeQueryNode;
return entityNameWithTypeArgumentsContainsReference(exprName, typeArguments);
}
case SyntaxKind.ExpressionWithTypeArguments: {
const { expression, typeArguments } = node as ExpressionWithTypeArguments;
return isEntityName(expression) ? entityNameWithTypeArgumentsContainsReference(expression, typeArguments) : isNodeDescendantOf(node, getTypeParameterScope());
}
case SyntaxKind.MethodDeclaration:
case SyntaxKind.MethodSignature:
return !(node as FunctionLikeDeclaration).type && !!(node as FunctionLikeDeclaration).body ||
@@ -20278,6 +20272,20 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return !!forEachChild(node, containsReference);
}

function entityNameWithTypeArgumentsContainsReference(entityName: EntityName, typeArguments: NodeArray<TypeNode> | undefined) {
const firstIdentifier = getFirstIdentifier(entityName);
if (isThisIdentifier(firstIdentifier)) { // Don't attempt to analyze typeof this.xxx
return true;
}
const firstIdentifierSymbol = getResolvedSymbol(firstIdentifier);
const tpScope = getTypeParameterScope();
if (!firstIdentifierSymbol.declarations || !tpScope) {
return true;
}
return some(firstIdentifierSymbol.declarations, idDecl => isNodeDescendantOf(idDecl, tpScope)) ||
some(typeArguments, containsReference);
}

function getTypeParameterScope() {
const tpDeclaration = tp.symbol.declarations![0]; // There is exactly one declaration, otherwise `containsReference` is not called
return tpDeclaration.kind === SyntaxKind.TypeParameter ? tpDeclaration.parent : // Type parameter is a regular type parameter, e.g. foo<T>