Skip to content

Commit

Permalink
fix: types
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Mar 6, 2024
1 parent 1780817 commit bf89031
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 50 deletions.
22 changes: 11 additions & 11 deletions lib/Resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ const {
* @property {BufferEncoding | null | undefined} [encoding]
*/

/** @typedef {function(NodeJS.ErrnoException | null, string | undefined): void} StringCallback */
/** @typedef {function(NodeJS.ErrnoException | null, Buffer | undefined): void} BufferCallback */
/** @typedef {function(NodeJS.ErrnoException | null, string | Buffer | undefined): void} StringOrBufferCallback */
/** @typedef {function(NodeJS.ErrnoException | null, IStats | undefined): void} StatsCallback */
/** @typedef {function(NodeJS.ErrnoException | null, IBigIntStats | undefined): void} BigIntStatsCallback */
/** @typedef {function(NodeJS.ErrnoException | null, IStats | IBigIntStats | undefined): void} StatsOrBigIntStatsCallback */
/** @typedef {function(NodeJS.ErrnoException | Error | null, JsonObject | undefined): void} ReadJsonCallback */
/** @typedef {function(NodeJS.ErrnoException | null, string[] | undefined): void} ReaddirStringCallback */
/** @typedef {function(NodeJS.ErrnoException | null, Buffer[] | undefined): void} ReaddirBufferCallback */
/** @typedef {function(NodeJS.ErrnoException | null, string[] | Buffer[] | undefined): void} ReaddirStringOrBufferCallback */
/** @typedef {function(NodeJS.ErrnoException | null, Dirent[] | undefined): void} ReaddirDirentCallback */
/** @typedef {function(NodeJS.ErrnoException | null, string=): void} StringCallback */
/** @typedef {function(NodeJS.ErrnoException | null, Buffer=): void} BufferCallback */
/** @typedef {function(NodeJS.ErrnoException | null, (string | Buffer)=): void} StringOrBufferCallback */
/** @typedef {function(NodeJS.ErrnoException | null, IStats=): void} StatsCallback */
/** @typedef {function(NodeJS.ErrnoException | null, IBigIntStats=): void} BigIntStatsCallback */
/** @typedef {function(NodeJS.ErrnoException | null, (IStats | IBigIntStats)=): void} StatsOrBigIntStatsCallback */
/** @typedef {function(NodeJS.ErrnoException | Error | null, JsonObject=): void} ReadJsonCallback */
/** @typedef {function(NodeJS.ErrnoException | null, string[]=): void} ReaddirStringCallback */
/** @typedef {function(NodeJS.ErrnoException | null, Buffer[]=): void} ReaddirBufferCallback */
/** @typedef {function(NodeJS.ErrnoException | null, (string[] | Buffer[])=): void} ReaddirStringOrBufferCallback */
/** @typedef {function(NodeJS.ErrnoException | null, Dirent[]=): void} ReaddirDirentCallback */

/**
* @template T
Expand Down
119 changes: 91 additions & 28 deletions lib/SyncAsyncFileSystemDecorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"use strict";

/** @typedef {import("./Resolver").FileSystem} FileSystem */
/** @typedef {import("./Resolver").Dirent} Dirent */
/** @typedef {import("./Resolver").ReaddirStringCallback} ReaddirStringCallback */
/** @typedef {import("./Resolver").StringCallback} StringCallback */
/** @typedef {import("./Resolver").SyncFileSystem} SyncFileSystem */

/**
Expand All @@ -26,12 +27,16 @@ function SyncAsyncFileSystemDecorator(fs) {
(arg, options, callback) => {
let result;
try {
result = lstatSync.call(fs, arg);
result = /** @type {Function | undefined} */ (callback)
? lstatSync.call(fs, arg, options)
: lstatSync.call(fs, arg);
} catch (e) {
return (callback || options)(e);
return (callback || options)(
/** @type {NodeJS.ErrnoException | null} */ (e)
);
}

(callback || options)(null, result);
(callback || options)(null, /** @type {any} */ (result));
}
);
this.lstatSync =
Expand All @@ -45,11 +50,16 @@ function SyncAsyncFileSystemDecorator(fs) {
(arg, options, callback) => {
let result;
try {
result = callback ? fs.statSync(arg, options) : fs.statSync(arg);
result = /** @type {Function | undefined} */ (callback)
? fs.statSync(arg, options)
: fs.statSync(arg);
} catch (e) {
return (callback || options)(e);
return (callback || options)(
/** @type {NodeJS.ErrnoException | null} */ (e)
);
}
(callback || options)(null, result);

(callback || options)(null, /** @type {any} */ (result));
}
);
this.statSync =
Expand All @@ -62,31 +72,48 @@ function SyncAsyncFileSystemDecorator(fs) {
(arg, options, callback) => {
let result;
try {
result = callback
? fs.readdirSync(arg, options)
result = /** @type {Function | undefined} */ (callback)
? fs.readdirSync(
arg,
/** @type {Exclude<Parameters<FileSystem["readdir"]>[1], ReaddirStringCallback>} */
(options)
)
: fs.readdirSync(arg);
} catch (e) {
return (callback || options)(e);
return (callback || options)(
/** @type {NodeJS.ErrnoException | null} */ (e)
);
}

(callback || options)(null, result);
(callback || options)(null, /** @type {any} */ (result));
}
);
this.readdirSync =
/** @type {SyncFileSystem["readdirSync"]} */
((arg, options) => fs.readdirSync(arg, options));
(
(arg, options) =>
fs.readdirSync(
arg,
/** @type {Parameters<SyncFileSystem["readdirSync"]>[1]} */ (options)
)
);

this.readFile =
/** @type {FileSystem["readFile"]} */
(
(arg, options, callback) => {
let result;
try {
result = fs.readFileSync(arg);
result = /** @type {Function | undefined} */ (callback)
? fs.readFileSync(arg, options)
: fs.readFileSync(arg);
} catch (e) {
return (callback || options)(e);
return (callback || options)(
/** @type {NodeJS.ErrnoException | null} */ (e)
);
}
(callback || options)(null, result);

(callback || options)(null, /** @type {any} */ (result));
}
);
this.readFileSync =
Expand All @@ -99,37 +126,55 @@ function SyncAsyncFileSystemDecorator(fs) {
(arg, options, callback) => {
let result;
try {
result = fs.readlinkSync(arg);
result = /** @type {Function | undefined} */ (callback)
? fs.readlinkSync(
arg,
/** @type {Exclude<Parameters<FileSystem["readlink"]>[1], StringCallback>} */
(options)
)
: fs.readlinkSync(arg);
} catch (e) {
return (callback || options)(e);
return (callback || options)(
/** @type {NodeJS.ErrnoException | null} */ (e)
);
}
(callback || options)(null, result);

(callback || options)(null, /** @type {any} */ (result));
}
);
this.readlinkSync =
/** @type {SyncFileSystem["readlinkSync"]} */
((arg, options) => fs.readlinkSync(arg, options));
(
(arg, options) =>
fs.readlinkSync(
arg,
/** @type {Parameters<SyncFileSystem["readlinkSync"]>[1]} */ (options)
)
);

this.readJson = undefined;
this.readJsonSync = undefined;
const readJsonSync = fs.readJsonSync;
if (readJsonSync) {
this.readJson =
/** @type {NonNullable<FileSystem["readJson"]>} */
/** @type {FileSystem["readJson"]} */
(
(arg, options, callback) => {
(arg, callback) => {
let result;
try {
result = readJsonSync.call(fs, arg);
} catch (e) {
return (callback || options)(e);
return callback(
/** @type {NodeJS.ErrnoException | Error | null} */ (e)
);
}
(callback || options)(null, result);

callback(null, result);
}
);
this.readJsonSync =
/** @type {SyncFileSystem["readJsonSync"]} */
((arg, options) => readJsonSync.call(fs, arg, options));
(arg => readJsonSync.call(fs, arg));
}

this.realpath = undefined;
Expand All @@ -142,16 +187,34 @@ function SyncAsyncFileSystemDecorator(fs) {
(arg, options, callback) => {
let result;
try {
result = realpathSync.call(fs, arg);
result = /** @type {Function | undefined} */ (callback)
? realpathSync.call(
fs,
arg,
/** @type {Exclude<Parameters<NonNullable<FileSystem["realpath"]>>[1], StringCallback>} */
(options)
)
: realpathSync.call(fs, arg);
} catch (e) {
return (callback || options)(e);
return (callback || options)(
/** @type {NodeJS.ErrnoException | null} */ (e)
);
}
(callback || options)(null, result);

(callback || options)(null, /** @type {any} */ (result));
}
);
this.realpathSync =
/** @type {SyncFileSystem["realpathSync"]} */
((arg, options) => realpathSync.call(fs, arg, options));
(
(arg, options) =>
realpathSync.call(
fs,
arg,
/** @type {Parameters<NonNullable<SyncFileSystem["realpathSync"]>>[1]} */
(options)
)
);
}
}
module.exports = SyncAsyncFileSystemDecorator;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"lint-staged": "^10.4.0",
"memfs": "^3.2.0",
"prettier": "^2.1.2",
"tooling": "webpack/tooling#v1.14.0",
"typescript": "^5.0.4"
"tooling": "webpack/tooling#v1.23.1",
"typescript": "^5.3.3"
},
"engines": {
"node": ">=10.13.0"
Expand Down
48 changes: 39 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,14 @@
resolved "https://registry.yarnpkg.com/@jridgewell/set-array/-/set-array-1.1.2.tgz#7c6cf998d6d20b914c0a55a91ae928ff25965e72"
integrity sha512-xnkseuNADM0gt2bs+BvhO0p78Mk762YnZdsuzFV018NoG1Sj1SCQvpSqa7XUaTam5vAGasABV9qXASMKnFMwMw==

"@jridgewell/source-map@^0.3.3":
version "0.3.5"
resolved "https://registry.yarnpkg.com/@jridgewell/source-map/-/source-map-0.3.5.tgz#a3bb4d5c6825aab0d281268f47f6ad5853431e91"
integrity sha512-UTYAUj/wviwdsMfzoSJspJxbkH5o1snzwX0//0ENX1u/55kkZZkcTZP6u9bwKGkv+dkk9at4m1Cpt0uY80kcpQ==
dependencies:
"@jridgewell/gen-mapping" "^0.3.0"
"@jridgewell/trace-mapping" "^0.3.9"

"@jridgewell/[email protected]":
version "1.4.14"
resolved "https://registry.yarnpkg.com/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.14.tgz#add4c98d341472a289190b424efbdb096991bb24"
Expand Down Expand Up @@ -970,6 +978,11 @@ acorn@^8.2.4:
resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.8.2.tgz#1b2f25db02af965399b9776b0c2c391276d37c4a"
integrity sha512-xjIYgE8HBrkpd/sJqOGNspf8uHG+NOHGOw6a/Urj8taM2EXfdNAH2oFcPeIFfsv3+kz/mJrS5VuMqbNLjCa2vw==

acorn@^8.8.2:
version "8.11.3"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.11.3.tgz#71e0b14e13a4ec160724b38fb7b0f233b1b81d7a"
integrity sha512-Y9rRfJG5jcKOE0CLisYbojUjIrIEE7AGMzA/Sm4BslANhbS+cDMpgBdcPT91oJ7OuJ9hYJBx59RjbhxVnrF8Xg==

agent-base@6:
version "6.0.2"
resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-6.0.2.tgz#49fff58577cfee3f37176feab4c22e00f86d7f77"
Expand All @@ -995,7 +1008,7 @@ ajv@^6.10.0, ajv@^6.12.4:
json-schema-traverse "^0.4.1"
uri-js "^4.2.2"

ajv@^8.0.1:
ajv@^8.0.1, ajv@^8.1.0:
version "8.12.0"
resolved "https://registry.yarnpkg.com/ajv/-/ajv-8.12.0.tgz#d1a0527323e22f53562c567c00991577dfbe19d1"
integrity sha512-sRu1kpcO9yLtYxBKvqfTeh9KzZEwO3STyX1HT+4CaDzC6HpTGYhIhPIzj9XuKU7KYDwnaeh5hcOwjy1QuJzBPA==
Expand Down Expand Up @@ -1337,6 +1350,11 @@ combined-stream@^1.0.8:
dependencies:
delayed-stream "~1.0.0"

commander@^2.20.0:
version "2.20.3"
resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33"
integrity sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==

commander@^6.2.0:
version "6.2.1"
resolved "https://registry.yarnpkg.com/commander/-/commander-6.2.1.tgz#0792eb682dfbc325999bb2b84fddddba110ac73c"
Expand Down Expand Up @@ -3585,7 +3603,7 @@ slice-ansi@^4.0.0:
astral-regex "^2.0.0"
is-fullwidth-code-point "^3.0.0"

source-map-support@^0.5.6:
source-map-support@^0.5.6, source-map-support@~0.5.20:
version "0.5.21"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.21.tgz#04fe7c7f9e1ed2d662233c28cb2b35b9f63f6e4f"
integrity sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w==
Expand Down Expand Up @@ -3754,6 +3772,16 @@ terminal-link@^2.0.0:
ansi-escapes "^4.2.1"
supports-hyperlinks "^2.0.0"

terser@^5.6.1:
version "5.29.1"
resolved "https://registry.yarnpkg.com/terser/-/terser-5.29.1.tgz#44e58045b70c09792ba14bfb7b4e14ca8755b9fa"
integrity sha512-lZQ/fyaIGxsbGxApKmoPTODIzELy3++mXhS5hOqaAWZjQtpq/hFHAc+rm29NND1rYRxRWKcjuARNwULNXa5RtQ==
dependencies:
"@jridgewell/source-map" "^0.3.3"
acorn "^8.8.2"
commander "^2.20.0"
source-map-support "~0.5.20"

test-exclude@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/test-exclude/-/test-exclude-6.0.0.tgz#04a8698661d805ea6fa293b6cb9e63ac044ef15e"
Expand Down Expand Up @@ -3817,14 +3845,16 @@ to-regex-range@^5.0.1:
dependencies:
is-number "^7.0.0"

tooling@webpack/tooling#v1.14.0:
version "1.14.0"
resolved "https://codeload.github.com/webpack/tooling/tar.gz/f8500c02cf2a04b3a0c0c2ca38ad9df959b9d02a"
tooling@webpack/tooling#v1.23.1:
version "1.23.1"
resolved "https://codeload.github.com/webpack/tooling/tar.gz/017c076821912f5e9f5c0d04508ed6fb7f0319ad"
dependencies:
"@yarnpkg/lockfile" "^1.1.0"
ajv "^8.1.0"
commondir "^1.0.1"
glob "^7.1.6"
json-schema-to-typescript "^9.1.1"
terser "^5.6.1"
yargs "^16.1.1"

tough-cookie@^4.0.0:
Expand Down Expand Up @@ -3895,10 +3925,10 @@ typedarray-to-buffer@^3.1.5:
dependencies:
is-typedarray "^1.0.0"

typescript@^5.0.4:
version "5.0.4"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.0.4.tgz#b217fd20119bd61a94d4011274e0ab369058da3b"
integrity sha512-cW9T5W9xY37cc+jfEnaUvX91foxtHkza3Nw3wkoF4sSlKn0MONdkdEndig/qPBWXNkmplh3NzayQzCiHM4/hqw==
typescript@^5.3.3:
version "5.3.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.3.3.tgz#b3ce6ba258e72e6305ba66f5c9b452aaee3ffe37"
integrity sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw==

undici-types@~5.26.4:
version "5.26.5"
Expand Down

0 comments on commit bf89031

Please sign in to comment.