-
Notifications
You must be signed in to change notification settings - Fork 12.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
preserve this when extracting functions (#41992)
* preserve this when extracting functions * rename IsThisReferringToFunction to UsesThisInFunction * refactor * update tests
- Loading branch information
Showing
5 changed files
with
180 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
|
||
////function test(this: string, foo: string) { | ||
//// /*start*/console.log(this); | ||
//// console.log(foo); /*end*/ | ||
////} | ||
|
||
goTo.select("start", "end"); | ||
verify.refactorAvailable("Extract Symbol", "function_scope_0"); | ||
|
||
goTo.select("start", "end"); | ||
edit.applyRefactor({ | ||
refactorName: "Extract Symbol", | ||
actionName: "function_scope_1", | ||
actionDescription: "Extract to function in global scope", | ||
newContent: | ||
`function test(this: string, foo: string) { | ||
/*RENAME*/newFunction.call(this, foo); | ||
} | ||
function newFunction(this: string, foo: string) { | ||
console.log(this); | ||
console.log(foo); | ||
} | ||
` | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
////function test(this: { bar: string }, foo: string) { | ||
//// /*start*/console.log(this); | ||
//// console.log(foo); /*end*/ | ||
////} | ||
|
||
goTo.select("start", "end"); | ||
verify.refactorAvailable("Extract Symbol", "function_scope_0"); | ||
|
||
goTo.select("start", "end"); | ||
edit.applyRefactor({ | ||
refactorName: "Extract Symbol", | ||
actionName: "function_scope_1", | ||
actionDescription: "Extract to function in global scope", | ||
newContent: | ||
`function test(this: { bar: string }, foo: string) { | ||
/*RENAME*/newFunction.call(this, foo); | ||
} | ||
function newFunction(this: { bar: string; }, foo: string) { | ||
console.log(this); | ||
console.log(foo); | ||
} | ||
` | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
|
||
////const foo = { | ||
//// bar: "1", | ||
//// baz() { | ||
//// /*start*/console.log(this); | ||
//// console.log(this.bar);/*end*/ | ||
//// } | ||
////} | ||
|
||
goTo.select("start", "end"); | ||
verify.refactorAvailable("Extract Symbol", "function_scope_0"); | ||
verify.refactorAvailable("Extract Symbol", "function_scope_1"); | ||
|
||
goTo.select("start", "end"); | ||
edit.applyRefactor({ | ||
refactorName: "Extract Symbol", | ||
actionName: "function_scope_1", | ||
actionDescription: "Extract to function in global scope", | ||
newContent: | ||
`const foo = { | ||
bar: "1", | ||
baz() { | ||
/*RENAME*/newFunction.call(this); | ||
} | ||
} | ||
function newFunction(this: any) { | ||
console.log(this); | ||
console.log(this.bar); | ||
} | ||
` | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
////class Foo { | ||
//// bar() { | ||
//// function test(this: string, foo: string) { | ||
//// /*start*/console.log(this); | ||
//// console.log(foo); /*end*/ | ||
//// } | ||
//// } | ||
////} | ||
|
||
|
||
goTo.select("start", "end"); | ||
verify.refactorAvailable("Extract Symbol", "function_scope_1"); | ||
verify.not.refactorAvailable("Extract Symbol", "function_scope_2"); | ||
verify.refactorAvailable("Extract Symbol", "function_scope_3"); | ||
|
||
edit.applyRefactor({ | ||
refactorName: "Extract Symbol", | ||
actionName: "function_scope_0", | ||
actionDescription: "Extract to inner function in function 'test'", | ||
newContent: | ||
`class Foo { | ||
bar() { | ||
function test(this: string, foo: string) { | ||
/*RENAME*/newFunction.call(this); | ||
function newFunction(this: string) { | ||
console.log(this); | ||
console.log(foo); | ||
} | ||
} | ||
} | ||
}` | ||
}); |