Skip to content

Commit c7b4ed3

Browse files
mjbvzmhegazy
authored andcommitted
Fix extract method for anon class expressions (#18168)
Check `scope.name` when trying to extract from an anon class
1 parent 3d0c239 commit c7b4ed3

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

src/harness/unittests/extractMethods.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,14 @@ namespace A {
606606
`function F<T, U extends T[], V extends U[]>(v: V) {
607607
[#|v.toString()|];
608608
}`);
609+
610+
testExtractMethod("extractMethod20",
611+
`const _ = class {
612+
a() {
613+
[#|let a1 = { x: 1 };
614+
return a1.x + 10;|]
615+
}
616+
}`);
609617
});
610618

611619

src/services/refactors/extractMethod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ namespace ts.refactor.extractMethod {
584584
else if (isClassLike(scope)) {
585585
return scope.kind === SyntaxKind.ClassDeclaration
586586
? `class '${scope.name.text}'`
587-
: scope.name.text
587+
: scope.name && scope.name.text
588588
? `class expression '${scope.name.text}'`
589589
: "anonymous class expression";
590590
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// ==ORIGINAL==
2+
const _ = class {
3+
a() {
4+
let a1 = { x: 1 };
5+
return a1.x + 10;
6+
}
7+
}
8+
// ==SCOPE::anonymous class expression==
9+
const _ = class {
10+
a() {
11+
return this.newFunction();
12+
}
13+
14+
private newFunction() {
15+
let a1 = { x: 1 };
16+
return a1.x + 10;
17+
}
18+
}
19+
// ==SCOPE::global scope==
20+
const _ = class {
21+
a() {
22+
return newFunction();
23+
}
24+
}
25+
function newFunction() {
26+
let a1 = { x: 1 };
27+
return a1.x + 10;
28+
}

0 commit comments

Comments
 (0)