Fix extract method for anon class expressions (#18168)

Check `scope.name` when trying to extract from an anon class

(cherry picked from commit c7b4ed3a91)
This commit is contained in:
Matt Bierner authored and Andrew Casey committed 2017-09-19 17:31:17 -07:00
1 parent f80995653a
commit d36f254f5e
3 files changed
+37 -1

No files matched your search

+8
View File
@@ -547,6 +547,14 @@ namespace A {
}
}
}`);
testExtractMethod("extractMethod20",
`const _ = class {
a() {
[#|let a1 = { x: 1 };
return a1.x + 10;|]
}
}`);
});
+1 -1
View File
@@ -570,7 +570,7 @@ namespace ts.refactor.extractMethod {
else if (isClassLike(scope)) {
return scope.kind === SyntaxKind.ClassDeclaration
? `class '${scope.name.text}'`
: scope.name.text
: scope.name && scope.name.text
? `class expression '${scope.name.text}'`
: "anonymous class expression";
}
@@ -0,0 +1,28 @@
// ==ORIGINAL==
const _ = class {
a() {
let a1 = { x: 1 };
return a1.x + 10;
}
}
// ==SCOPE::anonymous class expression==
const _ = class {
a() {
return this./*RENAME*/newFunction();
}
private newFunction() {
let a1 = { x: 1 };
return a1.x + 10;
}
}
// ==SCOPE::global scope==
const _ = class {
a() {
return /*RENAME*/newFunction();
}
}
function newFunction() {
let a1 = { x: 1 };
return a1.x + 10;
}