Skip to content

Commit 97cd07d

Browse files
Cache the emit of 'fourslash.ts'.
Yields a >25% decrease in running time for fourslash tests on my machine.
1 parent f3b28c4 commit 97cd07d

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

src/compiler/program.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,11 @@ module ts {
102102
};
103103
}
104104

105-
export function getPreEmitDiagnostics(program: Program): Diagnostic[] {
106-
let diagnostics = program.getSyntacticDiagnostics().concat(program.getGlobalDiagnostics()).concat(program.getSemanticDiagnostics());
105+
export function getPreEmitDiagnostics(program: Program, sourceFile?: SourceFile): Diagnostic[] {
106+
let diagnostics = program.getSyntacticDiagnostics(sourceFile).concat(program.getGlobalDiagnostics()).concat(program.getSemanticDiagnostics(sourceFile));
107107

108108
if (program.getCompilerOptions().declaration) {
109-
diagnostics.concat(program.getDeclarationDiagnostics());
109+
diagnostics.concat(program.getDeclarationDiagnostics(sourceFile));
110110
}
111111

112112
return sortAndDeduplicateDiagnostics(diagnostics);

src/harness/fourslash.ts

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2192,38 +2192,62 @@ module FourSlash {
21922192
xmlData.push(xml);
21932193
}
21942194

2195+
// We don't want to recompile 'fourslash.ts' for every test, so
2196+
// here we cache the JS output and reuse it for every test.
2197+
let fourslashJsOutput: string;
2198+
{
2199+
let host = Harness.Compiler.createCompilerHost([{ unitName: Harness.Compiler.fourslashFileName, content: undefined }],
2200+
(fn, contents) => fourslashJsOutput = contents,
2201+
ts.ScriptTarget.Latest,
2202+
ts.sys.useCaseSensitiveFileNames);
2203+
2204+
let program = ts.createProgram([Harness.Compiler.fourslashFileName], { noResolve: true, target: ts.ScriptTarget.ES3 }, host);
2205+
2206+
program.emit(host.getSourceFile(Harness.Compiler.fourslashFileName, ts.ScriptTarget.ES3));
2207+
}
2208+
2209+
21952210
export function runFourSlashTestContent(basePath: string, testType: FourSlashTestType, content: string, fileName: string): TestXmlData {
21962211
// Parse out the files and their metadata
2197-
var testData = parseTestData(basePath, content, fileName);
2212+
let testData = parseTestData(basePath, content, fileName);
21982213

21992214
currentTestState = new TestState(basePath, testType, testData);
22002215

2201-
var result = '';
2202-
var host = Harness.Compiler.createCompilerHost([{ unitName: Harness.Compiler.fourslashFileName, content: undefined },
2203-
{ unitName: fileName, content: content }],
2216+
let result = '';
2217+
let host = Harness.Compiler.createCompilerHost(
2218+
[
2219+
{ unitName: Harness.Compiler.fourslashFileName, content: undefined },
2220+
{ unitName: fileName, content: content }
2221+
],
22042222
(fn, contents) => result = contents,
22052223
ts.ScriptTarget.Latest,
22062224
ts.sys.useCaseSensitiveFileNames);
2207-
// TODO (drosen): We need to enforce checking on these tests.
2208-
var program = ts.createProgram([Harness.Compiler.fourslashFileName, fileName], { out: "fourslashTestOutput.js", noResolve: true, target: ts.ScriptTarget.ES3 }, host);
22092225

2210-
var diagnostics = ts.getPreEmitDiagnostics(program);
2226+
let program = ts.createProgram([Harness.Compiler.fourslashFileName, fileName], { out: "fourslashTestOutput.js", noResolve: true, target: ts.ScriptTarget.ES3 }, host);
2227+
2228+
let sourceFile = host.getSourceFile(fileName, ts.ScriptTarget.ES3);
2229+
2230+
let diagnostics = ts.getPreEmitDiagnostics(program, sourceFile);
22112231
if (diagnostics.length > 0) {
22122232
throw new Error('Error compiling ' + fileName + ': ' +
22132233
diagnostics.map(e => ts.flattenDiagnosticMessageText(e.messageText, ts.sys.newLine)).join('\r\n'));
22142234
}
2215-
program.emit();
2235+
2236+
program.emit(sourceFile);
22162237
result = result || ''; // Might have an empty fourslash file
22172238

2239+
result = fourslashJsOutput + "\r\n" + result;
2240+
22182241
// Compile and execute the test
22192242
try {
22202243
eval(result);
2221-
} catch (err) {
2244+
}
2245+
catch (err) {
22222246
// Debugging: FourSlash.currentTestState.printCurrentFileState();
22232247
throw err;
22242248
}
22252249

2226-
var xmlData = currentTestState.getTestXmlData();
2250+
let xmlData = currentTestState.getTestXmlData();
22272251
xmlData.originalName = fileName;
22282252
return xmlData;
22292253
}

0 commit comments

Comments
 (0)