Skip to content
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 9 commits into from
Sep 28, 2017
5 changes: 4 additions & 1 deletion Jakefile.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ var harnessSources = harnessCoreSources.concat([
"projectErrors.ts",
"matchFiles.ts",
"initializeTSConfig.ts",
"extractMethods.ts",
"extractConstants.ts",
"extractFunctions.ts",
"extractRanges.ts",
"extractTestHelpers.ts",
"printer.ts",
"textChanges.ts",
"telemetry.ts",
Expand Down
12 changes: 11 additions & 1 deletion src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3703,13 +3703,23 @@
"code": 95002
},

"Extract function": {
"Extract symbol": {
"category": "Message",
"code": 95003
},

"Extract to {0}": {
"category": "Message",
"code": 95004
},

"Extract function": {
"category": "Message",
"code": 95005
},

"Extract constant": {
"category": "Message",
"code": 95006
}
}
5 changes: 4 additions & 1 deletion src/harness/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@
"./unittests/printer.ts",
"./unittests/transform.ts",
"./unittests/customTransforms.ts",
"./unittests/extractMethods.ts",
"./unittests/extractConstants.ts",
"./unittests/extractFunctions.ts",
"./unittests/extractRanges.ts",
"./unittests/extractTestHelpers.ts",
"./unittests/textChanges.ts",
"./unittests/telemetry.ts",
"./unittests/languageService.ts",
Expand Down
87 changes: 87 additions & 0 deletions src/harness/unittests/extractConstants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/// <reference path="extractTestHelpers.ts" />

namespace ts {
describe("extractConstants", () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extractConstants [](start = 14, length = 16)

This test coverage isn't great because I didn't want to revise everything as soon as I updated the insertion locations.

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);
}
}
Loading