Skip to content

Commit 0d5d5cd

Browse files
committed
For any Extract* baseline that is valid JS, produce a JS baseline
1 parent 8e7a5ba commit 0d5d5cd

19 files changed

+437
-30
lines changed

src/harness/unittests/extractTestHelpers.ts

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -98,35 +98,48 @@ namespace ts {
9898
}
9999

100100
export function testExtractSymbol(caption: string, text: string, baselineFolder: string, description: DiagnosticMessage) {
101-
it(caption, () => {
102-
Harness.Baseline.runBaseline(`${baselineFolder}/${caption}.ts`, () => {
103-
const t = extractTest(text);
104-
const selectionRange = t.ranges.get("selection");
105-
if (!selectionRange) {
106-
throw new Error(`Test ${caption} does not specify selection range`);
107-
}
108-
const f = {
109-
path: "/a.ts",
110-
content: t.source
111-
};
112-
const host = projectSystem.createServerHost([f, projectSystem.libFile]);
113-
const projectService = projectSystem.createProjectService(host);
114-
projectService.openClientFile(f.path);
115-
const program = projectService.inferredProjects[0].getLanguageService().getProgram();
116-
const sourceFile = program.getSourceFile(f.path);
117-
const context: RefactorContext = {
118-
cancellationToken: { throwIfCancellationRequested() { }, isCancellationRequested() { return false; } },
119-
newLineCharacter,
120-
program,
121-
file: sourceFile,
122-
startPosition: selectionRange.start,
123-
endPosition: selectionRange.end,
124-
rulesProvider: getRuleProvider()
125-
};
126-
const rangeToExtract = refactor.extractSymbol.getRangeToExtract(sourceFile, createTextSpanFromBounds(selectionRange.start, selectionRange.end));
127-
assert.equal(rangeToExtract.errors, undefined, rangeToExtract.errors && "Range error: " + rangeToExtract.errors[0].messageText);
128-
const infos = refactor.extractSymbol.getAvailableActions(context);
129-
const actions = find(infos, info => info.description === description.message).actions;
101+
const t = extractTest(text);
102+
const selectionRange = t.ranges.get("selection");
103+
if (!selectionRange) {
104+
throw new Error(`Test ${caption} does not specify selection range`);
105+
}
106+
107+
[Extension.Ts, Extension.Js].forEach(extension =>
108+
it(`${caption} [${extension}]`, () => runBaseline(extension)));
109+
110+
function runBaseline(extension: Extension) {
111+
const f = {
112+
path: "/a" + extension,
113+
content: t.source
114+
};
115+
const host = projectSystem.createServerHost([f, projectSystem.libFile]);
116+
const projectService = projectSystem.createProjectService(host);
117+
projectService.openClientFile(f.path);
118+
const program = projectService.inferredProjects[0].getLanguageService().getProgram();
119+
120+
// Don't bother generating JS baselines for inputs that aren't valid JS.
121+
const diags = program.getSyntacticDiagnostics();
122+
if (diags && diags.length) {
123+
assert.equal(Extension.Js, extension);
124+
return;
125+
}
126+
127+
const sourceFile = program.getSourceFile(f.path);
128+
const context: RefactorContext = {
129+
cancellationToken: { throwIfCancellationRequested() { }, isCancellationRequested() { return false; } },
130+
newLineCharacter,
131+
program,
132+
file: sourceFile,
133+
startPosition: selectionRange.start,
134+
endPosition: selectionRange.end,
135+
rulesProvider: getRuleProvider()
136+
};
137+
const rangeToExtract = refactor.extractSymbol.getRangeToExtract(sourceFile, createTextSpanFromBounds(selectionRange.start, selectionRange.end));
138+
assert.equal(rangeToExtract.errors, undefined, rangeToExtract.errors && "Range error: " + rangeToExtract.errors[0].messageText);
139+
const infos = refactor.extractSymbol.getAvailableActions(context);
140+
const actions = find(infos, info => info.description === description.message).actions;
141+
142+
Harness.Baseline.runBaseline(`${baselineFolder}/${caption}${extension}`, () => {
130143
const data: string[] = [];
131144
data.push(`// ==ORIGINAL==`);
132145
data.push(sourceFile.text);
@@ -140,7 +153,7 @@ namespace ts {
140153
}
141154
return data.join(newLineCharacter);
142155
});
143-
});
156+
}
144157
}
145158

146159
export function testExtractSymbolFailed(caption: string, text: string, description: DiagnosticMessage) {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// ==ORIGINAL==
2+
for (let i = 0; i < 10; i++) {
3+
for (let j = 0; j < 10; j++) {
4+
let x = 1;
5+
}
6+
}
7+
// ==SCOPE::Extract to constant in global scope==
8+
const newLocal = 1;
9+
10+
for (let i = 0; i < 10; i++) {
11+
for (let j = 0; j < 10; j++) {
12+
let x = /*RENAME*/newLocal;
13+
}
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// ==ORIGINAL==
2+
class C {
3+
x = 1;
4+
}
5+
// ==SCOPE::Extract to constant in global scope==
6+
const newLocal = 1;
7+
8+
class C {
9+
x = /*RENAME*/newLocal;
10+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// ==ORIGINAL==
2+
class C {
3+
a = 1;
4+
b = 2;
5+
M1() { }
6+
M2() { }
7+
M3() {
8+
let x = 1;
9+
}
10+
}
11+
// ==SCOPE::Extract to constant in method 'M3==
12+
class C {
13+
a = 1;
14+
b = 2;
15+
M1() { }
16+
M2() { }
17+
M3() {
18+
const newLocal = 1;
19+
20+
let x = /*RENAME*/newLocal;
21+
}
22+
}
23+
// ==SCOPE::Extract to constant in global scope==
24+
const newLocal = 1;
25+
26+
class C {
27+
a = 1;
28+
b = 2;
29+
M1() { }
30+
M2() { }
31+
M3() {
32+
let x = /*RENAME*/newLocal;
33+
}
34+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// ==ORIGINAL==
2+
"hello";
3+
// ==SCOPE::Extract to constant in global scope==
4+
const /*RENAME*/newLocal = "hello";
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// ==ORIGINAL==
2+
"hello";
3+
// ==SCOPE::Extract to constant in global scope==
4+
const /*RENAME*/newLocal = "hello";
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// ==ORIGINAL==
2+
function F() {
3+
let x = 1;
4+
}
5+
// ==SCOPE::Extract to constant in function 'F'==
6+
function F() {
7+
const newLocal = 1;
8+
9+
let x = /*RENAME*/newLocal;
10+
}
11+
// ==SCOPE::Extract to constant in global scope==
12+
const newLocal = 1;
13+
14+
function F() {
15+
let x = /*RENAME*/newLocal;
16+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// ==ORIGINAL==
2+
class C {
3+
M() {
4+
let x = 1;
5+
}
6+
}
7+
// ==SCOPE::Extract to constant in method 'M==
8+
class C {
9+
M() {
10+
const newLocal = 1;
11+
12+
let x = /*RENAME*/newLocal;
13+
}
14+
}
15+
// ==SCOPE::Extract to constant in global scope==
16+
const newLocal = 1;
17+
18+
class C {
19+
M() {
20+
let x = /*RENAME*/newLocal;
21+
}
22+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// ==ORIGINAL==
2+
function F() {
3+
let w = 1;
4+
let x = w + 1;
5+
}
6+
// ==SCOPE::Extract to constant in function 'F'==
7+
function F() {
8+
let w = 1;
9+
const newLocal = w + 1;
10+
11+
let x = /*RENAME*/newLocal;
12+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// ==ORIGINAL==
2+
let x = 1;
3+
// ==SCOPE::Extract to constant in global scope==
4+
const newLocal = 1;
5+
6+
let x = /*RENAME*/newLocal;

0 commit comments

Comments
 (0)