Skip to content

Commit 6b79161

Browse files
committed
More cleanup and reorganization
1 parent 0322d71 commit 6b79161

13 files changed

Lines changed: 298 additions & 455 deletions

src/harness/compiler.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
/// <reference path="./core.ts" />
44
/// <reference path="./vpath.ts" />
55
/// <reference path="./vfs.ts" />
6-
/// <reference path="./vfsutils.ts" />
76
/// <reference path="./utils.ts" />
87
/// <reference path="./fakes.ts" />
98

@@ -79,13 +78,13 @@ namespace compiler {
7978
const dts = this.dts = new core.SortedMap<string, documents.TextDocument>({ comparer: this.vfs.stringComparer, sort: "insertion" });
8079
const maps = this.maps = new core.SortedMap<string, documents.TextDocument>({ comparer: this.vfs.stringComparer, sort: "insertion" });
8180
for (const document of this.host.outputs) {
82-
if (vfsutils.isJavaScript(document.file)) {
81+
if (vpath.isJavaScript(document.file)) {
8382
js.set(document.file, document);
8483
}
85-
else if (vfsutils.isDeclaration(document.file)) {
84+
else if (vpath.isDeclaration(document.file)) {
8685
dts.set(document.file, document);
8786
}
88-
else if (vfsutils.isSourceMap(document.file)) {
87+
else if (vpath.isSourceMap(document.file)) {
8988
maps.set(document.file, document);
9089
}
9190
}
@@ -100,7 +99,7 @@ namespace compiler {
10099
if (sourceFile) {
101100
const input = new documents.TextDocument(sourceFile.fileName, sourceFile.text);
102101
this._inputs.push(input);
103-
if (!vfsutils.isDeclaration(sourceFile.fileName)) {
102+
if (!vpath.isDeclaration(sourceFile.fileName)) {
104103
inputs.push(input);
105104
}
106105
}
@@ -126,7 +125,7 @@ namespace compiler {
126125
if (sourceFile) {
127126
const input = new documents.TextDocument(sourceFile.fileName, sourceFile.text);
128127
this._inputs.push(input);
129-
if (!vfsutils.isDeclaration(sourceFile.fileName)) {
128+
if (!vpath.isDeclaration(sourceFile.fileName)) {
130129
const extname = ts.getOutputExtension(sourceFile, this.options);
131130
const outputs: CompilationOutput = {
132131
inputs: [input],
@@ -198,7 +197,7 @@ namespace compiler {
198197
}
199198

200199
public getSourceMap(path: string): documents.SourceMap | undefined {
201-
if (this.options.noEmit || vfsutils.isDeclaration(path)) return undefined;
200+
if (this.options.noEmit || vpath.isDeclaration(path)) return undefined;
202201
if (this.options.inlineSourceMap) {
203202
const document = this.getOutput(path, "js");
204203
return document && documents.SourceMap.fromSource(document.text);

src/harness/fakes.ts

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace fakes {
2929
private readonly _executingFilePath: string | undefined;
3030
private readonly _env: Record<string, string> | undefined;
3131

32-
constructor(vfs: vfs.FileSystem, { executingFilePath, newLine = "\n", env }: SystemOptions = {}) {
32+
constructor(vfs: vfs.FileSystem, { executingFilePath, newLine = "\r\n", env }: SystemOptions = {}) {
3333
this.vfs = vfs.isReadonly ? vfs.shadow() : vfs;
3434
this.useCaseSensitiveFileNames = !this.vfs.ignoreCase;
3535
this.newLine = newLine;
@@ -46,7 +46,7 @@ namespace fakes {
4646
const content = this.vfs.readFileSync(path, "utf8");
4747
return content === undefined ? undefined :
4848
vpath.extname(path) === ".json" ? utils.removeComments(core.removeByteOrderMark(content), utils.CommentRemoval.leadingAndTrailing) :
49-
core.removeByteOrderMark(content);
49+
core.removeByteOrderMark(content);
5050
}
5151
catch {
5252
return undefined;
@@ -90,26 +90,28 @@ namespace fakes {
9090
}
9191

9292
public readDirectory(path: string, extensions?: ReadonlyArray<string>, exclude?: ReadonlyArray<string>, include?: ReadonlyArray<string>, depth?: number): string[] {
93-
return ts.matchFiles(path, extensions, exclude, include, this.useCaseSensitiveFileNames, this.getCurrentDirectory(), depth, path => {
94-
const files: string[] = [];
95-
const directories: string[] = [];
96-
try {
97-
for (const file of this.vfs.readdirSync(path)) {
98-
try {
99-
const stats = this.vfs.statSync(vpath.combine(path, file));
100-
if (stats.isFile()) {
101-
files.push(file);
102-
}
103-
else if (stats.isDirectory()) {
104-
directories.push(file);
105-
}
93+
return ts.matchFiles(path, extensions, exclude, include, this.useCaseSensitiveFileNames, this.getCurrentDirectory(), depth, path => this.getAccessibleFileSystemEntries(path));
94+
}
95+
96+
public getAccessibleFileSystemEntries(path: string): ts.FileSystemEntries {
97+
const files: string[] = [];
98+
const directories: string[] = [];
99+
try {
100+
for (const file of this.vfs.readdirSync(path)) {
101+
try {
102+
const stats = this.vfs.statSync(vpath.combine(path, file));
103+
if (stats.isFile()) {
104+
files.push(file);
105+
}
106+
else if (stats.isDirectory()) {
107+
directories.push(file);
106108
}
107-
catch { /*ignored*/ }
108109
}
110+
catch { /*ignored*/ }
109111
}
110-
catch { /*ignored*/ }
111-
return { files, directories };
112-
});
112+
}
113+
catch { /*ignored*/ }
114+
return { files, directories };
113115
}
114116

115117
public exit(exitCode?: number) {
@@ -214,8 +216,8 @@ namespace fakes {
214216
private _parseConfigHost: ParseConfigHost;
215217
private _newLine: string;
216218

217-
constructor(sys: System | vfs.FileSystem, options: ts.CompilerOptions, setParentNodes = false) {
218-
if (sys instanceof vfs.FileSystem) sys = new System(sys, { newLine: "\r\n" });
219+
constructor(sys: System | vfs.FileSystem, options = ts.getDefaultCompilerOptions(), setParentNodes = false) {
220+
if (sys instanceof vfs.FileSystem) sys = new System(sys);
219221
this.sys = sys;
220222
this.defaultLibLocation = sys.vfs.meta.get("defaultLibLocation") || "";
221223
this._newLine = ts.getNewLineCharacter(options, () => this.sys.newLine);

src/harness/fourslash.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,13 @@ namespace FourSlash {
276276

277277
if (configFileName) {
278278
const baseDir = ts.normalizePath(ts.getDirectoryPath(configFileName));
279-
const host = new fakes.ParseConfigHost(vfsutils.createFromMap(baseDir, /*ignoreCase*/ true, this.inputFiles));
279+
const files: vfs.FileSet = { [baseDir]: {} };
280+
this.inputFiles.forEach((data, path) => {
281+
const scriptInfo = new Harness.LanguageService.ScriptInfo(path, undefined, /*isRootFile*/ false);
282+
files[path] = new vfs.File(data, { meta: { scriptInfo } });
283+
});
284+
const fs = new vfs.FileSystem(/*ignoreCase*/ true, { cwd: baseDir, files });
285+
const host = new fakes.ParseConfigHost(fs);
280286

281287
const configJsonObj = ts.parseConfigFileTextToJson(configFileName, this.inputFiles.get(configFileName));
282288
assert.isTrue(configJsonObj.config !== undefined);

src/harness/harness.ts

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
/// <reference path="sourceMapRecorder.ts"/>
2222
/// <reference path="runnerbase.ts"/>
2323
/// <reference path="./vfs.ts" />
24-
/// <reference path="./vfsutils.ts" />
2524
/// <reference types="node" />
2625
/// <reference types="mocha" />
2726
/// <reference types="chai" />
@@ -525,17 +524,12 @@ namespace Harness {
525524
getExecutingFilePath(): string;
526525
exit(exitCode?: number): void;
527526
readDirectory(path: string, extension?: ReadonlyArray<string>, exclude?: ReadonlyArray<string>, include?: ReadonlyArray<string>, depth?: number): string[];
528-
getAccessibleFileSystemEntries(dirname: string): FileSystemEntries;
527+
getAccessibleFileSystemEntries(dirname: string): ts.FileSystemEntries;
529528
tryEnableSourceMapsForHost?(): void;
530529
getEnvironmentVariable?(name: string): string;
531530
getMemoryUsage?(): number;
532531
}
533532

534-
export interface FileSystemEntries {
535-
files: string[];
536-
directories: string[];
537-
}
538-
539533
export let IO: IO;
540534

541535
// harness always uses one kind of new line
@@ -591,7 +585,7 @@ namespace Harness {
591585
return filesInFolder(path);
592586
}
593587

594-
function getAccessibleFileSystemEntries(dirname: string): FileSystemEntries {
588+
function getAccessibleFileSystemEntries(dirname: string): ts.FileSystemEntries {
595589
try {
596590
const entries: string[] = fs.readdirSync(dirname || ".").sort(ts.sys.useCaseSensitiveFileNames ? ts.compareStringsCaseSensitive : ts.compareStringsCaseInsensitive);
597591
const files: string[] = [];
@@ -944,20 +938,22 @@ namespace Harness {
944938

945939
function readDirectory(path: string, extension?: string[], exclude?: string[], include?: string[], depth?: number) {
946940
const fs = new vfs.FileSystem(!useCaseSensitiveFileNames(), { cwd: path, files: { [path]: { } } });
941+
const sys = new fakes.System(fs);
947942
for (const file of IO.listFiles(path)) {
948943
fs.mkdirpSync(vpath.dirname(file));
949944
fs.writeFileSync(file, "");
950945
}
951-
return ts.matchFiles(path, extension, exclude, include, useCaseSensitiveFileNames(), /*currentDirectory*/ "", depth, path => vfsutils.getAccessibleFileSystemEntries(fs, path));
946+
return sys.readDirectory(path, extension, exclude, include, depth);
952947
}
953948

954-
function getAccessibleFileSystemEntries(dirname: string): FileSystemEntries {
949+
function getAccessibleFileSystemEntries(dirname: string): ts.FileSystemEntries {
955950
const fs = new vfs.FileSystem(!useCaseSensitiveFileNames(), { cwd: dirname, files: { [dirname]: {} } });
951+
const sys = new fakes.System(fs);
956952
for (const file of IO.listFiles(path)) {
957953
fs.mkdirpSync(vpath.dirname(file));
958954
fs.writeFileSync(file, "");
959955
}
960-
return vfsutils.getAccessibleFileSystemEntries(fs, dirname);
956+
return sys.getAccessibleFileSystemEntries(dirname);
961957
}
962958

963959
return {
@@ -1220,7 +1216,7 @@ namespace Harness {
12201216
options.skipDefaultLibCheck = typeof options.skipDefaultLibCheck === "undefined" ? true : options.skipDefaultLibCheck;
12211217

12221218
if (typeof currentDirectory === "undefined") {
1223-
currentDirectory = vfsutils.srcFolder;
1219+
currentDirectory = vfs.srcFolder;
12241220
}
12251221

12261222
// Parse settings
@@ -1237,27 +1233,20 @@ namespace Harness {
12371233
// Files from built\local that are requested by test "@includeBuiltFiles" to be in the context.
12381234
// Treat them as library files, so include them in build, but not in baselines.
12391235
if (options.includeBuiltFile) {
1240-
programFileNames.push(vpath.combine(vfsutils.builtFolder, options.includeBuiltFile));
1236+
programFileNames.push(vpath.combine(vfs.builtFolder, options.includeBuiltFile));
12411237
}
12421238

12431239
// Files from tests\lib that are requested by "@libFiles"
12441240
if (options.libFiles) {
12451241
for (const fileName of options.libFiles.split(",")) {
1246-
programFileNames.push(vpath.combine(vfsutils.testLibFolder, fileName));
1242+
programFileNames.push(vpath.combine(vfs.testLibFolder, fileName));
12471243
}
12481244
}
12491245

1250-
return compiler.compileFiles(
1251-
new fakes.CompilerHost(
1252-
vfsutils.createFromDocuments(
1253-
useCaseSensitiveFileNames,
1254-
inputFiles.concat(otherFiles).map(documents.TextDocument.fromTestFile),
1255-
{ currentDirectory, overwrite: true }
1256-
),
1257-
options
1258-
),
1259-
programFileNames,
1260-
options);
1246+
const docs = inputFiles.concat(otherFiles).map(documents.TextDocument.fromTestFile);
1247+
const fs = vfs.FileSystem.createFromFileSystem(IO, !useCaseSensitiveFileNames, { documents: docs, cwd: currentDirectory });
1248+
const host = new fakes.CompilerHost(fs, options);
1249+
return compiler.compileFiles(host, programFileNames, options);
12611250
}
12621251

12631252
export interface DeclarationCompilationContext {
@@ -1298,10 +1287,10 @@ namespace Harness {
12981287
}
12991288

13001289
function addDtsFile(file: TestFile, dtsFiles: TestFile[]) {
1301-
if (vfsutils.isDeclaration(file.unitName)) {
1290+
if (vpath.isDeclaration(file.unitName)) {
13021291
dtsFiles.push(file);
13031292
}
1304-
else if (vfsutils.isTypeScript(file.unitName)) {
1293+
else if (vpath.isTypeScript(file.unitName)) {
13051294
const declFile = findResultCodeFile(file.unitName);
13061295
if (declFile && !findUnit(declFile.file, declInputFiles) && !findUnit(declFile.file, declOtherFiles)) {
13071296
dtsFiles.push({ unitName: declFile.file, content: core.removeByteOrderMark(declFile.text) });
@@ -2114,7 +2103,7 @@ namespace Harness {
21142103

21152104
export function isBuiltFile(filePath: string): boolean {
21162105
return filePath.indexOf(libFolder) === 0 ||
2117-
filePath.indexOf(vpath.addTrailingSeparator(vfsutils.builtFolder)) === 0;
2106+
filePath.indexOf(vpath.addTrailingSeparator(vfs.builtFolder)) === 0;
21182107
}
21192108

21202109
export function getDefaultLibraryFile(filePath: string, io: IO): Compiler.TestFile {

src/harness/harnessLanguageService.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ namespace Harness.LanguageService {
117117
}
118118

119119
export abstract class LanguageServiceAdapterHost {
120-
public readonly vfs = new vfs.FileSystem(/*ignoreCase*/ true, { cwd: virtualFileSystemRoot });
120+
public readonly sys = new fakes.System(new vfs.FileSystem(/*ignoreCase*/ true, { cwd: virtualFileSystemRoot }));
121121
public typesRegistry: ts.Map<void> | undefined;
122122
private scriptInfos: core.SortedMap<string, ScriptInfo>;
123123

@@ -126,6 +126,10 @@ namespace Harness.LanguageService {
126126
this.scriptInfos = new core.SortedMap({ comparer: this.vfs.stringComparer, sort: "insertion" });
127127
}
128128

129+
public get vfs() {
130+
return this.sys.vfs;
131+
}
132+
129133
public getNewLine(): string {
130134
return harnessNewLine;
131135
}
@@ -191,7 +195,7 @@ namespace Harness.LanguageService {
191195
getCancellationToken() { return this.cancellationToken; }
192196

193197
getDirectories(path: string): string[] {
194-
return vfsutils.getDirectories(this.vfs, path);
198+
return this.sys.getDirectories(path);
195199
}
196200

197201
getCurrentDirectory(): string { return virtualFileSystemRoot; }
@@ -215,27 +219,23 @@ namespace Harness.LanguageService {
215219
}
216220

217221
directoryExists(dirName: string): boolean {
218-
return vfsutils.directoryExists(this.vfs, dirName);
222+
return this.sys.directoryExists(dirName);
219223
}
220224

221225
fileExists(fileName: string): boolean {
222-
return vfsutils.fileExists(this.vfs, fileName);
226+
return this.sys.fileExists(fileName);
223227
}
224228

225229
readDirectory(path: string, extensions?: ReadonlyArray<string>, exclude?: ReadonlyArray<string>, include?: ReadonlyArray<string>, depth?: number): string[] {
226-
return ts.matchFiles(path, extensions, exclude, include,
227-
/*useCaseSensitiveFileNames*/ false,
228-
this.getCurrentDirectory(),
229-
depth,
230-
(p) => vfsutils.getAccessibleFileSystemEntries(this.vfs, p));
230+
return this.sys.readDirectory(path, extensions, exclude, include, depth);
231231
}
232232

233233
readFile(path: string): string | undefined {
234-
return vfsutils.readFile(this.vfs, path);
234+
return this.sys.readFile(path);
235235
}
236236

237237
realpath(path: string): string {
238-
return this.vfs.realpathSync(path);
238+
return this.sys.realpath(path);
239239
}
240240

241241
getTypeRootsVersion() {

0 commit comments

Comments
 (0)