Skip to content

Commit 025418f

Browse files
committed
Handle class declarations without names in Extract Symbol
Fixes microsoft#21816
1 parent 22c4862 commit 025418f

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

src/harness/unittests/extractFunctions.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,13 @@ var q = /*b*/ //c
546546
/*g*/ + /*h*/ //i
547547
/*j*/ 2|] /*k*/ //l
548548
/*m*/; /*n*/ //o`);
549+
550+
testExtractFunction("extractFunction_NamelessClass", `
551+
export default class {
552+
M() {
553+
[#|1 + 1|];
554+
}
555+
}`);
549556
});
550557

551558
function testExtractFunction(caption: string, text: string, includeLib?: boolean) {

src/services/refactors/extractSymbol.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ namespace ts.refactor.extractSymbol {
687687
}
688688
function getDescriptionForClassLikeDeclaration(scope: ClassLikeDeclaration): string {
689689
return scope.kind === SyntaxKind.ClassDeclaration
690-
? `class '${scope.name.text}'`
690+
? scope.name ? `class '${scope.name.text}'` : "anonymous class declaration"
691691
: scope.name ? `class expression '${scope.name.text}'` : "anonymous class expression";
692692
}
693693
function getDescriptionForModuleLikeDeclaration(scope: SourceFile | ModuleBlock): string | SpecialScope {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// ==ORIGINAL==
2+
3+
export default class {
4+
M() {
5+
/*[#|*/1 + 1/*|]*/;
6+
}
7+
}
8+
// ==SCOPE::Extract to method in anonymous class declaration==
9+
10+
export default class {
11+
M() {
12+
this./*RENAME*/newMethod();
13+
}
14+
15+
newMethod() {
16+
1 + 1;
17+
}
18+
}
19+
// ==SCOPE::Extract to function in module scope==
20+
21+
export default class {
22+
M() {
23+
/*RENAME*/newFunction();
24+
}
25+
}
26+
27+
function newFunction() {
28+
1 + 1;
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// ==ORIGINAL==
2+
3+
export default class {
4+
M() {
5+
/*[#|*/1 + 1/*|]*/;
6+
}
7+
}
8+
// ==SCOPE::Extract to method in anonymous class declaration==
9+
10+
export default class {
11+
M() {
12+
this./*RENAME*/newMethod();
13+
}
14+
15+
private newMethod() {
16+
1 + 1;
17+
}
18+
}
19+
// ==SCOPE::Extract to function in module scope==
20+
21+
export default class {
22+
M() {
23+
/*RENAME*/newFunction();
24+
}
25+
}
26+
27+
function newFunction() {
28+
1 + 1;
29+
}

0 commit comments

Comments
 (0)