mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-16 07:13:45 -05:00
Merge pull request #18931 from amcasey/ExtractConstantThis
Allow Extract Constant into enclosing scope in spite of RangeFacts.UsesThis
This commit is contained in:
@@ -230,6 +230,30 @@ function f(): void { }
|
||||
testExtractConstantFailed("extractConstant_Never", `
|
||||
function f(): never { }
|
||||
[#|f();|]`);
|
||||
|
||||
testExtractConstant("extractConstant_This_Constructor", `
|
||||
class C {
|
||||
constructor() {
|
||||
[#|this.m2()|];
|
||||
}
|
||||
m2() { return 1; }
|
||||
}`);
|
||||
|
||||
testExtractConstant("extractConstant_This_Method", `
|
||||
class C {
|
||||
m1() {
|
||||
[#|this.m2()|];
|
||||
}
|
||||
m2() { return 1; }
|
||||
}`);
|
||||
|
||||
testExtractConstant("extractConstant_This_Property", `
|
||||
namespace N { // Force this test to be TS-only
|
||||
class C {
|
||||
x = 1;
|
||||
y = [#|this.x|];
|
||||
}
|
||||
}`);
|
||||
});
|
||||
|
||||
function testExtractConstant(caption: string, text: string) {
|
||||
|
||||
@@ -476,7 +476,10 @@ namespace ts.refactor.extractSymbol {
|
||||
// if range uses this as keyword or as type inside the class then it can only be extracted to a method of the containing class
|
||||
const containingClass = getContainingClass(current);
|
||||
if (containingClass) {
|
||||
return [containingClass];
|
||||
const containingFunction = findAncestor(current, isFunctionLikeDeclaration);
|
||||
return containingFunction
|
||||
? [containingFunction, containingClass]
|
||||
: [containingClass];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user