Skip to content

Commit c5e5020

Browse files
committed
Migrate tsserverProjectSystem to vfs
1 parent fa42835 commit c5e5020

10 files changed

Lines changed: 5616 additions & 5411 deletions

File tree

scripts/typemock/src/timers.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,16 @@ export class Timers {
124124
* - Use `Timers.MAX_DEPTH` to continue processing nested `setImmediate` calls up to the maximum depth.
125125
*/
126126
public advance(ms: number, maxDepth = 0): number {
127-
if (ms <= 0) throw new TypeError("Argument 'ms' out of range.");
127+
if (ms < 0) throw new TypeError("Argument 'ms' out of range.");
128128
if (maxDepth < 0) throw new TypeError("Argument 'maxDepth' out of range.");
129129
let count = 0;
130130
const endTime = this._time + (ms | 0);
131131
while (true) {
132-
count += this.executeImmediates(maxDepth);
132+
if (maxDepth >= 0) {
133+
count += this.executeImmediates(maxDepth);
134+
maxDepth--;
135+
}
136+
133137
const dueTimer = this.dequeueIfBefore(endTime);
134138
if (dueTimer) {
135139
this._time = dueTimer.due;
@@ -150,7 +154,7 @@ export class Timers {
150154
* - Use `Timers.MAX_DEPTH` to continue processing nested `setImmediate` calls up to the maximum depth.
151155
*/
152156
public advanceToEnd(maxDepth = 0) {
153-
return this.remainingTime > 0 ? this.advance(this.remainingTime, maxDepth) : 0;
157+
return this.advance(this.remainingTime, maxDepth);
154158
}
155159

156160
/**

src/harness/fakes.ts

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,18 @@ namespace fakes {
3030
* Indicates whether to include a bare _lib.d.ts_.
3131
*/
3232
lib?: boolean;
33+
/**
34+
* Indicates whether to use DOS paths by default.
35+
*/
36+
dos?: boolean;
3337
}
3438

35-
export class FakeServerHost implements ts.server.ServerHost, ts.FormatDiagnosticsHost {
39+
export class FakeServerHost implements ts.server.ServerHost, ts.FormatDiagnosticsHost, ts.ModuleResolutionHost {
40+
public static readonly dosExecutingFilePath = "c:/.ts/tsc.js";
3641
public static readonly defaultExecutingFilePath = "/.ts/tsc.js";
42+
public static readonly dosDefaultCurrentDirectory = "c:/";
3743
public static readonly defaultCurrentDirectory = "/";
44+
public static readonly dosSafeListPath = "c:/safelist.json";
3845
public static readonly safeListPath = "/safelist.json";
3946
public static readonly safeListContent =
4047
`{\n` +
@@ -46,6 +53,7 @@ namespace fakes {
4653
` "chroma": "chroma-js"\n` +
4754
`}`;
4855

56+
public static readonly dosLibPath = "c:/.ts/lib.d.ts";
4957
public static readonly libPath = "/.ts/lib.d.ts";
5058
public static readonly libContent =
5159
`/// <reference no-default-lib="true"/>\n` +
@@ -64,34 +72,48 @@ namespace fakes {
6472

6573
private static readonly processExitSentinel = new Error("System exit");
6674
private readonly _output: string[] = [];
75+
private readonly _trace: string[] = [];
6776
private readonly _executingFilePath: string;
6877
private readonly _getCanonicalFileName: (file: string) => string;
6978

7079
constructor(options: FakeServerHostOptions = {}) {
7180
const {
81+
dos = false,
7282
vfs: _vfs = {},
73-
executingFilePath = FakeServerHost.defaultExecutingFilePath,
83+
executingFilePath = dos
84+
? FakeServerHost.dosExecutingFilePath
85+
: FakeServerHost.defaultExecutingFilePath,
7486
newLine = "\n",
7587
safeList = false,
7688
lib = false
7789
} = options;
7890

79-
const { currentDirectory = FakeServerHost.defaultCurrentDirectory, useCaseSensitiveFileNames = false } = _vfs;
91+
const {
92+
currentDirectory = dos
93+
? FakeServerHost.dosDefaultCurrentDirectory
94+
: FakeServerHost.defaultCurrentDirectory,
95+
useCaseSensitiveFileNames = false
96+
} = _vfs;
8097

81-
this.vfs = _vfs instanceof vfs.VirtualFileSystem ? _vfs :
82-
new vfs.VirtualFileSystem(currentDirectory, useCaseSensitiveFileNames);
98+
this.vfs = _vfs instanceof vfs.VirtualFileSystem
99+
? _vfs
100+
: new vfs.VirtualFileSystem(currentDirectory, useCaseSensitiveFileNames);
83101

84102
this.useCaseSensitiveFileNames = this.vfs.useCaseSensitiveFileNames;
85103
this.newLine = newLine;
86104
this._executingFilePath = executingFilePath;
87105
this._getCanonicalFileName = ts.createGetCanonicalFileName(this.useCaseSensitiveFileNames);
88106

89107
if (safeList) {
90-
this.vfs.addFile(FakeServerHost.safeListPath, FakeServerHost.safeListContent);
108+
this.vfs.addFile(
109+
dos ? FakeServerHost.dosSafeListPath : FakeServerHost.safeListPath,
110+
FakeServerHost.safeListContent);
91111
}
92112

93113
if (lib) {
94-
this.vfs.addFile(FakeServerHost.libPath, FakeServerHost.libContent);
114+
this.vfs.addFile(
115+
dos ? FakeServerHost.dosLibPath : FakeServerHost.libPath,
116+
FakeServerHost.libContent);
95117
}
96118
}
97119

@@ -143,6 +165,12 @@ namespace fakes {
143165
}
144166
// #endregion DirectoryStructureHost members
145167

168+
// #region ModuleResolutionHost members
169+
public trace(message: string) {
170+
this._trace.push(message);
171+
}
172+
// #endregion
173+
146174
// #region System members
147175
public readonly args: string[] = [];
148176

@@ -226,6 +254,14 @@ namespace fakes {
226254
this._output.length = 0;
227255
}
228256

257+
public getTrace(): ReadonlyArray<string> {
258+
return this._trace;
259+
}
260+
261+
public clearTrace() {
262+
this._trace.length = 0;
263+
}
264+
229265
public checkTimeoutQueueLength(expected: number) {
230266
const callbacksCount = this.timers.getPending({ kind: "timeout", ms: this.timers.remainingTime }).length;
231267
assert.equal(callbacksCount, expected, `expected ${expected} timeout callbacks queued but found ${callbacksCount}.`);
@@ -236,6 +272,17 @@ namespace fakes {
236272
this.runQueuedTimeoutCallbacks();
237273
}
238274

275+
public runQueuedImmediateCallbacks() {
276+
try {
277+
this.timers.executeImmediates();
278+
}
279+
catch (e) {
280+
if (e !== FakeServerHost.processExitSentinel) {
281+
throw e;
282+
}
283+
}
284+
}
285+
239286
public runQueuedTimeoutCallbacks() {
240287
try {
241288
this.timers.advanceToEnd();

src/harness/unittests/compileOnSave.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
namespace ts.projectSystem {
88
const nullCancellationToken = server.nullCancellationToken;
9+
import libFile = ts.TestFSWithWatch.libFile;
10+
import FileOrFolder = ts.TestFSWithWatch.FileOrFolder;
911

1012
function createTestTypingsInstaller(host: server.ServerHost) {
1113
return new TestTypingsInstaller("/a/data/", /*throttleLimit*/5, host);

src/harness/unittests/extractTestHelpers.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/// <reference path="..\harness.ts" />
22
/// <reference path="tsserverProjectSystem.ts" />
3+
/// <reference path="../fakes.ts" />
34

45
namespace ts {
56
export interface Range {
@@ -98,6 +99,19 @@ namespace ts {
9899
getCurrentDirectory: notImplemented,
99100
};
100101

102+
function createServerHost(files: ts.TestFSWithWatch.FileOrFolder[], options?: Partial<fakes.FakeServerHostOptions>) {
103+
const host = new fakes.FakeServerHost(options);
104+
for (const file of files) {
105+
if (isString(file.content)) {
106+
host.vfs.writeFile(file.path, file.content);
107+
}
108+
else {
109+
host.vfs.addDirectory(file.path);
110+
}
111+
}
112+
return host;
113+
}
114+
101115
export function testExtractSymbol(caption: string, text: string, baselineFolder: string, description: DiagnosticMessage, includeLib?: boolean) {
102116
const t = extractTest(text);
103117
const selectionRange = t.ranges.get("selection");
@@ -154,7 +168,7 @@ namespace ts {
154168
}
155169

156170
function makeProgram(f: {path: string, content: string }, includeLib?: boolean) {
157-
const host = projectSystem.createServerHost(includeLib ? [f, projectSystem.libFile] : [f]); // libFile is expensive to parse repeatedly - only test when required
171+
const host = createServerHost(includeLib ? [f, ts.TestFSWithWatch.libFile] : [f]); // libFile is expensive to parse repeatedly - only test when required
158172
const projectService = projectSystem.createProjectService(host);
159173
projectService.openClientFile(f.path);
160174
const program = projectService.inferredProjects[0].getLanguageService().getProgram();
@@ -178,7 +192,7 @@ namespace ts {
178192
path: "/a.ts",
179193
content: t.source
180194
};
181-
const host = projectSystem.createServerHost([f, projectSystem.libFile]);
195+
const host = ts.TestFSWithWatch.createServerHost([f, ts.TestFSWithWatch.libFile]);
182196
const projectService = projectSystem.createProjectService(host);
183197
projectService.openClientFile(f.path);
184198
const program = projectService.inferredProjects[0].getLanguageService().getProgram();

src/harness/unittests/telemetry.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
/// <reference path="./tsserverProjectSystem.ts" />
33

44
namespace ts.projectSystem {
5+
import FileOrFolder = ts.TestFSWithWatch.FileOrFolder;
6+
57
describe("project telemetry", () => {
68
it("does nothing for inferred project", () => {
79
const file = makeFile("/a.js");
@@ -235,7 +237,7 @@ namespace ts.projectSystem {
235237
});
236238
});
237239

238-
function makeFile(path: string, content: {} = ""): projectSystem.FileOrFolder {
240+
function makeFile(path: string, content: {} = ""): FileOrFolder {
239241
return { path, content: isString(content) ? "" : JSON.stringify(content) };
240242
}
241243
}

0 commit comments

Comments
 (0)