Skip to content

Commit 60b8f8c

Browse files
author
Andy
authored
Remove unnecessary callback in runBaseline (microsoft#26500)
1 parent 0f97620 commit 60b8f8c

33 files changed

+208
-299
lines changed

src/harness/fourslash.ts

Lines changed: 33 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,11 +1639,7 @@ Actual: ${stringify(fullActual)}`);
16391639
baselineFile = baselineFile.replace(ts.Extension.Ts, ".baseline");
16401640

16411641
}
1642-
Harness.Baseline.runBaseline(
1643-
baselineFile,
1644-
() => {
1645-
return this.baselineCurrentFileLocations(pos => this.getBreakpointStatementLocation(pos)!);
1646-
});
1642+
Harness.Baseline.runBaseline(baselineFile, this.baselineCurrentFileLocations(pos => this.getBreakpointStatementLocation(pos)!));
16471643
}
16481644

16491645
private getEmitFiles(): ReadonlyArray<FourSlashFile> {
@@ -1680,44 +1676,40 @@ Actual: ${stringify(fullActual)}`);
16801676
}
16811677

16821678
public baselineGetEmitOutput(): void {
1683-
Harness.Baseline.runBaseline(
1684-
ts.Debug.assertDefined(this.testData.globalOptions[MetadataOptionNames.baselineFile]),
1685-
() => {
1686-
let resultString = "";
1687-
// Loop through all the emittedFiles and emit them one by one
1688-
for (const emitFile of this.getEmitFiles()) {
1689-
const emitOutput = this.languageService.getEmitOutput(emitFile.fileName);
1690-
// Print emitOutputStatus in readable format
1691-
resultString += "EmitSkipped: " + emitOutput.emitSkipped + Harness.IO.newLine();
1692-
1693-
if (emitOutput.emitSkipped) {
1694-
resultString += "Diagnostics:" + Harness.IO.newLine();
1695-
const diagnostics = ts.getPreEmitDiagnostics(this.languageService.getProgram()!); // TODO: GH#18217
1696-
for (const diagnostic of diagnostics) {
1697-
if (!ts.isString(diagnostic.messageText)) {
1698-
let chainedMessage: ts.DiagnosticMessageChain | undefined = diagnostic.messageText;
1699-
let indentation = " ";
1700-
while (chainedMessage) {
1701-
resultString += indentation + chainedMessage.messageText + Harness.IO.newLine();
1702-
chainedMessage = chainedMessage.next;
1703-
indentation = indentation + " ";
1704-
}
1705-
}
1706-
else {
1707-
resultString += " " + diagnostic.messageText + Harness.IO.newLine();
1708-
}
1679+
let resultString = "";
1680+
// Loop through all the emittedFiles and emit them one by one
1681+
for (const emitFile of this.getEmitFiles()) {
1682+
const emitOutput = this.languageService.getEmitOutput(emitFile.fileName);
1683+
// Print emitOutputStatus in readable format
1684+
resultString += "EmitSkipped: " + emitOutput.emitSkipped + Harness.IO.newLine();
1685+
1686+
if (emitOutput.emitSkipped) {
1687+
resultString += "Diagnostics:" + Harness.IO.newLine();
1688+
const diagnostics = ts.getPreEmitDiagnostics(this.languageService.getProgram()!); // TODO: GH#18217
1689+
for (const diagnostic of diagnostics) {
1690+
if (!ts.isString(diagnostic.messageText)) {
1691+
let chainedMessage: ts.DiagnosticMessageChain | undefined = diagnostic.messageText;
1692+
let indentation = " ";
1693+
while (chainedMessage) {
1694+
resultString += indentation + chainedMessage.messageText + Harness.IO.newLine();
1695+
chainedMessage = chainedMessage.next;
1696+
indentation = indentation + " ";
17091697
}
17101698
}
1711-
1712-
for (const outputFile of emitOutput.outputFiles) {
1713-
const fileName = "FileName : " + outputFile.name + Harness.IO.newLine();
1714-
resultString = resultString + Harness.IO.newLine() + fileName + outputFile.text;
1699+
else {
1700+
resultString += " " + diagnostic.messageText + Harness.IO.newLine();
17151701
}
1716-
resultString += Harness.IO.newLine();
17171702
}
1703+
}
17181704

1719-
return resultString;
1720-
});
1705+
for (const outputFile of emitOutput.outputFiles) {
1706+
const fileName = "FileName : " + outputFile.name + Harness.IO.newLine();
1707+
resultString = resultString + Harness.IO.newLine() + fileName + outputFile.text;
1708+
}
1709+
resultString += Harness.IO.newLine();
1710+
}
1711+
1712+
Harness.Baseline.runBaseline(ts.Debug.assertDefined(this.testData.globalOptions[MetadataOptionNames.baselineFile]), resultString);
17211713
}
17221714

17231715
public baselineQuickInfo() {
@@ -1728,12 +1720,11 @@ Actual: ${stringify(fullActual)}`);
17281720

17291721
Harness.Baseline.runBaseline(
17301722
baselineFile,
1731-
() => stringify(
1723+
stringify(
17321724
this.testData.markers.map(marker => ({
17331725
marker,
17341726
quickInfo: this.languageService.getQuickInfoAtPosition(marker.fileName, marker.position)
1735-
}))
1736-
));
1727+
}))));
17371728
}
17381729

17391730
public printBreakpointLocation(pos: number) {
@@ -2347,10 +2338,7 @@ Actual: ${stringify(fullActual)}`);
23472338
public baselineCurrentFileNameOrDottedNameSpans() {
23482339
Harness.Baseline.runBaseline(
23492340
this.testData.globalOptions[MetadataOptionNames.baselineFile],
2350-
() => {
2351-
return this.baselineCurrentFileLocations(pos =>
2352-
this.getNameOrDottedNameSpan(pos)!);
2353-
});
2341+
this.baselineCurrentFileLocations(pos => this.getNameOrDottedNameSpan(pos)!));
23542342
}
23552343

23562344
public printNameOrDottedNameSpans(pos: number) {

src/harness/harness.ts

Lines changed: 49 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,14 +1483,9 @@ namespace Harness {
14831483
}
14841484

14851485
export function doErrorBaseline(baselinePath: string, inputFiles: ReadonlyArray<TestFile>, errors: ReadonlyArray<ts.Diagnostic>, pretty?: boolean) {
1486-
Baseline.runBaseline(baselinePath.replace(/\.tsx?$/, ".errors.txt"), (): string | null => {
1487-
if (!errors || (errors.length === 0)) {
1488-
/* tslint:disable:no-null-keyword */
1489-
return null;
1490-
/* tslint:enable:no-null-keyword */
1491-
}
1492-
return getErrorBaseline(inputFiles, errors, pretty);
1493-
});
1486+
Baseline.runBaseline(baselinePath.replace(/\.tsx?$/, ".errors.txt"),
1487+
// tslint:disable-next-line no-null-keyword
1488+
!errors || (errors.length === 0) ? null : getErrorBaseline(inputFiles, errors, pretty));
14941489
}
14951490

14961491
export function doTypeAndSymbolBaseline(baselinePath: string, program: ts.Program, allFiles: {unitName: string, content: string}[], opts?: Baseline.BaselineOptions, multifile?: boolean, skipTypeBaselines?: boolean, skipSymbolBaselines?: boolean) {
@@ -1552,7 +1547,7 @@ namespace Harness {
15521547

15531548
if (!multifile) {
15541549
const fullBaseLine = generateBaseLine(isSymbolBaseLine, isSymbolBaseLine ? skipSymbolBaselines : skipTypeBaselines);
1555-
Baseline.runBaseline(outputFileName + fullExtension, () => fullBaseLine, opts);
1550+
Baseline.runBaseline(outputFileName + fullExtension, fullBaseLine, opts);
15561551
}
15571552
else {
15581553
Baseline.runMultifileBaseline(outputFileName, fullExtension, () => {
@@ -1637,22 +1632,21 @@ namespace Harness {
16371632
throw new Error("Number of sourcemap files should be same as js files.");
16381633
}
16391634

1640-
Baseline.runBaseline(baselinePath.replace(/\.tsx?/, ".js.map"), () => {
1641-
if ((options.noEmitOnError && result.diagnostics.length !== 0) || result.maps.size === 0) {
1642-
// We need to return null here or the runBaseLine will actually create a empty file.
1643-
// Baselining isn't required here because there is no output.
1644-
/* tslint:disable:no-null-keyword */
1645-
return null;
1646-
/* tslint:enable:no-null-keyword */
1647-
}
1648-
1649-
let sourceMapCode = "";
1635+
let sourceMapCode: string | null;
1636+
if ((options.noEmitOnError && result.diagnostics.length !== 0) || result.maps.size === 0) {
1637+
// We need to return null here or the runBaseLine will actually create a empty file.
1638+
// Baselining isn't required here because there is no output.
1639+
/* tslint:disable:no-null-keyword */
1640+
sourceMapCode = null;
1641+
/* tslint:enable:no-null-keyword */
1642+
}
1643+
else {
1644+
sourceMapCode = "";
16501645
result.maps.forEach(sourceMap => {
16511646
sourceMapCode += fileOutput(sourceMap, harnessSettings);
16521647
});
1653-
1654-
return sourceMapCode;
1655-
});
1648+
}
1649+
Baseline.runBaseline(baselinePath.replace(/\.tsx?/, ".js.map"), sourceMapCode);
16561650
}
16571651
}
16581652

@@ -1662,49 +1656,40 @@ namespace Harness {
16621656
}
16631657

16641658
// check js output
1665-
Baseline.runBaseline(baselinePath.replace(/\.tsx?/, ts.Extension.Js), () => {
1666-
let tsCode = "";
1667-
const tsSources = otherFiles.concat(toBeCompiled);
1668-
if (tsSources.length > 1) {
1669-
tsCode += "//// [" + header + "] ////\r\n\r\n";
1670-
}
1671-
for (let i = 0; i < tsSources.length; i++) {
1672-
tsCode += "//// [" + ts.getBaseFileName(tsSources[i].unitName) + "]\r\n";
1673-
tsCode += tsSources[i].content + (i < (tsSources.length - 1) ? "\r\n" : "");
1674-
}
1659+
let tsCode = "";
1660+
const tsSources = otherFiles.concat(toBeCompiled);
1661+
if (tsSources.length > 1) {
1662+
tsCode += "//// [" + header + "] ////\r\n\r\n";
1663+
}
1664+
for (let i = 0; i < tsSources.length; i++) {
1665+
tsCode += "//// [" + ts.getBaseFileName(tsSources[i].unitName) + "]\r\n";
1666+
tsCode += tsSources[i].content + (i < (tsSources.length - 1) ? "\r\n" : "");
1667+
}
16751668

1676-
let jsCode = "";
1677-
result.js.forEach(file => {
1678-
jsCode += fileOutput(file, harnessSettings);
1679-
});
1669+
let jsCode = "";
1670+
result.js.forEach(file => {
1671+
jsCode += fileOutput(file, harnessSettings);
1672+
});
16801673

1681-
if (result.dts.size > 0) {
1682-
jsCode += "\r\n\r\n";
1683-
result.dts.forEach(declFile => {
1684-
jsCode += fileOutput(declFile, harnessSettings);
1685-
});
1686-
}
1674+
if (result.dts.size > 0) {
1675+
jsCode += "\r\n\r\n";
1676+
result.dts.forEach(declFile => {
1677+
jsCode += fileOutput(declFile, harnessSettings);
1678+
});
1679+
}
16871680

1688-
const declFileContext = prepareDeclarationCompilationContext(
1689-
toBeCompiled, otherFiles, result, harnessSettings, options, /*currentDirectory*/ undefined
1690-
);
1691-
const declFileCompilationResult = compileDeclarationFiles(declFileContext, result.symlinks);
1681+
const declFileContext = prepareDeclarationCompilationContext(
1682+
toBeCompiled, otherFiles, result, harnessSettings, options, /*currentDirectory*/ undefined
1683+
);
1684+
const declFileCompilationResult = compileDeclarationFiles(declFileContext, result.symlinks);
16921685

1693-
if (declFileCompilationResult && declFileCompilationResult.declResult.diagnostics.length) {
1694-
jsCode += "\r\n\r\n//// [DtsFileErrors]\r\n";
1695-
jsCode += "\r\n\r\n";
1696-
jsCode += getErrorBaseline(tsConfigFiles.concat(declFileCompilationResult.declInputFiles, declFileCompilationResult.declOtherFiles), declFileCompilationResult.declResult.diagnostics);
1697-
}
1686+
if (declFileCompilationResult && declFileCompilationResult.declResult.diagnostics.length) {
1687+
jsCode += "\r\n\r\n//// [DtsFileErrors]\r\n";
1688+
jsCode += "\r\n\r\n";
1689+
jsCode += getErrorBaseline(tsConfigFiles.concat(declFileCompilationResult.declInputFiles, declFileCompilationResult.declOtherFiles), declFileCompilationResult.declResult.diagnostics);
1690+
}
16981691

1699-
if (jsCode.length > 0) {
1700-
return tsCode + "\r\n\r\n" + jsCode;
1701-
}
1702-
else {
1703-
/* tslint:disable:no-null-keyword */
1704-
return null;
1705-
/* tslint:enable:no-null-keyword */
1706-
}
1707-
});
1692+
Baseline.runBaseline(baselinePath.replace(/\.tsx?/, ts.Extension.Js), jsCode.length > 0 ? tsCode + "\r\n\r\n" + jsCode : null); // tslint:disable-line no-null-keyword
17081693
}
17091694

17101695
function fileOutput(file: documents.TextDocument, harnessSettings: TestCaseParser.CompilerSettings): string {
@@ -2027,16 +2012,6 @@ namespace Harness {
20272012
}
20282013

20292014
const fileCache: { [idx: string]: boolean } = {};
2030-
function generateActual(generateContent: () => string | null): string | null {
2031-
2032-
const actual = generateContent();
2033-
2034-
if (actual === undefined) {
2035-
throw new Error("The generated content was \"undefined\". Return \"null\" if no baselining is required.\"");
2036-
}
2037-
2038-
return actual;
2039-
}
20402015

20412016
function compareToBaseline(actual: string | null, relativeFileName: string, opts: BaselineOptions | undefined) {
20422017
// actual is now either undefined (the generator had an error), null (no file requested),
@@ -2100,9 +2075,11 @@ namespace Harness {
21002075
}
21012076
}
21022077

2103-
export function runBaseline(relativeFileName: string, generateContent: () => string | null, opts?: BaselineOptions): void {
2078+
export function runBaseline(relativeFileName: string, actual: string | null, opts?: BaselineOptions): void {
21042079
const actualFileName = localPath(relativeFileName, opts && opts.Baselinefolder, opts && opts.Subfolder);
2105-
const actual = generateActual(generateContent);
2080+
if (actual === undefined) {
2081+
throw new Error("The generated content was \"undefined\". Return \"null\" if no baselining is required.\"");
2082+
}
21062083
const comparison = compareToBaseline(actual, relativeFileName, opts);
21072084
writeComparison(comparison.expected, comparison.actual, relativeFileName, actualFileName);
21082085
}

src/testRunner/compilerRunner.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -207,24 +207,19 @@ class CompilerTest {
207207

208208
public verifyModuleResolution() {
209209
if (this.options.traceResolution) {
210-
Harness.Baseline.runBaseline(this.justName.replace(/\.tsx?$/, ".trace.json"), () => {
211-
return utils.removeTestPathPrefixes(JSON.stringify(this.result.traces, undefined, 4));
212-
});
210+
Harness.Baseline.runBaseline(this.justName.replace(/\.tsx?$/, ".trace.json"),
211+
utils.removeTestPathPrefixes(JSON.stringify(this.result.traces, undefined, 4)));
213212
}
214213
}
215214

216215
public verifySourceMapRecord() {
217216
if (this.options.sourceMap || this.options.inlineSourceMap || this.options.declarationMap) {
218-
Harness.Baseline.runBaseline(this.justName.replace(/\.tsx?$/, ".sourcemap.txt"), () => {
219-
const record = utils.removeTestPathPrefixes(this.result.getSourceMapRecord()!);
220-
if ((this.options.noEmitOnError && this.result.diagnostics.length !== 0) || record === undefined) {
221-
// Because of the noEmitOnError option no files are created. We need to return null because baselining isn't required.
222-
/* tslint:disable:no-null-keyword */
223-
return null;
224-
/* tslint:enable:no-null-keyword */
225-
}
226-
return record;
227-
});
217+
const record = utils.removeTestPathPrefixes(this.result.getSourceMapRecord()!);
218+
const baseline = (this.options.noEmitOnError && this.result.diagnostics.length !== 0) || record === undefined
219+
// Because of the noEmitOnError option no files are created. We need to return null because baselining isn't required.
220+
? null // tslint:disable-line no-null-keyword
221+
: record;
222+
Harness.Baseline.runBaseline(this.justName.replace(/\.tsx?$/, ".sourcemap.txt"), baseline);
228223
}
229224
}
230225

src/testRunner/externalCompileRunner.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,7 @@ abstract class ExternalCompileRunnerBase extends RunnerBase {
8080
if (install.status !== 0) throw new Error(`NPM Install types for ${directoryName} failed: ${install.stderr.toString()}`);
8181
}
8282
args.push("--noEmit");
83-
Harness.Baseline.runBaseline(`${cls.kind()}/${directoryName}.log`, () => {
84-
return cls.report(cp.spawnSync(`node`, args, { cwd, timeout, shell: true }), cwd);
85-
});
83+
Harness.Baseline.runBaseline(`${cls.kind()}/${directoryName}.log`, cls.report(cp.spawnSync(`node`, args, { cwd, timeout, shell: true }), cwd));
8684
});
8785
});
8886
}

0 commit comments

Comments
 (0)