-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Initial implementation of Extract Constant #18783
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3eea1a9
Generalize extract method to handle constants as well
amcasey eb1fb5c
Rename extractMethod.ts to extractSymbol.ts
amcasey 2601bbc
Add simple tests for Extract Constant
amcasey 52ab05e
Rename extractMethods.ts to extractFunctions.ts for consistency
amcasey 697bce7
Split range tests and helpers out of extractFunctions.ts
amcasey cb6037b
Forbid extraction of constants to class scopes in JS
amcasey 13e60bc
Use resources, rather than string literals, in test baselines
amcasey e6bfce1
Add additional TODO about insertion positions
amcasey 386e765
TODOs for repeated substitution
amcasey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,87 @@ | ||
/// <reference path="extractTestHelpers.ts" /> | ||
|
||
namespace ts { | ||
describe("extractConstants", () => { | ||
testExtractConstant("extractConstant_TopLevel", | ||
`let x = [#|1|];`); | ||
|
||
testExtractConstant("extractConstant_Namespace", | ||
`namespace N { | ||
let x = [#|1|]; | ||
}`); | ||
|
||
testExtractConstant("extractConstant_Class", | ||
`class C { | ||
x = [#|1|]; | ||
}`); | ||
|
||
testExtractConstant("extractConstant_Method", | ||
`class C { | ||
M() { | ||
let x = [#|1|]; | ||
} | ||
}`); | ||
|
||
testExtractConstant("extractConstant_Function", | ||
`function F() { | ||
let x = [#|1|]; | ||
}`); | ||
|
||
testExtractConstant("extractConstant_ExpressionStatement", | ||
`[#|"hello";|]`); | ||
|
||
testExtractConstant("extractConstant_ExpressionStatementExpression", | ||
`[#|"hello"|];`); | ||
|
||
testExtractConstant("extractConstant_BlockScopes_NoDependencies", | ||
`for (let i = 0; i < 10; i++) { | ||
for (let j = 0; j < 10; j++) { | ||
let x = [#|1|]; | ||
} | ||
}`); | ||
|
||
testExtractConstant("extractConstant_ClassInsertionPosition", | ||
`class C { | ||
a = 1; | ||
b = 2; | ||
M1() { } | ||
M2() { } | ||
M3() { | ||
let x = [#|1|]; | ||
} | ||
}`); | ||
|
||
testExtractConstant("extractConstant_Parameters", | ||
`function F() { | ||
let w = 1; | ||
let x = [#|w + 1|]; | ||
}`); | ||
|
||
testExtractConstant("extractConstant_TypeParameters", | ||
`function F<T>(t: T) { | ||
let x = [#|t + 1|]; | ||
}`); | ||
|
||
// TODO (acasey): handle repeated substitution | ||
// testExtractConstant("extractConstant_RepeatedSubstitution", | ||
// `namespace X { | ||
// export const j = 10; | ||
// export const y = [#|j * j|]; | ||
// }`); | ||
|
||
testExtractConstantFailed("extractConstant_BlockScopes_Dependencies", | ||
`for (let i = 0; i < 10; i++) { | ||
for (let j = 0; j < 10; j++) { | ||
let x = [#|i + 1|]; | ||
} | ||
}`); | ||
}); | ||
|
||
function testExtractConstant(caption: string, text: string) { | ||
testExtractSymbol(caption, text, "extractConstant", Diagnostics.Extract_constant); | ||
} | ||
|
||
function testExtractConstantFailed(caption: string, text: string) { | ||
testExtractSymbolFailed(caption, text, Diagnostics.Extract_constant); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 test coverage isn't great because I didn't want to revise everything as soon as I updated the insertion locations.