Skip to content

Make new harness fake host more performant in large complications #23951

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2223,6 +2223,7 @@ namespace ts {
const reduced = [components[0]];
for (let i = 1; i < components.length; i++) {
const component = components[i];
if (!component) continue;
if (component === ".") continue;
if (component === "..") {
if (reduced.length > 1) {
Expand Down
7 changes: 6 additions & 1 deletion src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,12 @@ namespace ts {
export function readJson(path: string, host: { readFile(fileName: string): string | undefined }): object {
try {
const jsonText = host.readFile(path);
return jsonText ? JSON.parse(jsonText) : {};
if (!jsonText) return {};
const result = parseConfigFileTextToJson(path, jsonText);
if (result.error) {
return {};
}
return result.config;
}
catch (e) {
// gracefully handle if readFile fails or returns not JSON
Expand Down
14 changes: 6 additions & 8 deletions src/harness/fakes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ namespace fakes {
public readFile(path: string) {
try {
const content = this.vfs.readFileSync(path, "utf8");
return content === undefined ? undefined :
vpath.extname(path) === ".json" ? utils.removeComments(utils.removeByteOrderMark(content), utils.CommentRemoval.leadingAndTrailing) :
utils.removeByteOrderMark(content);
return content === undefined ? undefined : utils.removeByteOrderMark(content);
}
catch {
return undefined;
Expand Down Expand Up @@ -203,6 +201,7 @@ namespace fakes {
public readonly sys: System;
public readonly defaultLibLocation: string;
public readonly outputs: documents.TextDocument[] = [];
private readonly _outputsMap: collections.SortedMap<string, number>;
public readonly traces: string[] = [];
public readonly shouldAssertInvariants = !Harness.lightMode;

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

public get vfs() {
Expand Down Expand Up @@ -267,13 +267,11 @@ namespace fakes {
const document = new documents.TextDocument(fileName, content);
document.meta.set("fileName", fileName);
this.vfs.filemeta(fileName).set("document", document);
const index = this.outputs.findIndex(output => this.vfs.stringComparer(document.file, output.file) === 0);
if (index < 0) {
if (!this._outputsMap.has(document.file)) {
this._outputsMap.set(document.file, this.outputs.length);
this.outputs.push(document);
}
else {
this.outputs[index] = document;
}
this.outputs[this._outputsMap.get(document.file)] = document;
}

public trace(s: string): void {
Expand Down
25 changes: 0 additions & 25 deletions src/harness/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,6 @@
* Common utilities
*/
namespace utils {
const leadingCommentRegExp = /^(\s*\/\*[^]*?\*\/\s*|\s*\/\/[^\r\n\u2028\u2029]*[\r\n\u2028\u2029]*)+/;
const trailingCommentRegExp = /(\s*\/\*[^]*?\*\/\s*|\s*\/\/[^\r\n\u2028\u2029]*[\r\n\u2028\u2029]*)+$/;
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;
const allCommentRegExp = /(['"])(?:(?!\1).|\\[^])*\1|(\/\*[^]*?\*\/|\/\/[^\r\n\u2028\u2029]*[\r\n\u2028\u2029]*)/g;

export const enum CommentRemoval {
leading,
trailing,
leadingAndTrailing,
all
}

export function removeComments(text: string, removal: CommentRemoval) {
switch (removal) {
case CommentRemoval.leading:
return text.replace(leadingCommentRegExp, "");
case CommentRemoval.trailing:
return text.replace(trailingCommentRegExp, "");
case CommentRemoval.leadingAndTrailing:
return text.replace(leadingAndTrailingCommentRegExp, "");
case CommentRemoval.all:
return text.replace(allCommentRegExp, (match, quote) => quote ? match : "");
}
}

const testPathPrefixRegExp = /(?:(file:\/{3})|\/)\.(ts|lib|src)\//g;
export function removeTestPathPrefixes(text: string, retainTrailingDirectorySeparator?: boolean) {
return text !== undefined ? text.replace(testPathPrefixRegExp, (_, scheme) => scheme || (retainTrailingDirectorySeparator ? "/" : "")) : undefined;
Expand Down