Handle class declarations without names in Extract Symbol

Fixes #21816
This commit is contained in:
Andrew Casey 2018-02-09 15:54:32 -08:00
parent 22c4862d9e
commit 025418fdb0
4 changed files with 66 additions and 1 deletions

View File

@ -546,6 +546,13 @@ var q = /*b*/ //c
/*g*/ + /*h*/ //i
/*j*/ 2|] /*k*/ //l
/*m*/; /*n*/ //o`);
testExtractFunction("extractFunction_NamelessClass", `
export default class {
M() {
[#|1 + 1|];
}
}`);
});
function testExtractFunction(caption: string, text: string, includeLib?: boolean) {

View File

@ -687,7 +687,7 @@ namespace ts.refactor.extractSymbol {
}
function getDescriptionForClassLikeDeclaration(scope: ClassLikeDeclaration): string {
return scope.kind === SyntaxKind.ClassDeclaration
? `class '${scope.name.text}'`
? scope.name ? `class '${scope.name.text}'` : "anonymous class declaration"
: scope.name ? `class expression '${scope.name.text}'` : "anonymous class expression";
}
function getDescriptionForModuleLikeDeclaration(scope: SourceFile | ModuleBlock): string | SpecialScope {

View File

@ -0,0 +1,29 @@
// ==ORIGINAL==
export default class {
M() {
/*[#|*/1 + 1/*|]*/;
}
}
// ==SCOPE::Extract to method in anonymous class declaration==
export default class {
M() {
this./*RENAME*/newMethod();
}
newMethod() {
1 + 1;
}
}
// ==SCOPE::Extract to function in module scope==
export default class {
M() {
/*RENAME*/newFunction();
}
}
function newFunction() {
1 + 1;
}

View File

@ -0,0 +1,29 @@
// ==ORIGINAL==
export default class {
M() {
/*[#|*/1 + 1/*|]*/;
}
}
// ==SCOPE::Extract to method in anonymous class declaration==
export default class {
M() {
this./*RENAME*/newMethod();
}
private newMethod() {
1 + 1;
}
}
// ==SCOPE::Extract to function in module scope==
export default class {
M() {
/*RENAME*/newFunction();
}
}
function newFunction() {
1 + 1;
}