fix(47582): skip extraction if the type node is in the range of the type parameter declaration (#47596)

This commit is contained in:
Oleksandr T
2022-01-28 22:19:48 +02:00
committed by GitHub
parent eb3e7bd9d9
commit 2d501b1d98
5 changed files with 55 additions and 5 deletions

View File

@@ -144,11 +144,20 @@ namespace ts.refactor {
function visitor(node: Node): true | undefined {
if (isTypeReferenceNode(node)) {
if (isIdentifier(node.typeName)) {
const symbol = checker.resolveName(node.typeName.text, node.typeName, SymbolFlags.TypeParameter, /* excludeGlobals */ true);
const declaration = tryCast(symbol?.declarations?.[0], isTypeParameterDeclaration);
if (declaration) {
if (rangeContainsSkipTrivia(statement, declaration, file) && !rangeContainsSkipTrivia(selection, declaration, file)) {
pushIfUnique(result, declaration);
const typeName = node.typeName;
const symbol = checker.resolveName(typeName.text, typeName, SymbolFlags.TypeParameter, /* excludeGlobals */ true);
for (const decl of symbol?.declarations || emptyArray) {
if (isTypeParameterDeclaration(decl) && decl.getSourceFile() === file) {
// skip extraction if the type node is in the range of the type parameter declaration.
// function foo<T extends { a?: /**/T }>(): void;
if (decl.name.escapedText === typeName.escapedText && rangeContainsSkipTrivia(decl, selection, file)) {
return true;
}
if (rangeContainsSkipTrivia(statement, decl, file) && !rangeContainsSkipTrivia(selection, decl, file)) {
pushIfUnique(result, decl);
break;
}
}
}
}