diff --git a/src/harness/unittests/extractFunctions.ts b/src/harness/unittests/extractFunctions.ts index 56b3c35a292..ecc4112c3a7 100644 --- a/src/harness/unittests/extractFunctions.ts +++ b/src/harness/unittests/extractFunctions.ts @@ -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) { diff --git a/src/services/refactors/extractSymbol.ts b/src/services/refactors/extractSymbol.ts index 245f6d5859c..68001262a3f 100644 --- a/src/services/refactors/extractSymbol.ts +++ b/src/services/refactors/extractSymbol.ts @@ -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 { diff --git a/tests/baselines/reference/extractFunction/extractFunction_NamelessClass.js b/tests/baselines/reference/extractFunction/extractFunction_NamelessClass.js new file mode 100644 index 00000000000..f0c42044996 --- /dev/null +++ b/tests/baselines/reference/extractFunction/extractFunction_NamelessClass.js @@ -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; +} diff --git a/tests/baselines/reference/extractFunction/extractFunction_NamelessClass.ts b/tests/baselines/reference/extractFunction/extractFunction_NamelessClass.ts new file mode 100644 index 00000000000..9905cd08c74 --- /dev/null +++ b/tests/baselines/reference/extractFunction/extractFunction_NamelessClass.ts @@ -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; +}