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

feat: add withFileTypes in FileSystem #374

Merged
merged 2 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 9 additions & 1 deletion lib/Resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<Buffer | string>): void) & function(string, object, FileSystemCallback<Buffer | string>): 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<object>): void) & function(string, object, FileSystemCallback<object>): void)=} readJson
* @property {(function(string, FileSystemCallback<Buffer | string>): void) & function(string, object, FileSystemCallback<Buffer | string>): void} readlink
* @property {(function(string, FileSystemCallback<FileSystemStats>): void) & function(string, object, FileSystemCallback<Buffer | string>): void=} lstat
Expand Down
85 changes: 63 additions & 22 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -107,17 +120,30 @@ declare interface FileSystem {
arg2: FileSystemCallback<string | Buffer>
): 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined possible too

| ((
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<object>): void;
(arg0: string, arg1: object, arg2: FileSystemCallback<object>): void;
Expand Down Expand Up @@ -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";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's create a new type BufferEncoding and move them there and reuse here and above

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also buffer possible too

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for infromation, here is node types for such method:

export function readdir(
        path: PathLike,
        options:
            | {
                  encoding: BufferEncoding | null;
                  withFileTypes?: false | undefined;
              }
            | BufferEncoding
            | undefined
            | null,
        callback: (err: NodeJS.ErrnoException | null, files: string[]) => void
    ): void;
    /**
     * Asynchronous readdir(3) - read a directory.
     * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
     * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
     */
    export function readdir(
        path: PathLike,
        options:
            | {
                  encoding: 'buffer';
                  withFileTypes?: false | undefined;
              }
            | 'buffer',
        callback: (err: NodeJS.ErrnoException | null, files: Buffer[]) => void
    ): void;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's create a new type BufferEncoding and move them there and reuse here and above

"This types.d.ts is automatically generated by running the 'yarn special lint fix' command. I think what I can do is modify the jsDoc syntax in 'Resolver. js' to generate it, but I cannot control the addition of' ascii '|' utf8 '|' utf-8 '|' utf16le '|' ucs2 '|' ucs-2 '|' base64 '|' latin1 '|' binary '|' hex ';"; "Force the use of the BufferEncoding type, even if I manually modify it after running the 'yarn special lint fix' command, but I don't think this is a good method. Do you have any good ideas for controlling the use of the BufferEncoding type instead of its corresponding enumeration value?"?

The following figure shows that after I manually changed to BufferEncoding and executed the 'yarn special lint fix' command, my manual modification was invalid

image

withFileTypes?: boolean;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible to pass undefined directly here, so boolean | undefined

}

/**
* Resolve context
Expand Down