Skip to content

Commit b4a4500

Browse files
committed
Use compiler directly in fourslash runner so we can avoid repeated compile overhead
1 parent fedbc1c commit b4a4500

File tree

2 files changed

+32
-30
lines changed

2 files changed

+32
-30
lines changed

src/harness/fourslash.ts

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@ module FourSlash {
226226
});
227227
}
228228

229-
// NEWTODO: Re-implement commented-out section
229+
// NEWTODO: Re-implement commented-out section
230+
230231
//harnessCompiler.addInputFiles(inputFiles);
231232
//try {
232233
// var resolvedFiles = harnessCompiler.resolve();
@@ -246,8 +247,15 @@ module FourSlash {
246247
// harnessCompiler.reset();
247248
//}
248249

249-
/// NEWTODO: For now do not resolve, just use the input files inputFiles.forEach(file => { if (!Harness.isLibraryFile(file.unitName)) { this.languageServiceShimHost.addScript(file.unitName, file.content); } });
250-
this.languageServiceShimHost.addScript('lib.d.ts', Harness.Compiler.libTextMinimal);
250+
/// NEWTODO: For now do not resolve, just use the input files
251+
inputFiles.forEach(file => {
252+
if (!Harness.isLibraryFile(file.unitName)) {
253+
this.languageServiceShimHost.addScript(file.unitName, file.content);
254+
}
255+
});
256+
257+
this.languageServiceShimHost.addScript('lib.d.ts', Harness.Compiler.libTextMinimal);
258+
251259

252260
// Sneak into the language service and get its compiler so we can examine the syntax trees
253261
this.languageService = this.languageServiceShimHost.getLanguageService().languageService;
@@ -1875,50 +1883,44 @@ module FourSlash {
18751883
xmlData.push(xml);
18761884
}
18771885

1886+
// Cache these between executions so we don't have to re-parse them for every test
1887+
var fourslashSourceFile: ts.SourceFile = undefined;
1888+
var libdtsSourceFile: ts.SourceFile = undefined;
18781889
export function runFourSlashTestContent(content: string, fileName: string): TestXmlData {
18791890
// Parse out the files and their metadata
18801891
var testData = parseTestData(content, fileName);
18811892

18821893
currentTestState = new TestState(testData);
18831894

18841895
var result = '';
1885-
var tsFn = 'tests/cases/fourslash/fourslash.ts';
1896+
var fourslashFilename = 'fourslash.ts';
1897+
var tsFn = 'tests/cases/fourslash/' + fourslashFilename;
1898+
fourslashSourceFile = fourslashSourceFile || ts.createSourceFile(tsFn, Harness.IO.readFile(tsFn), ts.ScriptTarget.ES5);
1899+
libdtsSourceFile = libdtsSourceFile || ts.createSourceFile('lib.d.ts', Harness.Compiler.libTextMinimal, ts.ScriptTarget.ES3);
18861900

1887-
fsOutput.reset();
1888-
fsErrors.reset();
1901+
var files: { [filename: string]: ts.SourceFile; } = {};
1902+
files[fourslashFilename] = fourslashSourceFile;
1903+
files[fileName] = ts.createSourceFile(fileName, content, ts.ScriptTarget.ES5);
1904+
files['lib.d.ts'] = libdtsSourceFile;
18891905

1890-
var harnessCompiler = Harness.Compiler.getCompiler();
1891-
harnessCompiler.reset();
1906+
var host = Harness.Compiler.createCompilerHost(files, (fn, contents) => result = contents);
1907+
var program = ts.createProgram([fileName, fourslashFilename], {}, host);
1908+
var checker = ts.createTypeChecker(program);
1909+
checker.checkProgram();
18921910

1893-
var filesToAdd = [
1894-
{ unitName: tsFn, content: Harness.IO.readFile(tsFn) },
1895-
{ unitName: fileName, content: Harness.IO.readFile(fileName) }
1896-
];
1897-
harnessCompiler.addInputFiles(filesToAdd);
1898-
1899-
var emitterIOHost: Harness.Compiler.IEmitterIOHost = {
1900-
writeFile: (path: string, contents: string, writeByteOrderMark: boolean) => fsOutput.Write(contents),
1901-
resolvePath: (s: string) => s
1911+
var errs = checker.getDiagnostics(files[fileName]);
1912+
if (errs.length > 0) {
1913+
throw new Error('Error compiling ' + fileName + ': ' + errs.map(e => e.messageText).join('\r\n'));
19021914
}
1903-
1904-
harnessCompiler.emitAll(emitterIOHost);
1905-
fsOutput.Close();
1906-
fsErrors.Close();
1907-
1908-
if (fsErrors.lines.length > 0) {
1909-
throw new Error('Error compiling ' + fileName + ': ' + fsErrors.lines.join('\r\n'));
1910-
}
1911-
1912-
result = fsOutput.lines.join('\r\n');
1915+
checker.emitFiles();
1916+
result = result || ''; // Might have an empty fourslash file
19131917

19141918
// Compile and execute the test
19151919
try {
19161920
eval(result);
19171921
} catch (err) {
19181922
// Debugging: FourSlash.currentTestState.printCurrentFileState();
19191923
throw err;
1920-
} finally {
1921-
harnessCompiler.reset();
19221924
}
19231925

19241926
var xmlData = currentTestState.getTestXmlData();

src/harness/fourslashRunner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class FourslashRunner extends RunnerBase {
1111

1212
public initializeTests() {
1313
if (this.tests.length === 0) {
14-
this.tests = this.enumerateFiles(this.basePath);
14+
this.tests = this.enumerateFiles(this.basePath, /\.ts/i);
1515
}
1616

1717
describe("fourslash tests", () => {

0 commit comments

Comments
 (0)