Skip to content

Commit d36f254

Browse files
mjbvzamcasey
authored andcommitted
Fix extract method for anon class expressions (#18168)
Check `scope.name` when trying to extract from an anon class (cherry picked from commit c7b4ed3)
1 parent f809956 commit d36f254

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
@@ -547,6 +547,14 @@ namespace A {
547547
}
548548
}
549549
}`);
550+
551+
testExtractMethod("extractMethod20",
552+
`const _ = class {
553+
a() {
554+
[#|let a1 = { x: 1 };
555+
return a1.x + 10;|]
556+
}
557+
}`);
550558
});
551559

552560

src/services/refactors/extractMethod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ namespace ts.refactor.extractMethod {
570570
else if (isClassLike(scope)) {
571571
return scope.kind === SyntaxKind.ClassDeclaration
572572
? `class '${scope.name.text}'`
573-
: scope.name.text
573+
: scope.name && scope.name.text
574574
? `class expression '${scope.name.text}'`
575575
: "anonymous class expression";
576576
}
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./*RENAME*/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 /*RENAME*/newFunction();
23+
}
24+
}
25+
function newFunction() {
26+
let a1 = { x: 1 };
27+
return a1.x + 10;
28+
}

0 commit comments

Comments
 (0)