-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Extract method #16960
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
Extract method #16960
Changes from 4 commits
be27a1a
e798257
bf176a6
0130ebb
bbf5cd7
9dd6bef
f21b7ae
674b1bf
9367473
1ea9972
1787fa8
fb89c00
4aef5e7
b5c5c40
b1fa49f
825237b
aa6f3aa
b7e4451
419a4c2
4218738
6d2bc90
9a24549
271ecb1
bdefcc3
6428b1e
8af8ac2
ede6671
55b28ad
2d9eaa9
2f2c780
63193da
889292a
e229d42
7b2dd8a
bf6c0a9
3424312
ff18df3
7f29fcd
d39d95b
dda5496
e88ff21
bd80005
ff716d0
1291cfd
a26790f
56e1d6d
7f3a0a8
003cd0a
a76664c
42e0652
46a84c3
a97ab93
37ae957
2adb2b3
ba37830
4dafb40
c1c7e83
a4c41b8
b5a88c1
568d5f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
// Don't emit type annotations in JavaScript files | ||
// Also tests that single-variable return extractions don't get superfluous destructuring | ||
|
||
// @allowNonTsExtensions: true | ||
// @Filename: foo.js | ||
//// function foo() { | ||
//// var i = 10; | ||
//// /*a*/return i++;/*b*/ | ||
//// } | ||
|
||
goTo.select('a', 'b'); | ||
edit.applyRefactor('Extract Method', 'scope_1'); | ||
verify.currentFileContentIs(`function foo() { | ||
var i = 10; | ||
var __return: any; | ||
({ i, __return } = newFunction(i)); | ||
return __return; | ||
} | ||
function newFunction(i) { | ||
return { i, __return: i++ }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You assign function foo() {
var i = 10;
function inner() {
return i++; // extract this line into file scope, the console output changes
}
inner();
console.log(i);
}
foo(); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree that |
||
} | ||
`); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
// Extracting an increment expression (not statement) should do the right thing, | ||
// including not generating extra destructuring unless needed | ||
|
||
//// function foo() { | ||
//// var i = 10; | ||
//// /*a*/i++/*b*/; | ||
//// } | ||
|
||
goTo.select('a', 'b'); | ||
edit.applyRefactor('Extract Method', 'scope_1'); | ||
|
||
verify.currentFileContentIs(`function foo() { | ||
var i = 10; | ||
i = newFunction(i); | ||
} | ||
function newFunction(i: number) { | ||
i++; | ||
return i; | ||
} | ||
`); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
//// function foo () { | ||
//// var x = 3; | ||
//// var y = /*start*/x++ + 5/*end*/; | ||
//// } | ||
|
||
goTo.select('start', 'end') | ||
verify.refactorAvailable('Extract Method', 'scope_0'); | ||
verify.not.refactorAvailable('Extract Method', 'scope_1'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
// Don't try to propagate property accessed variables back, | ||
// or emit spurious returns when the value is clearly ignored | ||
|
||
//// function fn() { | ||
//// const x = { m: 1 }; | ||
//// /*a*/x.m = 3/*b*/; | ||
//// } | ||
|
||
goTo.select('a', 'b') | ||
verify.refactorAvailable('Extract Method'); | ||
edit.applyRefactor('Extract Method', "scope_1"); | ||
verify.currentFileContentIs(`function fn() { | ||
const x = { m: 1 }; | ||
newFunction(x); | ||
} | ||
function newFunction(x: { m: number; }) { | ||
x.m = 3; | ||
} | ||
`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We seem to have an existing pattern, but this is a particularly pointed example of using a predicate with a negative name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is to unify the
verify.something
andverify.not.something
functions