Skip to content

Commit 5f30106

Browse files
authored
Merge pull request #18783 from amcasey/ExtractConstant
Initial implementation of Extract Constant
2 parents a92d315 + 386e765 commit 5f30106

File tree

83 files changed

+1687
-1123
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+1687
-1123
lines changed

Jakefile.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,10 @@ var harnessSources = harnessCoreSources.concat([
138138
"projectErrors.ts",
139139
"matchFiles.ts",
140140
"initializeTSConfig.ts",
141-
"extractMethods.ts",
141+
"extractConstants.ts",
142+
"extractFunctions.ts",
143+
"extractRanges.ts",
144+
"extractTestHelpers.ts",
142145
"printer.ts",
143146
"textChanges.ts",
144147
"telemetry.ts",

src/compiler/diagnosticMessages.json

+11-1
Original file line numberDiff line numberDiff line change
@@ -3703,13 +3703,23 @@
37033703
"code": 95002
37043704
},
37053705

3706-
"Extract function": {
3706+
"Extract symbol": {
37073707
"category": "Message",
37083708
"code": 95003
37093709
},
37103710

37113711
"Extract to {0}": {
37123712
"category": "Message",
37133713
"code": 95004
3714+
},
3715+
3716+
"Extract function": {
3717+
"category": "Message",
3718+
"code": 95005
3719+
},
3720+
3721+
"Extract constant": {
3722+
"category": "Message",
3723+
"code": 95006
37143724
}
37153725
}

src/harness/tsconfig.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,10 @@
128128
"./unittests/printer.ts",
129129
"./unittests/transform.ts",
130130
"./unittests/customTransforms.ts",
131-
"./unittests/extractMethods.ts",
131+
"./unittests/extractConstants.ts",
132+
"./unittests/extractFunctions.ts",
133+
"./unittests/extractRanges.ts",
134+
"./unittests/extractTestHelpers.ts",
132135
"./unittests/textChanges.ts",
133136
"./unittests/telemetry.ts",
134137
"./unittests/languageService.ts",
+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/// <reference path="extractTestHelpers.ts" />
2+
3+
namespace ts {
4+
describe("extractConstants", () => {
5+
testExtractConstant("extractConstant_TopLevel",
6+
`let x = [#|1|];`);
7+
8+
testExtractConstant("extractConstant_Namespace",
9+
`namespace N {
10+
let x = [#|1|];
11+
}`);
12+
13+
testExtractConstant("extractConstant_Class",
14+
`class C {
15+
x = [#|1|];
16+
}`);
17+
18+
testExtractConstant("extractConstant_Method",
19+
`class C {
20+
M() {
21+
let x = [#|1|];
22+
}
23+
}`);
24+
25+
testExtractConstant("extractConstant_Function",
26+
`function F() {
27+
let x = [#|1|];
28+
}`);
29+
30+
testExtractConstant("extractConstant_ExpressionStatement",
31+
`[#|"hello";|]`);
32+
33+
testExtractConstant("extractConstant_ExpressionStatementExpression",
34+
`[#|"hello"|];`);
35+
36+
testExtractConstant("extractConstant_BlockScopes_NoDependencies",
37+
`for (let i = 0; i < 10; i++) {
38+
for (let j = 0; j < 10; j++) {
39+
let x = [#|1|];
40+
}
41+
}`);
42+
43+
testExtractConstant("extractConstant_ClassInsertionPosition",
44+
`class C {
45+
a = 1;
46+
b = 2;
47+
M1() { }
48+
M2() { }
49+
M3() {
50+
let x = [#|1|];
51+
}
52+
}`);
53+
54+
testExtractConstant("extractConstant_Parameters",
55+
`function F() {
56+
let w = 1;
57+
let x = [#|w + 1|];
58+
}`);
59+
60+
testExtractConstant("extractConstant_TypeParameters",
61+
`function F<T>(t: T) {
62+
let x = [#|t + 1|];
63+
}`);
64+
65+
// TODO (acasey): handle repeated substitution
66+
// testExtractConstant("extractConstant_RepeatedSubstitution",
67+
// `namespace X {
68+
// export const j = 10;
69+
// export const y = [#|j * j|];
70+
// }`);
71+
72+
testExtractConstantFailed("extractConstant_BlockScopes_Dependencies",
73+
`for (let i = 0; i < 10; i++) {
74+
for (let j = 0; j < 10; j++) {
75+
let x = [#|i + 1|];
76+
}
77+
}`);
78+
});
79+
80+
function testExtractConstant(caption: string, text: string) {
81+
testExtractSymbol(caption, text, "extractConstant", Diagnostics.Extract_constant);
82+
}
83+
84+
function testExtractConstantFailed(caption: string, text: string) {
85+
testExtractSymbolFailed(caption, text, Diagnostics.Extract_constant);
86+
}
87+
}

0 commit comments

Comments
 (0)