Skip to content
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

perf: use path.exists functions that don't throw internally when not exists #1478

Merged
merged 2 commits into from
Dec 2, 2023
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
2 changes: 1 addition & 1 deletion deno/basic_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertEquals } from "https://deno.land/std@0.181.0/testing/asserts.ts";
import { assertEquals } from "https://deno.land/std@0.208.0/testing/asserts.ts";
import { Project } from "./mod.ts";

// todo: Eventually all tests run for the node package should also be run for Deno
Expand Down
2 changes: 1 addition & 1 deletion deno/bootstrap/basic_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertEquals } from "https://deno.land/std@0.181.0/testing/asserts.ts";
import { assertEquals } from "https://deno.land/std@0.208.0/testing/asserts.ts";
import { createProjectSync } from "./mod.ts";

// todo: Eventually all tests run for the node package should also be run for Deno
Expand Down
28 changes: 21 additions & 7 deletions deno/common/DenoRuntime.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ensureDir, ensureDirSync } from "https://deno.land/std@0.181.0/fs/ensure_dir.ts";
import { expandGlob, expandGlobSync } from "https://deno.land/std@0.181.0/fs/expand_glob.ts";
import * as stdPath from "https://deno.land/std@0.181.0/path/mod.ts";
import { ensureDir, ensureDirSync } from "https://deno.land/std@0.208.0/fs/ensure_dir.ts";
import { expandGlob, expandGlobSync } from "https://deno.land/std@0.208.0/fs/expand_glob.ts";
import * as stdPath from "https://deno.land/std@0.208.0/path/mod.ts";

// deno-lint-ignore no-explicit-any

Expand Down Expand Up @@ -93,13 +93,27 @@ class DenoRuntimeFileSystem {
}

async stat(filePath: string) {
const stat = await Deno.stat(filePath);
return this.#toStat(stat);
try {
const stat = await Deno.stat(filePath);
return this.#toStat(stat);
} catch (err) {
if (err instanceof Deno.errors.NotFound)
return undefined;
else
throw err;
}
}

statSync(path: string) {
const stat = Deno.statSync(path);
return this.#toStat(stat);
try {
const stat = Deno.statSync(path);
return this.#toStat(stat);
} catch (err) {
if (err instanceof Deno.errors.NotFound)
return undefined;
else
throw err;
}
}

// deno-lint-ignore no-explicit-any
Expand Down
4 changes: 2 additions & 2 deletions deno/common/ts_morph_common.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -926,9 +926,9 @@ export interface RuntimeFileSystem {
/** Synchronously copies a file or directory. */
copySync(srcPath: string, destPath: string): void;
/** Asynchronously gets the path's stat information. */
stat(path: string): Promise<RuntimeFileInfo>;
stat(path: string): Promise<RuntimeFileInfo | undefined>;
/** Synchronously gets the path's stat information. */
statSync(path: string): RuntimeFileInfo;
statSync(path: string): RuntimeFileInfo | undefined;
/** See https://nodejs.org/api/fs.html#fs_fs_realpathsync_path_options */
realpathSync(path: string): string;
/** Gets the current directory of the environment. */
Expand Down
14 changes: 8 additions & 6 deletions deno/common/ts_morph_common.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions deno/ts_morph.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5670,6 +5670,13 @@ export declare class ShorthandPropertyAssignment extends ShorthandPropertyAssign
set(structure: Partial<ShorthandPropertyAssignmentStructure>): this;
/** Gets the structure equivalent to this node. */
getStructure(): ShorthandPropertyAssignmentStructure;
/**
* Gets the shorthand assignment value symbol of this node if it exists. Convenience API
* for TypeChecker#getShorthandAssignmentValueSymbol(node)
*/
getValueSymbol(): Symbol | undefined;
/** Gets the value symbol or throws if it doesn't exist. */
getValueSymbolOrThrow(message?: string | (() => string)): Symbol;
/** @inheritdoc **/
getParent(): NodeParentType<ts.ShorthandPropertyAssignment>;
/** @inheritdoc **/
Expand Down Expand Up @@ -9847,6 +9854,8 @@ export declare class TypeChecker {
* @param typeReference - Type reference.
*/
getTypeArguments(typeReference: Type): Type<ts.Type>[];
/** Gets the shorthand assignment value symbol of the provided node. */
getShorthandAssignmentValueSymbol(node: Node): Symbol | undefined;
}

export declare class Type<TType extends ts.Type = ts.Type> {
Expand Down
10 changes: 10 additions & 0 deletions deno/ts_morph.js
Original file line number Diff line number Diff line change
Expand Up @@ -11195,6 +11195,12 @@ class ShorthandPropertyAssignment extends ShorthandPropertyAssignmentBase {
delete structure.hasQuestionToken;
return structure;
}
getValueSymbol() {
return this._context.typeChecker.getShorthandAssignmentValueSymbol(this);
}
getValueSymbolOrThrow(message) {
return errors.throwIfNullOrUndefined(this.getValueSymbol(), message ?? "Expected to find a value symbol.", this);
}
}

const SpreadAssignmentBase = ExpressionedNode(ObjectLiteralElement);
Expand Down Expand Up @@ -17872,6 +17878,10 @@ class TypeChecker {
formatFlags |= TypeFormatFlags.InTypeAlias;
return formatFlags;
}
getShorthandAssignmentValueSymbol(node) {
const symbol = this.compilerObject.getShorthandAssignmentValueSymbol(node.compilerNode);
return symbol ? this.#context.compilerFactory.getSymbol(symbol) : undefined;
}
}

class Program {
Expand Down
4 changes: 2 additions & 2 deletions packages/common/lib/ts-morph-common.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -925,9 +925,9 @@ export interface RuntimeFileSystem {
/** Synchronously copies a file or directory. */
copySync(srcPath: string, destPath: string): void;
/** Asynchronously gets the path's stat information. */
stat(path: string): Promise<RuntimeFileInfo>;
stat(path: string): Promise<RuntimeFileInfo | undefined>;
/** Synchronously gets the path's stat information. */
statSync(path: string): RuntimeFileInfo;
statSync(path: string): RuntimeFileInfo | undefined;
/** See https://nodejs.org/api/fs.html#fs_fs_realpathsync_path_options */
realpathSync(path: string): string;
/** Gets the current directory of the environment. */
Expand Down
14 changes: 8 additions & 6 deletions packages/common/src/fileSystem/RealFileSystemHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ export class RealFileSystemHost implements FileSystemHost {
if (entry.isSymlink) {
try {
const info = fs.statSync(entry.name);
entry.isDirectory = info.isDirectory();
entry.isFile = info.isFile();
if (info != null) {
entry.isDirectory = info.isDirectory();
entry.isFile = info.isFile();
}
} catch {
// ignore
}
Expand Down Expand Up @@ -108,7 +110,7 @@ export class RealFileSystemHost implements FileSystemHost {
/** @inheritdoc */
async fileExists(filePath: string) {
try {
return (await fs.stat(filePath)).isFile();
return (await fs.stat(filePath))?.isFile() ?? false;
} catch {
return false;
}
Expand All @@ -117,7 +119,7 @@ export class RealFileSystemHost implements FileSystemHost {
/** @inheritdoc */
fileExistsSync(filePath: string) {
try {
return fs.statSync(filePath).isFile();
return fs.statSync(filePath)?.isFile() ?? false;
} catch {
return false;
}
Expand All @@ -126,7 +128,7 @@ export class RealFileSystemHost implements FileSystemHost {
/** @inheritdoc */
async directoryExists(dirPath: string) {
try {
return (await fs.stat(dirPath)).isDirectory();
return (await fs.stat(dirPath))?.isDirectory() ?? false;
} catch {
return false;
}
Expand All @@ -135,7 +137,7 @@ export class RealFileSystemHost implements FileSystemHost {
/** @inheritdoc */
directoryExistsSync(dirPath: string) {
try {
return fs.statSync(dirPath).isDirectory();
return fs.statSync(dirPath)?.isDirectory() ?? false;
} catch {
return false;
}
Expand Down
28 changes: 21 additions & 7 deletions packages/common/src/runtimes/DenoRuntime.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// @ts-ignore
import { ensureDir, ensureDirSync } from "https://deno.land/std@0.181.0/fs/ensure_dir.ts";
import { ensureDir, ensureDirSync } from "https://deno.land/std@0.208.0/fs/ensure_dir.ts";
// @ts-ignore
import { expandGlob, expandGlobSync } from "https://deno.land/std@0.181.0/fs/expand_glob.ts";
import { expandGlob, expandGlobSync } from "https://deno.land/std@0.208.0/fs/expand_glob.ts";
// @ts-ignore
import * as stdPath from "https://deno.land/std@0.181.0/path/mod.ts";
import * as stdPath from "https://deno.land/std@0.208.0/path/mod.ts";

// deno-lint-ignore no-explicit-any
const Deno = (globalThis as any).Deno;
Expand Down Expand Up @@ -97,13 +97,27 @@ class DenoRuntimeFileSystem {
}

async stat(filePath: string) {
const stat = await Deno.stat(filePath);
return this.#toStat(stat);
try {
const stat = await Deno.stat(filePath);
return this.#toStat(stat);
} catch (err) {
if (err instanceof Deno.errors.NotFound)
return undefined;
else
throw err;
}
}

statSync(path: string) {
const stat = Deno.statSync(path);
return this.#toStat(stat);
try {
const stat = Deno.statSync(path);
return this.#toStat(stat);
} catch (err) {
if (err instanceof Deno.errors.NotFound)
return undefined;
else
throw err;
}
}

// deno-lint-ignore no-explicit-any
Expand Down
14 changes: 9 additions & 5 deletions packages/common/src/runtimes/NodeRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,22 @@ class NodeRuntimeFileSystem implements RuntimeFileSystem {
}

stat(path: string) {
return new Promise<RuntimeFileInfo>(resolve => {
return new Promise<RuntimeFileInfo | undefined>((resolve, reject) => {
fs.stat(path, (err, stat) => {
if (err)
throw err;
else
if (err) {
if (err.code === "ENOENT" || err.code === "ENOTDIR")
resolve(undefined);
else
reject(err);
} else {
resolve(stat);
}
});
});
}

statSync(path: string) {
return fs.statSync(path);
return fs.statSync(path, { throwIfNoEntry: false });
}

realpathSync(path: string) {
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/runtimes/Runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ export interface RuntimeFileSystem {
/** Synchronously copies a file or directory. */
copySync(srcPath: string, destPath: string): void;
/** Asynchronously gets the path's stat information. */
stat(path: string): Promise<RuntimeFileInfo>;
stat(path: string): Promise<RuntimeFileInfo | undefined>;
/** Synchronously gets the path's stat information. */
statSync(path: string): RuntimeFileInfo;
statSync(path: string): RuntimeFileInfo | undefined;
/** See https://nodejs.org/api/fs.html#fs_fs_realpathsync_path_options */
realpathSync(path: string): string;
/** Gets the current directory of the environment. */
Expand Down
9 changes: 9 additions & 0 deletions packages/ts-morph/lib/ts-morph.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5670,6 +5670,13 @@ export declare class ShorthandPropertyAssignment extends ShorthandPropertyAssign
set(structure: Partial<ShorthandPropertyAssignmentStructure>): this;
/** Gets the structure equivalent to this node. */
getStructure(): ShorthandPropertyAssignmentStructure;
/**
* Gets the shorthand assignment value symbol of this node if it exists. Convenience API
* for TypeChecker#getShorthandAssignmentValueSymbol(node)
*/
getValueSymbol(): Symbol | undefined;
/** Gets the value symbol or throws if it doesn't exist. */
getValueSymbolOrThrow(message?: string | (() => string)): Symbol;
/** @inheritdoc **/
getParent(): NodeParentType<ts.ShorthandPropertyAssignment>;
/** @inheritdoc **/
Expand Down Expand Up @@ -9847,6 +9854,8 @@ export declare class TypeChecker {
* @param typeReference - Type reference.
*/
getTypeArguments(typeReference: Type): Type<ts.Type>[];
/** Gets the shorthand assignment value symbol of the provided node. */
getShorthandAssignmentValueSymbol(node: Node): Symbol | undefined;
}

export declare class Type<TType extends ts.Type = ts.Type> {
Expand Down
4 changes: 2 additions & 2 deletions packages/ts-morph/src/compiler/tools/TypeChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ export class TypeChecker {
* Gets the shorthand assignment value symbol of the provided node.
*/
getShorthandAssignmentValueSymbol(node: Node) {
const symbol = this.compilerObject.getShorthandAssignmentValueSymbol(node.compilerNode)
return symbol ? this._context.compilerFactory.getSymbol(symbol) : undefined
const symbol = this.compilerObject.getShorthandAssignmentValueSymbol(node.compilerNode);
return symbol ? this.#context.compilerFactory.getSymbol(symbol) : undefined;
}
}
Loading