Skip to content

Commit ce7f554

Browse files
committed
Speed up fourslash tests
1 parent f6a850b commit ce7f554

File tree

1 file changed

+35
-72
lines changed

1 file changed

+35
-72
lines changed

src/harness/fourslash.ts

Lines changed: 35 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ namespace FourSlash {
249249
if (compilationOptions.typeRoots) {
250250
compilationOptions.typeRoots = compilationOptions.typeRoots.map(p => ts.getNormalizedAbsolutePath(p, this.basePath));
251251
}
252+
compilationOptions.skipDefaultLibCheck = true;
252253

253254
const languageServiceAdapter = this.getLanguageServiceAdapter(testType, this.cancellationToken, compilationOptions);
254255
this.languageServiceAdapterHost = languageServiceAdapter.getHost();
@@ -376,7 +377,7 @@ namespace FourSlash {
376377
public verifyErrorExistsBetweenMarkers(startMarkerName: string, endMarkerName: string, negative: boolean) {
377378
const startMarker = this.getMarkerByName(startMarkerName);
378379
const endMarker = this.getMarkerByName(endMarkerName);
379-
const predicate = function (errorMinChar: number, errorLimChar: number, startPos: number, endPos: number) {
380+
const predicate = function(errorMinChar: number, errorLimChar: number, startPos: number, endPos: number) {
380381
return ((errorMinChar === startPos) && (errorLimChar === endPos)) ? true : false;
381382
};
382383

@@ -428,12 +429,12 @@ namespace FourSlash {
428429
let predicate: (errorMinChar: number, errorLimChar: number, startPos: number, endPos: number) => boolean;
429430

430431
if (after) {
431-
predicate = function (errorMinChar: number, errorLimChar: number, startPos: number, endPos: number) {
432+
predicate = function(errorMinChar: number, errorLimChar: number, startPos: number, endPos: number) {
432433
return ((errorMinChar >= startPos) && (errorLimChar >= startPos)) ? true : false;
433434
};
434435
}
435436
else {
436-
predicate = function (errorMinChar: number, errorLimChar: number, startPos: number, endPos: number) {
437+
predicate = function(errorMinChar: number, errorLimChar: number, startPos: number, endPos: number) {
437438
return ((errorMinChar <= startPos) && (errorLimChar <= startPos)) ? true : false;
438439
};
439440
}
@@ -458,7 +459,7 @@ namespace FourSlash {
458459
endPos = endMarker.position;
459460
}
460461

461-
errors.forEach(function (error: ts.Diagnostic) {
462+
errors.forEach(function(error: ts.Diagnostic) {
462463
if (predicate(error.start, error.start + error.length, startPos, endPos)) {
463464
exists = true;
464465
}
@@ -475,7 +476,7 @@ namespace FourSlash {
475476
Harness.IO.log("Unexpected error(s) found. Error list is:");
476477
}
477478

478-
errors.forEach(function (error: ts.Diagnostic) {
479+
errors.forEach(function(error: ts.Diagnostic) {
479480
Harness.IO.log(" minChar: " + error.start +
480481
", limChar: " + (error.start + error.length) +
481482
", message: " + ts.flattenDiagnosticMessageText(error.messageText, Harness.IO.newLine()) + "\n");
@@ -1348,14 +1349,7 @@ namespace FourSlash {
13481349
}
13491350

13501351
// Enters lines of text at the current caret position
1351-
public type(text: string) {
1352-
return this.typeHighFidelity(text);
1353-
}
1354-
1355-
// Enters lines of text at the current caret position, invoking
1356-
// language service APIs to mimic Visual Studio's behavior
1357-
// as much as possible
1358-
private typeHighFidelity(text: string) {
1352+
public type(text: string, highFidelity = false) {
13591353
let offset = this.currentCaretPosition;
13601354
const prevChar = " ";
13611355
const checkCadence = (text.length >> 2) + 1;
@@ -1364,39 +1358,39 @@ namespace FourSlash {
13641358
// Make the edit
13651359
const ch = text.charAt(i);
13661360
this.languageServiceAdapterHost.editScript(this.activeFile.fileName, offset, offset, ch);
1367-
this.languageService.getBraceMatchingAtPosition(this.activeFile.fileName, offset);
1361+
if (highFidelity) {
1362+
this.languageService.getBraceMatchingAtPosition(this.activeFile.fileName, offset);
1363+
}
13681364

13691365
this.updateMarkersForEdit(this.activeFile.fileName, offset, offset, ch);
13701366
offset++;
13711367

1372-
if (ch === "(" || ch === ",") {
1373-
/* Signature help*/
1374-
this.languageService.getSignatureHelpItems(this.activeFile.fileName, offset);
1375-
}
1376-
else if (prevChar === " " && /A-Za-z_/.test(ch)) {
1377-
/* Completions */
1378-
this.languageService.getCompletionsAtPosition(this.activeFile.fileName, offset);
1379-
}
1368+
if (highFidelity) {
1369+
if (ch === "(" || ch === ",") {
1370+
/* Signature help*/
1371+
this.languageService.getSignatureHelpItems(this.activeFile.fileName, offset);
1372+
}
1373+
else if (prevChar === " " && /A-Za-z_/.test(ch)) {
1374+
/* Completions */
1375+
this.languageService.getCompletionsAtPosition(this.activeFile.fileName, offset);
1376+
}
13801377

1381-
if (i % checkCadence === 0) {
1382-
this.checkPostEditInvariants();
1383-
// this.languageService.getSyntacticDiagnostics(this.activeFile.fileName);
1384-
// this.languageService.getSemanticDiagnostics(this.activeFile.fileName);
1378+
if (i % checkCadence === 0) {
1379+
this.checkPostEditInvariants();
1380+
}
13851381
}
13861382

13871383
// Handle post-keystroke formatting
13881384
if (this.enableFormatting) {
13891385
const edits = this.languageService.getFormattingEditsAfterKeystroke(this.activeFile.fileName, offset, ch, this.formatCodeOptions);
13901386
if (edits.length) {
13911387
offset += this.applyEdits(this.activeFile.fileName, edits, /*isFormattingEdit*/ true);
1392-
// this.checkPostEditInvariants();
13931388
}
13941389
}
13951390
}
13961391

13971392
// Move the caret to wherever we ended up
13981393
this.currentCaretPosition = offset;
1399-
14001394
this.fixCaretPosition();
14011395
this.checkPostEditInvariants();
14021396
}
@@ -1415,7 +1409,6 @@ namespace FourSlash {
14151409
const edits = this.languageService.getFormattingEditsForRange(this.activeFile.fileName, start, offset, this.formatCodeOptions);
14161410
if (edits.length) {
14171411
offset += this.applyEdits(this.activeFile.fileName, edits, /*isFormattingEdit*/ true);
1418-
this.checkPostEditInvariants();
14191412
}
14201413
}
14211414

@@ -1433,20 +1426,22 @@ namespace FourSlash {
14331426
return;
14341427
}
14351428

1436-
const incrementalSourceFile = this.languageService.getNonBoundSourceFile(this.activeFile.fileName);
1437-
Utils.assertInvariants(incrementalSourceFile, /*parent:*/ undefined);
1429+
if (1 + 1 === 2) {
1430+
const incrementalSourceFile = this.languageService.getNonBoundSourceFile(this.activeFile.fileName);
1431+
Utils.assertInvariants(incrementalSourceFile, /*parent:*/ undefined);
14381432

1439-
const incrementalSyntaxDiagnostics = incrementalSourceFile.parseDiagnostics;
1433+
const incrementalSyntaxDiagnostics = incrementalSourceFile.parseDiagnostics;
14401434

1441-
// Check syntactic structure
1442-
const content = this.getFileContent(this.activeFile.fileName);
1435+
// Check syntactic structure
1436+
const content = this.getFileContent(this.activeFile.fileName);
14431437

1444-
const referenceSourceFile = ts.createLanguageServiceSourceFile(
1445-
this.activeFile.fileName, createScriptSnapShot(content), ts.ScriptTarget.Latest, /*version:*/ "0", /*setNodeParents:*/ false);
1446-
const referenceSyntaxDiagnostics = referenceSourceFile.parseDiagnostics;
1438+
const referenceSourceFile = ts.createLanguageServiceSourceFile(
1439+
this.activeFile.fileName, createScriptSnapShot(content), ts.ScriptTarget.Latest, /*version:*/ "0", /*setNodeParents:*/ false);
1440+
const referenceSyntaxDiagnostics = referenceSourceFile.parseDiagnostics;
14471441

1448-
Utils.assertDiagnosticsEquals(incrementalSyntaxDiagnostics, referenceSyntaxDiagnostics);
1449-
Utils.assertStructuralEquals(incrementalSourceFile, referenceSourceFile);
1442+
Utils.assertDiagnosticsEquals(incrementalSyntaxDiagnostics, referenceSyntaxDiagnostics);
1443+
Utils.assertStructuralEquals(incrementalSourceFile, referenceSourceFile);
1444+
}
14501445
}
14511446

14521447
private fixCaretPosition() {
@@ -2269,40 +2264,8 @@ namespace FourSlash {
22692264
export function runFourSlashTestContent(basePath: string, testType: FourSlashTestType, content: string, fileName: string): void {
22702265
// Parse out the files and their metadata
22712266
const testData = parseTestData(basePath, content, fileName);
2272-
22732267
const state = new TestState(basePath, testType, testData);
2274-
2275-
let result = "";
2276-
const fourslashFile: Harness.Compiler.TestFile = {
2277-
unitName: Harness.Compiler.fourslashFileName,
2278-
content: undefined,
2279-
};
2280-
const testFile: Harness.Compiler.TestFile = {
2281-
unitName: fileName,
2282-
content: content
2283-
};
2284-
2285-
const host = Harness.Compiler.createCompilerHost(
2286-
[fourslashFile, testFile],
2287-
(fn, contents) => result = contents,
2288-
ts.ScriptTarget.Latest,
2289-
Harness.IO.useCaseSensitiveFileNames(),
2290-
Harness.IO.getCurrentDirectory());
2291-
2292-
const program = ts.createProgram([Harness.Compiler.fourslashFileName, fileName], { outFile: "fourslashTestOutput.js", noResolve: true, target: ts.ScriptTarget.ES3 }, host);
2293-
2294-
const sourceFile = host.getSourceFile(fileName, ts.ScriptTarget.ES3);
2295-
2296-
const diagnostics = ts.getPreEmitDiagnostics(program, sourceFile);
2297-
if (diagnostics.length > 0) {
2298-
throw new Error(`Error compiling ${fileName}: ` +
2299-
diagnostics.map(e => ts.flattenDiagnosticMessageText(e.messageText, Harness.IO.newLine())).join("\r\n"));
2300-
}
2301-
2302-
program.emit(sourceFile);
2303-
2304-
ts.Debug.assert(!!result);
2305-
runCode(result, state);
2268+
runCode(ts.transpile(content), state);
23062269
}
23072270

23082271
function runCode(code: string, state: TestState): void {

0 commit comments

Comments
 (0)