Skip to content

Commit 556c316

Browse files
authored
Make new harness fake host more performant in large complications (#23951)
* Make new harness fake host more performant in large complications * Use sortedmap
1 parent 12790e8 commit 556c316

File tree

4 files changed

+13
-34
lines changed

4 files changed

+13
-34
lines changed

src/compiler/core.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2227,6 +2227,7 @@ namespace ts {
22272227
const reduced = [components[0]];
22282228
for (let i = 1; i < components.length; i++) {
22292229
const component = components[i];
2230+
if (!component) continue;
22302231
if (component === ".") continue;
22312232
if (component === "..") {
22322233
if (reduced.length > 1) {

src/compiler/moduleNameResolver.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,12 @@ namespace ts {
119119
export function readJson(path: string, host: { readFile(fileName: string): string | undefined }): object {
120120
try {
121121
const jsonText = host.readFile(path);
122-
return jsonText ? JSON.parse(jsonText) : {};
122+
if (!jsonText) return {};
123+
const result = parseConfigFileTextToJson(path, jsonText);
124+
if (result.error) {
125+
return {};
126+
}
127+
return result.config;
123128
}
124129
catch (e) {
125130
// gracefully handle if readFile fails or returns not JSON

src/harness/fakes.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ namespace fakes {
3939
public readFile(path: string) {
4040
try {
4141
const content = this.vfs.readFileSync(path, "utf8");
42-
return content === undefined ? undefined :
43-
vpath.extname(path) === ".json" ? utils.removeComments(utils.removeByteOrderMark(content), utils.CommentRemoval.leadingAndTrailing) :
44-
utils.removeByteOrderMark(content);
42+
return content === undefined ? undefined : utils.removeByteOrderMark(content);
4543
}
4644
catch {
4745
return undefined;
@@ -203,6 +201,7 @@ namespace fakes {
203201
public readonly sys: System;
204202
public readonly defaultLibLocation: string;
205203
public readonly outputs: documents.TextDocument[] = [];
204+
private readonly _outputsMap: collections.SortedMap<string, number>;
206205
public readonly traces: string[] = [];
207206
public readonly shouldAssertInvariants = !Harness.lightMode;
208207

@@ -218,6 +217,7 @@ namespace fakes {
218217
this._newLine = ts.getNewLineCharacter(options, () => this.sys.newLine);
219218
this._sourceFiles = new collections.SortedMap<string, ts.SourceFile>({ comparer: sys.vfs.stringComparer, sort: "insertion" });
220219
this._setParentNodes = setParentNodes;
220+
this._outputsMap = new collections.SortedMap(this.vfs.stringComparer);
221221
}
222222

223223
public get vfs() {
@@ -271,13 +271,11 @@ namespace fakes {
271271
const document = new documents.TextDocument(fileName, content);
272272
document.meta.set("fileName", fileName);
273273
this.vfs.filemeta(fileName).set("document", document);
274-
const index = this.outputs.findIndex(output => this.vfs.stringComparer(document.file, output.file) === 0);
275-
if (index < 0) {
274+
if (!this._outputsMap.has(document.file)) {
275+
this._outputsMap.set(document.file, this.outputs.length);
276276
this.outputs.push(document);
277277
}
278-
else {
279-
this.outputs[index] = document;
280-
}
278+
this.outputs[this._outputsMap.get(document.file)] = document;
281279
}
282280

283281
public trace(s: string): void {

src/harness/utils.ts

-25
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,6 @@
22
* Common utilities
33
*/
44
namespace utils {
5-
const leadingCommentRegExp = /^(\s*\/\*[^]*?\*\/\s*|\s*\/\/[^\r\n\u2028\u2029]*[\r\n\u2028\u2029]*)+/;
6-
const trailingCommentRegExp = /(\s*\/\*[^]*?\*\/\s*|\s*\/\/[^\r\n\u2028\u2029]*[\r\n\u2028\u2029]*)+$/;
7-
const leadingAndTrailingCommentRegExp = /^(\s*\/\*[^]*?\*\/\s*|\s*\/\/[^\r\n\u2028\u2029]*[\r\n\u2028\u2029]*)+|(\s*\/\*[^]*?\*\/\s*|\s*\/\/[^\r\n\u2028\u2029]*[\r\n\u2028\u2029]*)+$/g;
8-
const allCommentRegExp = /(['"])(?:(?!\1).|\\[^])*\1|(\/\*[^]*?\*\/|\/\/[^\r\n\u2028\u2029]*[\r\n\u2028\u2029]*)/g;
9-
10-
export const enum CommentRemoval {
11-
leading,
12-
trailing,
13-
leadingAndTrailing,
14-
all
15-
}
16-
17-
export function removeComments(text: string, removal: CommentRemoval) {
18-
switch (removal) {
19-
case CommentRemoval.leading:
20-
return text.replace(leadingCommentRegExp, "");
21-
case CommentRemoval.trailing:
22-
return text.replace(trailingCommentRegExp, "");
23-
case CommentRemoval.leadingAndTrailing:
24-
return text.replace(leadingAndTrailingCommentRegExp, "");
25-
case CommentRemoval.all:
26-
return text.replace(allCommentRegExp, (match, quote) => quote ? match : "");
27-
}
28-
}
29-
305
const testPathPrefixRegExp = /(?:(file:\/{3})|\/)\.(ts|lib|src)\//g;
316
export function removeTestPathPrefixes(text: string, retainTrailingDirectorySeparator?: boolean) {
327
return text !== undefined ? text.replace(testPathPrefixRegExp, (_, scheme) => scheme || (retainTrailingDirectorySeparator ? "/" : "")) : undefined;

0 commit comments

Comments
 (0)