Skip to content

Commit

Permalink
refactor: more types
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed May 7, 2023
1 parent 74c17ad commit d529199
Show file tree
Hide file tree
Showing 19 changed files with 95 additions and 22 deletions.
1 change: 1 addition & 0 deletions lib/AliasFieldPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ module.exports = class AliasFieldPlugin {
}
return callback(null, ignoreObj);
}
/** @type {ResolveRequest} */
const obj = {
...request,
path: /** @type {string} */ (request.descriptionFileRoot),
Expand Down
1 change: 1 addition & 0 deletions lib/AliasPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ module.exports = class AliasPlugin {
) {
shouldStop = true;
const newRequestStr = alias + remainingRequest;
/** @type {ResolveRequest} */
const obj = {
...request,
request: newRequestStr,
Expand Down
2 changes: 2 additions & 0 deletions lib/AppendPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

/** @typedef {import("./Resolver")} Resolver */
/** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
/** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */

module.exports = class AppendPlugin {
/**
Expand All @@ -29,6 +30,7 @@ module.exports = class AppendPlugin {
resolver
.getHook(this.source)
.tapAsync("AppendPlugin", (request, resolveContext, callback) => {
/** @type {ResolveRequest} */
const obj = {
...request,
path: request.path + this.appending,
Expand Down
4 changes: 4 additions & 0 deletions lib/CachedInputFileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ const nextTick = require("process").nextTick;
/** @typedef {import("./Resolver").FileSystem} FileSystem */
/** @typedef {import("./Resolver").SyncFileSystem} SyncFileSystem */

/**
* @param {string} path path
* @returns {string} dirname
*/
const dirname = path => {
let idx = path.length - 1;
while (idx >= 0) {
Expand Down
7 changes: 7 additions & 0 deletions lib/CloneBasenamePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@
const basename = require("./getPaths").basename;

/** @typedef {import("./Resolver")} Resolver */
/** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
/** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */

module.exports = class CloneBasenamePlugin {
/**
* @param {string | ResolveStepHook} source source
* @param {string | ResolveStepHook} target target
*/
constructor(source, target) {
this.source = source;
this.target = target;
Expand All @@ -27,6 +33,7 @@ module.exports = class CloneBasenamePlugin {
const requestPath = /** @type {string} */ (request.path);
const filename = /** @type {string} */ (basename(requestPath));
const filePath = resolver.join(requestPath, filename);
/** @type {ResolveRequest} */
const obj = {
...request,
path: filePath,
Expand Down
2 changes: 2 additions & 0 deletions lib/DescriptionFilePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const DescriptionFileUtils = require("./DescriptionFileUtils");

/** @typedef {import("./Resolver")} Resolver */
/** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
/** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */

module.exports = class DescriptionFilePlugin {
/**
Expand Down Expand Up @@ -64,6 +65,7 @@ module.exports = class DescriptionFilePlugin {
}
const relativePath =
"." + path.substr(result.directory.length).replace(/\\/g, "/");
/** @type {ResolveRequest} */
const obj = {
...request,
descriptionFilePath: result.path,
Expand Down
3 changes: 3 additions & 0 deletions lib/ExportsFieldPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const { checkImportsExportsFieldTarget } = require("./util/path");

/** @typedef {import("./Resolver")} Resolver */
/** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
/** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
/** @typedef {import("./util/entrypoints").ExportsField} ExportsField */
/** @typedef {import("./util/entrypoints").FieldProcessor} FieldProcessor */

Expand Down Expand Up @@ -73,6 +74,7 @@ module.exports = class ExportsFieldPlugin {
);
}

/** @type {string[]} */
let paths;

try {
Expand Down Expand Up @@ -122,6 +124,7 @@ module.exports = class ExportsFieldPlugin {
return callback(error);
}

/** @type {ResolveRequest} */
const obj = {
...request,
request: undefined,
Expand Down
1 change: 1 addition & 0 deletions lib/ImportsFieldPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ module.exports = class ImportsFieldPlugin {
);
}

/** @type {string[]} */
let paths;

try {
Expand Down
11 changes: 9 additions & 2 deletions lib/JoinRequestPartPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

/** @typedef {import("./Resolver")} Resolver */
/** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
/** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */

const namespaceStartCharCode = "@".charCodeAt(0);

Expand Down Expand Up @@ -38,16 +39,22 @@ module.exports = class JoinRequestPartPlugin {
i = req.indexOf("/", i + 1);
}

let moduleName, remainingRequest, fullySpecified;
/** @type {string} */
let moduleName;
/** @type {string} */
let remainingRequest;
/** @type {boolean} */
let fullySpecified;
if (i < 0) {
moduleName = req;
remainingRequest = ".";
fullySpecified = false;
} else {
moduleName = req.slice(0, i);
remainingRequest = "." + req.slice(i);
fullySpecified = request.fullySpecified;
fullySpecified = /** @type {boolean} */ (request.fullySpecified);
}
/** @type {ResolveRequest} */
const obj = {
...request,
path: resolver.join(
Expand Down
2 changes: 2 additions & 0 deletions lib/JoinRequestPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

/** @typedef {import("./Resolver")} Resolver */
/** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
/** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */

module.exports = class JoinRequestPlugin {
/**
Expand All @@ -29,6 +30,7 @@ module.exports = class JoinRequestPlugin {
.tapAsync("JoinRequestPlugin", (request, resolveContext, callback) => {
const requestPath = /** @type {string} */ (request.path);
const requestRequest = /** @type {string} */ (request.request);
/** @type {ResolveRequest} */
const obj = {
...request,
path: resolver.join(requestPath, requestRequest),
Expand Down
5 changes: 5 additions & 0 deletions lib/MainFieldPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const DescriptionFileUtils = require("./DescriptionFileUtils");

/** @typedef {import("./Resolver")} Resolver */
/** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
/** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
/** @typedef {{name: string|Array<string>, forceRelative: boolean}} MainFieldOptions */

const alreadyTriedMainField = Symbol("alreadyTriedMainField");
Expand Down Expand Up @@ -42,6 +43,7 @@ module.exports = class MainFieldPlugin {
)
return callback();
const filename = path.basename(request.descriptionFilePath);
/** @type {string} */
let mainModule = DescriptionFileUtils.getField(
request.descriptionFileData,
this.options.name
Expand All @@ -57,11 +59,14 @@ module.exports = class MainFieldPlugin {
}
if (this.options.forceRelative && !/^\.\.?\//.test(mainModule))
mainModule = "./" + mainModule;
/** @type {ResolveRequest} */
const obj = {
...request,
request: mainModule,
module: false,
directory: mainModule.endsWith("/"),
// TODO improve types for this
// @ts-ignore
[alreadyTriedMainField]: request.descriptionFilePath
};
return resolver.doResolve(
Expand Down
2 changes: 2 additions & 0 deletions lib/ModulesInHierarchicalDirectoriesPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const getPaths = require("./getPaths");

/** @typedef {import("./Resolver")} Resolver */
/** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
/** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */

module.exports = class ModulesInHierarchicalDirectoriesPlugin {
/**
Expand Down Expand Up @@ -48,6 +49,7 @@ module.exports = class ModulesInHierarchicalDirectoriesPlugin {
(addr, callback) => {
fs.stat(addr, (err, stat) => {
if (!err && stat && stat.isDirectory()) {
/** @type {ResolveRequest} */
const obj = {
...request,
path: addr,
Expand Down
2 changes: 2 additions & 0 deletions lib/ModulesInRootPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

/** @typedef {import("./Resolver")} Resolver */
/** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
/** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */

module.exports = class ModulesInRootPlugin {
/**
Expand All @@ -29,6 +30,7 @@ module.exports = class ModulesInRootPlugin {
resolver
.getHook(this.source)
.tapAsync("ModulesInRootPlugin", (request, resolveContext, callback) => {
/** @type {ResolveRequest} */
const obj = {
...request,
path: this.path,
Expand Down
3 changes: 2 additions & 1 deletion lib/SelfReferencePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const DescriptionFileUtils = require("./DescriptionFileUtils");

/** @typedef {import("./Resolver")} Resolver */
/** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
/** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */

const slashCode = "/".charCodeAt(0);

Expand Down Expand Up @@ -57,7 +58,7 @@ module.exports = class SelfReferencePlugin {
req.charCodeAt(name.length) === slashCode)
) {
const remainingRequest = `.${req.slice(name.length)}`;

/** @type {ResolveRequest} */
const obj = {
...request,
request: remainingRequest,
Expand Down
2 changes: 2 additions & 0 deletions lib/SymlinkPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const { getType, PathType } = require("./util/path");

/** @typedef {import("./Resolver")} Resolver */
/** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
/** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */

module.exports = class SymlinkPlugin {
/**
Expand Down Expand Up @@ -70,6 +71,7 @@ module.exports = class SymlinkPlugin {
const result = resultSegments.reduceRight((a, b) => {
return resolver.join(a, b);
});
/** @type {ResolveRequest} */
const obj = {
...request,
path: result
Expand Down
3 changes: 3 additions & 0 deletions lib/UseFilePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

/** @typedef {import("./Resolver")} Resolver */
/** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
/** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */

module.exports = class UseFilePlugin {
/**
Expand All @@ -33,6 +34,8 @@ module.exports = class UseFilePlugin {
/** @type {string} */ (request.path),
this.filename
);

/** @type {ResolveRequest} */
const obj = {
...request,
path: filePath,
Expand Down
9 changes: 6 additions & 3 deletions lib/createInnerContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@

"use strict";

/** @typedef {import("./Resolver").ResolveContext} InnerContext */
/** @typedef {import("./Resolver").ResolveContext} ResolveContext */

/**
* @param {import("./Resolver").ResolveContext} options options for inner context
* @param {ResolveContext} options options for inner context
* @param {null|string} message message to log
* @returns {InnerContext} inner context
* @returns {ResolveContext} inner context
*/
module.exports = function createInnerContext(options, message) {
let messageReported = false;
let innerLog = undefined;
if (options.log) {
if (message) {
/**
* @param {string} msg message
*/
innerLog = msg => {
if (!messageReported) {
/** @type {(function(string): void)} */
Expand Down
10 changes: 10 additions & 0 deletions lib/forEachBail.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@

"use strict";

/** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */

/**
* @template T
* @param {T[]} array array
* @param {(item: T, callback: (err?: null|Error, result?: any) => void) => void} iterator iterator
* @param {(err?: null|Error, result?: ResolveRequest) => void} callback callback after all items are iterated
* @returns {void}
*/
module.exports = function forEachBail(array, iterator, callback) {
if (array.length === 0) return callback();

let i = 0;
const next = () => {
/** @type {boolean|undefined} */
let loop = undefined;
iterator(array[i++], (err, result) => {
if (err || result !== undefined || i >= array.length) {
Expand Down
Loading

0 comments on commit d529199

Please sign in to comment.