Skip to content

Commit 791c01e

Browse files
committed
Deprecate GeneratedFile
1 parent cd0809e commit 791c01e

10 files changed

+71
-92
lines changed

src/harness/compiler.ts

+7-12
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,9 @@ namespace compiler {
236236
private _inputsAndOutputs: core.KeyedCollection<string, CompilationOutput>;
237237

238238
// from CompilerResult
239-
public readonly files: ReadonlyArray<Harness.Compiler.GeneratedFile>;
240-
public readonly declFilesCode: ReadonlyArray<Harness.Compiler.GeneratedFile>;
241-
public readonly sourceMaps: ReadonlyArray<Harness.Compiler.GeneratedFile>;
242-
public readonly errors: ReadonlyArray<ts.Diagnostic>;
243-
public readonly currentDirectoryForProgram: string;
244-
public readonly traceResults: ReadonlyArray<string>;
239+
public readonly files: ReadonlyArray<documents.TextDocument>;
240+
public readonly declFilesCode: ReadonlyArray<documents.TextDocument>;
241+
public readonly sourceMaps: ReadonlyArray<documents.TextDocument>;
245242

246243
constructor(host: CompilerHost, options: ts.CompilerOptions, program: ts.Program | undefined, result: ts.EmitResult | undefined, diagnostics: ts.Diagnostic[]) {
247244
this.host = host;
@@ -291,12 +288,10 @@ namespace compiler {
291288
}
292289

293290
// from CompilerResult
294-
this.files = Array.from(this.js.values(), file => file.asGeneratedFile());
295-
this.declFilesCode = Array.from(this.dts.values(), file => file.asGeneratedFile());
296-
this.sourceMaps = Array.from(this.maps.values(), file => file.asGeneratedFile());
297-
this.errors = diagnostics;
298-
this.currentDirectoryForProgram = host.vfs.currentDirectory;
299-
this.traceResults = host.traces;
291+
this.files = Array.from(this.js.values());
292+
this.declFilesCode = Array.from(this.dts.values());
293+
this.sourceMaps = Array.from(this.maps.values());
294+
this.diagnostics = diagnostics;
300295
}
301296

302297
public get vfs(): vfs.VirtualFileSystem {

src/harness/compilerRunner.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,14 @@ class CompilerTest {
208208
Harness.Compiler.doErrorBaseline(
209209
this.justName,
210210
this.tsConfigFiles.concat(this.toBeCompiled, this.otherFiles),
211-
this.result.errors,
211+
this.result.diagnostics,
212212
!!this.options.pretty);
213213
}
214214

215215
public verifyModuleResolution() {
216216
if (this.options.traceResolution) {
217217
Harness.Baseline.runBaseline(this.justName.replace(/\.tsx?$/, ".trace.json"), () => {
218-
return utils.removeTestPathPrefixes(JSON.stringify(this.result.traceResults || [], undefined, 4));
218+
return utils.removeTestPathPrefixes(JSON.stringify(this.result.traces, undefined, 4));
219219
});
220220
}
221221
}
@@ -224,7 +224,7 @@ class CompilerTest {
224224
if (this.options.sourceMap || this.options.inlineSourceMap) {
225225
Harness.Baseline.runBaseline(this.justName.replace(/\.tsx?$/, ".sourcemap.txt"), () => {
226226
const record = utils.removeTestPathPrefixes(this.result.getSourceMapRecord());
227-
if ((this.options.noEmitOnError && this.result.errors.length !== 0) || record === undefined) {
227+
if ((this.options.noEmitOnError && this.result.diagnostics.length !== 0) || record === undefined) {
228228
// Because of the noEmitOnError option no files are created. We need to return null because baselining isn't required.
229229
/* tslint:disable:no-null-keyword */
230230
return null;

src/harness/core.ts

+5
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,11 @@ namespace core {
357357
return text;
358358
}
359359

360+
export function getByteOrderMark(text: string): string {
361+
const length = getByteOrderMarkLength(text);
362+
return length > 0 ? text.slice(0, length) : "";
363+
}
364+
360365
export function getByteOrderMarkLength(text: string): number {
361366
if (text.length >= 2) {
362367
const ch0 = text.charCodeAt(0);

src/harness/documents.ts

-15
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ namespace documents {
1111

1212
private _lineStarts: core.LineStarts | undefined;
1313
private _testFile: Harness.Compiler.TestFile | undefined;
14-
private _generatedFile: Harness.Compiler.GeneratedFile | undefined;
1514

1615
constructor(file: string, text: string, meta?: Map<string, string>) {
1716
this.file = file;
@@ -39,20 +38,6 @@ namespace documents {
3938
.reduce((obj, [key, value]) => (obj[key] = value, obj), {} as Record<string, string>)
4039
});
4140
}
42-
43-
public static fromGeneratedFile(file: Harness.Compiler.GeneratedFile) {
44-
return new TextDocument(
45-
file.fileName,
46-
file.writeByteOrderMark ? core.addUTF8ByteOrderMark(file.code) : file.code);
47-
}
48-
49-
public asGeneratedFile() {
50-
return this._generatedFile || (this._generatedFile = {
51-
fileName: this.file,
52-
code: core.removeByteOrderMark(this.text),
53-
writeByteOrderMark: core.getByteOrderMarkLength(this.text) > 0
54-
});
55-
}
5641
}
5742

5843
export interface RawSourceMap {

src/harness/harness.ts

+17-27
Original file line numberDiff line numberDiff line change
@@ -1244,15 +1244,15 @@ namespace Harness {
12441244
options: ts.CompilerOptions,
12451245
// Current directory is needed for rwcRunner to be able to use currentDirectory defined in json file
12461246
currentDirectory: string): DeclarationCompilationContext | undefined {
1247-
if (options.declaration && result.errors.length === 0 && result.declFilesCode.length !== result.files.length) {
1247+
if (options.declaration && result.diagnostics.length === 0 && result.declFilesCode.length !== result.files.length) {
12481248
throw new Error("There were no errors and declFiles generated did not match number of js files generated");
12491249
}
12501250

12511251
const declInputFiles: TestFile[] = [];
12521252
const declOtherFiles: TestFile[] = [];
12531253

12541254
// if the .d.ts is non-empty, confirm it compiles correctly as well
1255-
if (options.declaration && result.errors.length === 0 && result.declFilesCode.length > 0) {
1255+
if (options.declaration && result.diagnostics.length === 0 && result.declFilesCode.length > 0) {
12561256
ts.forEach(inputFiles, file => addDtsFile(file, declInputFiles));
12571257
ts.forEach(otherFiles, file => addDtsFile(file, declOtherFiles));
12581258
return { declInputFiles, declOtherFiles, harnessSettings, options, currentDirectory: currentDirectory || harnessSettings.currentDirectory };
@@ -1264,8 +1264,8 @@ namespace Harness {
12641264
}
12651265
else if (vpath.isTypeScript(file.unitName)) {
12661266
const declFile = findResultCodeFile(file.unitName);
1267-
if (declFile && !findUnit(declFile.fileName, declInputFiles) && !findUnit(declFile.fileName, declOtherFiles)) {
1268-
dtsFiles.push({ unitName: declFile.fileName, content: declFile.code });
1267+
if (declFile && !findUnit(declFile.file, declInputFiles) && !findUnit(declFile.file, declOtherFiles)) {
1268+
dtsFiles.push({ unitName: declFile.file, content: core.removeByteOrderMark(declFile.text) });
12691269
}
12701270
}
12711271
}
@@ -1278,7 +1278,7 @@ namespace Harness {
12781278
const outFile = options.outFile || options.out;
12791279
if (!outFile) {
12801280
if (options.outDir) {
1281-
let sourceFilePath = ts.getNormalizedAbsolutePath(sourceFile.fileName, result.currentDirectoryForProgram);
1281+
let sourceFilePath = ts.getNormalizedAbsolutePath(sourceFile.fileName, result.vfs.currentDirectory);
12821282
sourceFilePath = sourceFilePath.replace(result.program.getCommonSourceDirectory(), "");
12831283
sourceFileName = ts.combinePaths(options.outDir, sourceFilePath);
12841284
}
@@ -1293,7 +1293,7 @@ namespace Harness {
12931293

12941294
const dTsFileName = ts.removeFileExtension(sourceFileName) + ts.Extension.Dts;
12951295

1296-
return ts.forEach(result.declFilesCode, declFile => declFile.fileName === dTsFileName ? declFile : undefined);
1296+
return ts.forEach(result.declFilesCode, declFile => declFile.file === dTsFileName ? declFile : undefined);
12971297
}
12981298

12991299
function findUnit(fileName: string, units: TestFile[]) {
@@ -1602,10 +1602,6 @@ namespace Harness {
16021602
}
16031603
}
16041604

1605-
function getByteOrderMarkText(file: Harness.Compiler.GeneratedFile): string {
1606-
return file.writeByteOrderMark ? "\u00EF\u00BB\u00BF" : "";
1607-
}
1608-
16091605
export function doSourcemapBaseline(baselinePath: string, options: ts.CompilerOptions, result: compiler.CompilationResult, harnessSettings: Harness.TestCaseParser.CompilerSettings) {
16101606
if (options.inlineSourceMap) {
16111607
if (result.sourceMaps.length > 0) {
@@ -1619,7 +1615,7 @@ namespace Harness {
16191615
}
16201616

16211617
Harness.Baseline.runBaseline(baselinePath.replace(/\.tsx?/, ".js.map"), () => {
1622-
if ((options.noEmitOnError && result.errors.length !== 0) || result.sourceMaps.length === 0) {
1618+
if ((options.noEmitOnError && result.diagnostics.length !== 0) || result.sourceMaps.length === 0) {
16231619
// We need to return null here or the runBaseLine will actually create a empty file.
16241620
// Baselining isn't required here because there is no output.
16251621
/* tslint:disable:no-null-keyword */
@@ -1638,7 +1634,7 @@ namespace Harness {
16381634
}
16391635

16401636
export function doJsEmitBaseline(baselinePath: string, header: string, options: ts.CompilerOptions, result: compiler.CompilationResult, tsConfigFiles: ReadonlyArray<Harness.Compiler.TestFile>, toBeCompiled: ReadonlyArray<Harness.Compiler.TestFile>, otherFiles: ReadonlyArray<Harness.Compiler.TestFile>, harnessSettings: Harness.TestCaseParser.CompilerSettings) {
1641-
if (!options.noEmit && result.files.length === 0 && result.errors.length === 0) {
1637+
if (!options.noEmit && result.files.length === 0 && result.diagnostics.length === 0) {
16421638
throw new Error("Expected at least one js file to be emitted or at least one error to be created.");
16431639
}
16441640

@@ -1671,10 +1667,10 @@ namespace Harness {
16711667
);
16721668
const declFileCompilationResult = Harness.Compiler.compileDeclarationFiles(declFileContext);
16731669

1674-
if (declFileCompilationResult && declFileCompilationResult.declResult.errors.length) {
1670+
if (declFileCompilationResult && declFileCompilationResult.declResult.diagnostics.length) {
16751671
jsCode += "\r\n\r\n//// [DtsFileErrors]\r\n";
16761672
jsCode += "\r\n\r\n";
1677-
jsCode += Harness.Compiler.getErrorBaseline(tsConfigFiles.concat(declFileCompilationResult.declInputFiles, declFileCompilationResult.declOtherFiles), declFileCompilationResult.declResult.errors);
1673+
jsCode += Harness.Compiler.getErrorBaseline(tsConfigFiles.concat(declFileCompilationResult.declInputFiles, declFileCompilationResult.declOtherFiles), declFileCompilationResult.declResult.diagnostics);
16781674
}
16791675

16801676
if (jsCode.length > 0) {
@@ -1688,12 +1684,12 @@ namespace Harness {
16881684
});
16891685
}
16901686

1691-
function fileOutput(file: GeneratedFile, harnessSettings: Harness.TestCaseParser.CompilerSettings): string {
1692-
const fileName = harnessSettings.fullEmitPaths ? utils.removeTestPathPrefixes(file.fileName) : ts.getBaseFileName(file.fileName);
1693-
return "//// [" + fileName + "]\r\n" + getByteOrderMarkText(file) + utils.removeTestPathPrefixes(file.code);
1687+
function fileOutput(file: documents.TextDocument, harnessSettings: Harness.TestCaseParser.CompilerSettings): string {
1688+
const fileName = harnessSettings.fullEmitPaths ? utils.removeTestPathPrefixes(file.file) : ts.getBaseFileName(file.file);
1689+
return "//// [" + fileName + "]\r\n" + utils.removeTestPathPrefixes(file.text);
16941690
}
16951691

1696-
export function collateOutputs(outputFiles: ReadonlyArray<Harness.Compiler.GeneratedFile>): string {
1692+
export function collateOutputs(outputFiles: ReadonlyArray<documents.TextDocument>): string {
16971693
const gen = iterateOutputs(outputFiles);
16981694
// Emit them
16991695
let result = "";
@@ -1709,13 +1705,13 @@ namespace Harness {
17091705
return result;
17101706
}
17111707

1712-
export function *iterateOutputs(outputFiles: ReadonlyArray<Harness.Compiler.GeneratedFile>): IterableIterator<[string, string]> {
1708+
export function *iterateOutputs(outputFiles: ReadonlyArray<documents.TextDocument>): IterableIterator<[string, string]> {
17131709
// Collect, test, and sort the fileNames
1714-
outputFiles.slice().sort((a, b) => ts.compareStringsCaseSensitive(cleanName(a.fileName), cleanName(b.fileName)));
1710+
outputFiles.slice().sort((a, b) => ts.compareStringsCaseSensitive(cleanName(a.file), cleanName(b.file)));
17151711
const dupeCase = ts.createMap<number>();
17161712
// Yield them
17171713
for (const outputFile of outputFiles) {
1718-
yield [checkDuplicatedFileName(outputFile.fileName, dupeCase), "/*====== " + outputFile.fileName + " ======*/\r\n" + outputFile.code];
1714+
yield [checkDuplicatedFileName(outputFile.file, dupeCase), "/*====== " + outputFile.file + " ======*/\r\n" + core.removeByteOrderMark(outputFile.text)];
17191715
}
17201716

17211717
function cleanName(fn: string) {
@@ -1745,12 +1741,6 @@ namespace Harness {
17451741
}
17461742
return path;
17471743
}
1748-
1749-
export interface GeneratedFile {
1750-
fileName: string;
1751-
code: string;
1752-
writeByteOrderMark: boolean;
1753-
}
17541744
}
17551745

17561746
export namespace TestCaseParser {

0 commit comments

Comments
 (0)