Skip to content

Commit faeb858

Browse files
committed
fix(47582): skip extraction if the type node is in the range of the type parameter declaration
1 parent a9efe2b commit faeb858

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

src/services/refactors/extractType.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,13 @@ namespace ts.refactor {
145145
if (isTypeReferenceNode(node)) {
146146
if (isIdentifier(node.typeName)) {
147147
const symbol = checker.resolveName(node.typeName.text, node.typeName, SymbolFlags.TypeParameter, /* excludeGlobals */ true);
148-
const declaration = tryCast(symbol?.declarations?.[0], isTypeParameterDeclaration);
148+
const declaration = firstDefined(symbol?.declarations, d => isTypeParameterDeclaration(d) && d.getSourceFile() === file ? d : undefined);
149149
if (declaration) {
150+
if (declaration.name.escapedText === node.typeName.escapedText && rangeContainsSkipTrivia(declaration, selection, file)) {
151+
// skip extraction if the type node is in the range of the type parameter declaration.
152+
// function foo<T extends { a?: /**/T }>(): void;
153+
return true;
154+
}
150155
if (rangeContainsSkipTrivia(statement, declaration, file) && !rangeContainsSkipTrivia(selection, declaration, file)) {
151156
pushIfUnique(result, declaration);
152157
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////export declare function foo<T extends { a?: /*a*/T/*b*/ }>(): void;
4+
5+
goTo.select("a", "b");
6+
verify.not.refactorAvailable("Extract type", "Extract to type alias");
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: a.ts
4+
////interface Foo<T extends { prop: T }> {}
5+
6+
// @Filename: b.ts
7+
////interface Foo<T extends { prop: /*a*/T/*b*/ }> {}
8+
9+
goTo.file("b.ts");
10+
goTo.select("a", "b");
11+
verify.not.refactorAvailable("Extract type", "Extract to type alias");
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: a.ts
4+
//// interface Foo<T extends { prop: [|T|] }> {}
5+
6+
// @Filename: b.ts
7+
//// // Some initial comments.
8+
//// // We need to ensure these files have different contents,
9+
//// // so their ranges differ, so we'll start a few lines below in this file.
10+
//// interface Foo<T extends { prop: [|T|] }> {}
11+
12+
for (const range of test.ranges()) {
13+
goTo.selectRange(range);
14+
verify.not.refactorAvailable("Extract type", "Extract to type alias");
15+
}

0 commit comments

Comments
 (0)