Skip to content

Allow Extract Constant into enclosing scope in spite of RangeFacts.UsesThis #18931

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/harness/unittests/extractConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion src/services/refactors/extractSymbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// ==ORIGINAL==

class C {
constructor() {
/*[#|*/this.m2()/*|]*/;
}
m2() { return 1; }
}
// ==SCOPE::Extract to constant in enclosing scope==

class C {
constructor() {
const /*RENAME*/newLocal = this.m2();
}
m2() { return 1; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// ==ORIGINAL==

class C {
constructor() {
/*[#|*/this.m2()/*|]*/;
}
m2() { return 1; }
}
// ==SCOPE::Extract to constant in enclosing scope==

class C {
constructor() {
const /*RENAME*/newLocal = this.m2();
}
m2() { return 1; }
}
// ==SCOPE::Extract to readonly field in class 'C'==

class C {
private readonly newProperty = this.m2();

constructor() {
this./*RENAME*/newProperty;
}
m2() { return 1; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// ==ORIGINAL==

class C {
m1() {
/*[#|*/this.m2()/*|]*/;
}
m2() { return 1; }
}
// ==SCOPE::Extract to constant in enclosing scope==

class C {
m1() {
const /*RENAME*/newLocal = this.m2();
}
m2() { return 1; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// ==ORIGINAL==

class C {
m1() {
/*[#|*/this.m2()/*|]*/;
}
m2() { return 1; }
}
// ==SCOPE::Extract to constant in enclosing scope==

class C {
m1() {
const /*RENAME*/newLocal = this.m2();
}
m2() { return 1; }
}
// ==SCOPE::Extract to readonly field in class 'C'==

class C {
private readonly newProperty = this.m2();

m1() {
this./*RENAME*/newProperty;
}
m2() { return 1; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// ==ORIGINAL==

namespace N { // Force this test to be TS-only
class C {
x = 1;
y = /*[#|*/this.x/*|]*/;
}
}
// ==SCOPE::Extract to readonly field in class 'C'==

namespace N { // Force this test to be TS-only
class C {
x = 1;
private readonly newProperty = this.x;

y = this./*RENAME*/newProperty;
}
}
5 changes: 3 additions & 2 deletions tests/cases/fourslash/extract-method20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
//// }

goTo.select('a', 'b')
verify.refactorAvailable('Extract Symbol', 'function_scope_0');
verify.not.refactorAvailable('Extract Symbol', 'function_scope_1');
verify.not.refactorAvailable('Extract Symbol', 'function_scope_0');
verify.refactorAvailable('Extract Symbol', 'function_scope_1');
verify.not.refactorAvailable('Extract Symbol', 'function_scope_2');