Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cf261c3

Browse files
committedNov 10, 2017
Retire Harness.compileFiles
1 parent 8245f62 commit cf261c3

File tree

6 files changed

+19
-170
lines changed

6 files changed

+19
-170
lines changed
 

‎src/harness/compiler.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace compiler {
1616
public readonly defaultLibLocation: string;
1717
public readonly outputs: TextDocument[] = [];
1818
public readonly traces: string[] = [];
19+
public readonly shouldAssertInvariants = !Harness.lightMode;
1920

2021
private _setParentNodes: boolean;
2122
private _sourceFiles: KeyedCollection<string, ts.SourceFile>;
@@ -130,7 +131,12 @@ namespace compiler {
130131
}
131132
}
132133

133-
const parsed = ts.createSourceFile(fileName, content, languageVersion, this._setParentNodes);
134+
135+
const parsed = ts.createSourceFile(fileName, content, languageVersion, this._setParentNodes || this.shouldAssertInvariants);
136+
if (this.shouldAssertInvariants) {
137+
Utils.assertInvariants(parsed, /*parent*/ undefined);
138+
}
139+
134140
this._sourceFiles.set(canonicalFileName, parsed);
135141

136142
if (cacheKey) {

‎src/harness/compilerRunner.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ class CompilerBaselineRunner extends RunnerBase {
4646
}
4747

4848
private makeUnitName(name: string, root: string) {
49-
const path = ts.toPath(name, root, (fileName) => Harness.Compiler.getCanonicalFileName(fileName));
50-
const pathStart = ts.toPath(Harness.IO.getCurrentDirectory(), "", (fileName) => Harness.Compiler.getCanonicalFileName(fileName));
49+
const path = ts.toPath(name, root, utils.identity);
50+
const pathStart = ts.toPath(Harness.IO.getCurrentDirectory(), "", utils.identity);
5151
return pathStart ? path.replace(pathStart, "/") : path;
5252
}
5353

‎src/harness/harness.ts

-156
Original file line numberDiff line numberDiff line change
@@ -1030,19 +1030,12 @@ namespace Harness {
10301030
return result;
10311031
}
10321032

1033-
const carriageReturnLineFeed = "\r\n";
1034-
const lineFeed = "\n";
1035-
10361033
export const defaultLibFileName = "lib.d.ts";
10371034
export const es2015DefaultLibFileName = "lib.es2015.d.ts";
10381035

10391036
// Cache of lib files from "built/local"
10401037
let libFileNameSourceFileMap: ts.Map<ts.SourceFile> | undefined;
10411038

1042-
// Cache of lib files from "tests/lib/"
1043-
const testLibFileNameSourceFileMap = ts.createMap<ts.SourceFile>();
1044-
const es6TestLibFileNameSourceFileMap = ts.createMap<ts.SourceFile>();
1045-
10461039
export function getDefaultLibrarySourceFile(fileName = defaultLibFileName): ts.SourceFile {
10471040
if (!isDefaultLibraryFile(fileName)) {
10481041
return undefined;
@@ -1084,155 +1077,6 @@ namespace Harness {
10841077
return fileName;
10851078
}
10861079

1087-
export function createCompilerHost(
1088-
inputFiles: TestFile[],
1089-
writeFile: (fn: string, contents: string, writeByteOrderMark: boolean) => void,
1090-
scriptTarget: ts.ScriptTarget,
1091-
useCaseSensitiveFileNames: boolean,
1092-
// the currentDirectory is needed for rwcRunner to passed in specified current directory to compiler host
1093-
currentDirectory: string,
1094-
newLineKind?: ts.NewLineKind,
1095-
libFiles?: string): ts.CompilerHost {
1096-
1097-
// Local get canonical file name function, that depends on passed in parameter for useCaseSensitiveFileNames
1098-
const getCanonicalFileName = ts.createGetCanonicalFileName(useCaseSensitiveFileNames);
1099-
1100-
/** Maps a symlink name to a realpath. Used only for exposing `realpath`. */
1101-
const realPathMap = ts.createMap<string>();
1102-
/**
1103-
* Maps a file name to a source file.
1104-
* This will have a different SourceFile for every symlink pointing to that file;
1105-
* if the program resolves realpaths then symlink entries will be ignored.
1106-
*/
1107-
const fileMap = ts.createMap<ts.SourceFile>();
1108-
for (const file of inputFiles) {
1109-
if (file.content !== undefined) {
1110-
const fileName = ts.normalizePath(file.unitName);
1111-
const path = ts.toPath(file.unitName, currentDirectory, getCanonicalFileName);
1112-
if (file.fileOptions && file.fileOptions.symlink) {
1113-
const links = file.fileOptions.symlink.split(",");
1114-
for (const link of links) {
1115-
const linkPath = ts.toPath(link, currentDirectory, getCanonicalFileName);
1116-
realPathMap.set(linkPath, fileName);
1117-
// Create a different SourceFile for every symlink.
1118-
const sourceFile = createSourceFileAndAssertInvariants(linkPath, file.content, scriptTarget);
1119-
fileMap.set(linkPath, sourceFile);
1120-
}
1121-
}
1122-
const sourceFile = createSourceFileAndAssertInvariants(fileName, file.content, scriptTarget);
1123-
fileMap.set(path, sourceFile);
1124-
}
1125-
}
1126-
1127-
if (libFiles) {
1128-
// Because @libFiles don't change between execution. We would cache the result of the files and reuse it to speed help compilation
1129-
for (const fileName of libFiles.split(",")) {
1130-
const libFileName = "tests/lib/" + fileName;
1131-
1132-
if (scriptTarget <= ts.ScriptTarget.ES5) {
1133-
if (!testLibFileNameSourceFileMap.get(libFileName)) {
1134-
testLibFileNameSourceFileMap.set(libFileName, createSourceFileAndAssertInvariants(libFileName, IO.readFile(libFileName), scriptTarget));
1135-
}
1136-
}
1137-
else {
1138-
if (!es6TestLibFileNameSourceFileMap.get(libFileName)) {
1139-
es6TestLibFileNameSourceFileMap.set(libFileName, createSourceFileAndAssertInvariants(libFileName, IO.readFile(libFileName), scriptTarget));
1140-
}
1141-
}
1142-
}
1143-
}
1144-
1145-
function getSourceFile(fileName: string) {
1146-
fileName = ts.normalizePath(fileName);
1147-
const fromFileMap = fileMap.get(toPath(fileName));
1148-
if (fromFileMap) {
1149-
return fromFileMap;
1150-
}
1151-
else if (fileName === fourslashFileName) {
1152-
const tsFn = "tests/cases/fourslash/" + fourslashFileName;
1153-
fourslashSourceFile = fourslashSourceFile || createSourceFileAndAssertInvariants(tsFn, Harness.IO.readFile(tsFn), scriptTarget);
1154-
return fourslashSourceFile;
1155-
}
1156-
else if (ts.startsWith(fileName, "tests/lib/")) {
1157-
return scriptTarget <= ts.ScriptTarget.ES5 ? testLibFileNameSourceFileMap.get(fileName) : es6TestLibFileNameSourceFileMap.get(fileName);
1158-
}
1159-
else {
1160-
// Don't throw here -- the compiler might be looking for a test that actually doesn't exist as part of the TC
1161-
// Return if it is other library file, otherwise return undefined
1162-
return getDefaultLibrarySourceFile(fileName);
1163-
}
1164-
}
1165-
1166-
const newLine =
1167-
newLineKind === ts.NewLineKind.CarriageReturnLineFeed ? carriageReturnLineFeed :
1168-
newLineKind === ts.NewLineKind.LineFeed ? lineFeed :
1169-
Harness.IO.newLine();
1170-
1171-
function toPath(fileName: string): ts.Path {
1172-
return ts.toPath(fileName, currentDirectory, getCanonicalFileName);
1173-
}
1174-
1175-
return {
1176-
getCurrentDirectory: () => currentDirectory,
1177-
getSourceFile,
1178-
getDefaultLibFileName,
1179-
writeFile,
1180-
getCanonicalFileName,
1181-
useCaseSensitiveFileNames: () => useCaseSensitiveFileNames,
1182-
getNewLine: () => newLine,
1183-
fileExists: fileName => fileMap.has(toPath(fileName)),
1184-
readFile(fileName: string): string | undefined {
1185-
const file = fileMap.get(toPath(fileName));
1186-
if (ts.endsWith(fileName, "json")) {
1187-
// strip comments
1188-
return file.getText();
1189-
}
1190-
return file.text;
1191-
},
1192-
realpath: (fileName: string): ts.Path => {
1193-
const path = toPath(fileName);
1194-
return (realPathMap.get(path) as ts.Path) || path;
1195-
},
1196-
directoryExists: dir => {
1197-
let path = ts.toPath(dir, currentDirectory, getCanonicalFileName);
1198-
// Strip trailing /, which may exist if the path is a drive root
1199-
if (path[path.length - 1] === "/") {
1200-
path = <ts.Path>path.substr(0, path.length - 1);
1201-
}
1202-
return mapHasFileInDirectory(path, fileMap);
1203-
},
1204-
getDirectories: d => {
1205-
const path = ts.toPath(d, currentDirectory, getCanonicalFileName);
1206-
const result: string[] = [];
1207-
ts.forEachKey(fileMap, key => {
1208-
if (key.indexOf(path) === 0 && key.lastIndexOf("/") > path.length) {
1209-
let dirName = key.substr(path.length, key.indexOf("/", path.length + 1) - path.length);
1210-
if (dirName[0] === "/") {
1211-
dirName = dirName.substr(1);
1212-
}
1213-
if (result.indexOf(dirName) < 0) {
1214-
result.push(dirName);
1215-
}
1216-
}
1217-
});
1218-
return result;
1219-
}
1220-
};
1221-
}
1222-
1223-
function mapHasFileInDirectory(directoryPath: ts.Path, map: ts.Map<{}>): boolean {
1224-
if (!map) {
1225-
return false;
1226-
}
1227-
let exists = false;
1228-
ts.forEachKey(map, fileName => {
1229-
if (!exists && ts.startsWith(fileName, directoryPath) && fileName[directoryPath.length] === "/") {
1230-
exists = true;
1231-
}
1232-
});
1233-
return exists;
1234-
}
1235-
12361080
interface HarnessOptions {
12371081
useCaseSensitiveFileNames?: boolean;
12381082
includeBuiltFile?: string;

‎src/harness/unittests/programMissingFiles.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/// <reference path="..\harness.ts" />
2+
/// <reference path="../vfs.ts" />
23

34
namespace ts {
45
function verifyMissingFilePaths(missingPaths: ReadonlyArray<Path>, expected: ReadonlyArray<string>) {
@@ -39,15 +40,11 @@ namespace ts {
3940
"/// <reference path=\"nonexistent4\"/>\n" // No extension
4041
};
4142

42-
const testCompilerHost = Harness.Compiler.createCompilerHost(
43-
/*inputFiles*/ [emptyFile, referenceFile],
44-
/*writeFile*/ undefined,
45-
/*scriptTarget*/ undefined,
46-
/*useCaseSensitiveFileNames*/ false,
47-
/*currentDirectory*/ "d:\\pretend\\",
48-
/*newLineKind*/ NewLineKind.LineFeed,
49-
/*libFiles*/ undefined
50-
);
43+
const testCompilerHost = new compiler.CompilerHost(
44+
vfs.VirtualFileSystem.createFromTestFiles(
45+
{ useCaseSensitiveFileNames: false, currentDirectory: "d:\\pretend\\" },
46+
[emptyFile, referenceFile]),
47+
{ newLine: NewLineKind.LineFeed });
5148

5249
it("handles no missing root files", () => {
5350
const program = createProgram([emptyFileRelativePath], options, testCompilerHost);

‎src/harness/utils.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
namespace utils {
2+
export function identity<T>(v: T) { return v; }
3+
24
export function getByteOrderMarkLength(text: string) {
35
if (text.length >= 2) {
46
const ch0 = text.charCodeAt(0);

‎src/harness/vfs.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1373,7 +1373,7 @@ namespace vfs {
13731373
}
13741374

13751375
export interface VirtualFile {
1376-
// #region Event "contentChanged"
1376+
// #region Event "contentChanged"
13771377
on(event: "contentChanged", listener: (entry: VirtualFile) => void): this;
13781378
once(event: "contentChanged", listener: (entry: VirtualFile) => void): this;
13791379
addListener(event: "contentChanged", listener: (entry: VirtualFile) => void): this;
@@ -1385,7 +1385,7 @@ namespace vfs {
13851385
}
13861386

13871387
export interface VirtualFile {
1388-
// #region Untyped events
1388+
// #region Untyped events
13891389
on(event: string | symbol, listener: (...args: any[]) => void): this;
13901390
once(event: string | symbol, listener: (...args: any[]) => void): this;
13911391
addListener(event: string | symbol, listener: (...args: any[]) => void): this;

0 commit comments

Comments
 (0)
Please sign in to comment.