Skip to content

Commit ec49525

Browse files
Merge pull request #10211 from RyanCavanaugh/fourslashTestPerf
Speed up fourslash tests
2 parents 19d89c4 + 40f0c60 commit ec49525

File tree

2 files changed

+30
-64
lines changed

2 files changed

+30
-64
lines changed

src/harness/fourslash.ts

Lines changed: 26 additions & 61 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

@@ -2269,40 +2262,12 @@ namespace FourSlash {
22692262
export function runFourSlashTestContent(basePath: string, testType: FourSlashTestType, content: string, fileName: string): void {
22702263
// Parse out the files and their metadata
22712264
const testData = parseTestData(basePath, content, fileName);
2272-
22732265
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"));
2266+
const output = ts.transpileModule(content, { reportDiagnostics: true });
2267+
if (output.diagnostics.length > 0) {
2268+
throw new Error(`Syntax error in ${basePath}: ${output.diagnostics[0].messageText}`);
23002269
}
2301-
2302-
program.emit(sourceFile);
2303-
2304-
ts.Debug.assert(!!result);
2305-
runCode(result, state);
2270+
runCode(output.outputText, state);
23062271
}
23072272

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

tests/cases/fourslash/localGetReferences.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,13 @@ const rangesByText = test.rangesByText();
205205
for (const text in rangesByText) {
206206
const ranges = rangesByText[text];
207207
if (text === "globalVar") {
208-
function isShadow(r) {
209-
return r.marker && r.marker.data && r.marker.data.shadow;
210-
}
211208
verify.rangesReferenceEachOther(ranges.filter(isShadow));
212209
verify.rangesReferenceEachOther(ranges.filter(r => !isShadow(r)));
213210
} else {
214211
verify.rangesReferenceEachOther(ranges);
215212
}
216213
}
214+
215+
function isShadow(r) {
216+
return r.marker && r.marker.data && r.marker.data.shadow;
217+
}

0 commit comments

Comments
 (0)