From b7ca4f6f503ecb77a9e354a75c4866f1481fe5c6 Mon Sep 17 00:00:00 2001 From: fjk <42232973+fu1996@users.noreply.github.com> Date: Sat, 18 Mar 2023 16:49:57 +0800 Subject: [PATCH 1/2] feat: add withFileTypes in FileSystem --- lib/Resolver.js | 10 +++++- types.d.ts | 85 ++++++++++++++++++++++++++++++++++++------------- 2 files changed, 72 insertions(+), 23 deletions(-) diff --git a/lib/Resolver.js b/lib/Resolver.js index 875b435f..320fce50 100644 --- a/lib/Resolver.js +++ b/lib/Resolver.js @@ -45,10 +45,18 @@ const { * @param {T=} result */ +/** @typedef {function((NodeJS.ErrnoException | null)=, (string | Buffer)[] | IDirent[]=): void} DirentArrayCallback */ + +/** + * @typedef {Object} ReaddirOptions + * @property {BufferEncoding | null} [encoding] + * @property {boolean} [withFileTypes=false] + */ + /** * @typedef {Object} FileSystem * @property {(function(string, FileSystemCallback): void) & function(string, object, FileSystemCallback): void} readFile - * @property {(function(string, FileSystemCallback<(Buffer | string)[] | FileSystemDirent[]>): void) & function(string, object, FileSystemCallback<(Buffer | string)[] | FileSystemDirent[]>): void} readdir + * @property {function(string, (ReaddirOptions | BufferEncoding | null | DirentArrayCallback)=, DirentArrayCallback=): void} readdir * @property {((function(string, FileSystemCallback): void) & function(string, object, FileSystemCallback): void)=} readJson * @property {(function(string, FileSystemCallback): void) & function(string, object, FileSystemCallback): void} readlink * @property {(function(string, FileSystemCallback): void) & function(string, object, FileSystemCallback): void=} lstat diff --git a/types.d.ts b/types.d.ts index 01436533..7d29ee73 100644 --- a/types.d.ts +++ b/types.d.ts @@ -45,17 +45,30 @@ declare class CachedInputFileSystem { ): void; }; statSync: (arg0: string, arg1?: object) => FileSystemStats; - readdir: { - ( - arg0: string, - arg1: FileSystemCallback<(string | Buffer)[] | FileSystemDirent[]> - ): void; - ( - arg0: string, - arg1: object, - arg2: FileSystemCallback<(string | Buffer)[] | FileSystemDirent[]> - ): void; - }; + readdir: ( + arg0: string, + arg1?: + | null + | (( + arg0?: null | NodeJS.ErrnoException, + arg1?: (string | Buffer)[] | any[] + ) => void) + | ReaddirOptions + | "ascii" + | "utf8" + | "utf-8" + | "utf16le" + | "ucs2" + | "ucs-2" + | "base64" + | "latin1" + | "binary" + | "hex", + arg2?: ( + arg0?: null | NodeJS.ErrnoException, + arg1?: (string | Buffer)[] | any[] + ) => void + ) => void; readdirSync: ( arg0: string, arg1?: object @@ -107,17 +120,30 @@ declare interface FileSystem { arg2: FileSystemCallback ): void; }; - readdir: { - ( - arg0: string, - arg1: FileSystemCallback<(string | Buffer)[] | FileSystemDirent[]> - ): void; - ( - arg0: string, - arg1: object, - arg2: FileSystemCallback<(string | Buffer)[] | FileSystemDirent[]> - ): void; - }; + readdir: ( + arg0: string, + arg1?: + | null + | (( + arg0?: null | NodeJS.ErrnoException, + arg1?: (string | Buffer)[] | any[] + ) => void) + | ReaddirOptions + | "ascii" + | "utf8" + | "utf-8" + | "utf16le" + | "ucs2" + | "ucs-2" + | "base64" + | "latin1" + | "binary" + | "hex", + arg2?: ( + arg0?: null | NodeJS.ErrnoException, + arg1?: (string | Buffer)[] | any[] + ) => void + ) => void; readJson?: { (arg0: string, arg1: FileSystemCallback): void; (arg0: string, arg1: object, arg2: FileSystemCallback): void; @@ -185,6 +211,21 @@ declare interface PossibleFileSystemError { path?: string; syscall?: string; } +declare interface ReaddirOptions { + encoding?: + | null + | "ascii" + | "utf8" + | "utf-8" + | "utf16le" + | "ucs2" + | "ucs-2" + | "base64" + | "latin1" + | "binary" + | "hex"; + withFileTypes?: boolean; +} /** * Resolve context From 114d565cbf16998cde71268ccf05ac70ed2a83cd Mon Sep 17 00:00:00 2001 From: fjk <42232973+fu1996@users.noreply.github.com> Date: Sun, 19 Mar 2023 15:44:47 +0800 Subject: [PATCH 2/2] feat: add type (undefined | 'buffer') to readdir --- lib/Resolver.js | 6 +++--- types.d.ts | 9 ++++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/Resolver.js b/lib/Resolver.js index 320fce50..93951440 100644 --- a/lib/Resolver.js +++ b/lib/Resolver.js @@ -49,14 +49,14 @@ const { /** * @typedef {Object} ReaddirOptions - * @property {BufferEncoding | null} [encoding] - * @property {boolean} [withFileTypes=false] + * @property {BufferEncoding | null | 'buffer'} [encoding] + * @property {boolean | undefined} [withFileTypes=false] */ /** * @typedef {Object} FileSystem * @property {(function(string, FileSystemCallback): void) & function(string, object, FileSystemCallback): void} readFile - * @property {function(string, (ReaddirOptions | BufferEncoding | null | DirentArrayCallback)=, DirentArrayCallback=): void} readdir + * @property {function(string, (ReaddirOptions | BufferEncoding | null | undefined | 'buffer' | DirentArrayCallback)=, DirentArrayCallback=): void} readdir * @property {((function(string, FileSystemCallback): void) & function(string, object, FileSystemCallback): void)=} readJson * @property {(function(string, FileSystemCallback): void) & function(string, object, FileSystemCallback): void} readlink * @property {(function(string, FileSystemCallback): void) & function(string, object, FileSystemCallback): void=} lstat diff --git a/types.d.ts b/types.d.ts index 7d29ee73..2c0b668e 100644 --- a/types.d.ts +++ b/types.d.ts @@ -63,7 +63,8 @@ declare class CachedInputFileSystem { | "base64" | "latin1" | "binary" - | "hex", + | "hex" + | "buffer", arg2?: ( arg0?: null | NodeJS.ErrnoException, arg1?: (string | Buffer)[] | any[] @@ -138,7 +139,8 @@ declare interface FileSystem { | "base64" | "latin1" | "binary" - | "hex", + | "hex" + | "buffer", arg2?: ( arg0?: null | NodeJS.ErrnoException, arg1?: (string | Buffer)[] | any[] @@ -223,7 +225,8 @@ declare interface ReaddirOptions { | "base64" | "latin1" | "binary" - | "hex"; + | "hex" + | "buffer"; withFileTypes?: boolean; }