Skip to content

Commit 617b37a

Browse files
authored
Merge pull request #381 from webpack/refactor-types
refactor: types
2 parents 24a4279 + 76b2c9e commit 617b37a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1453
-964
lines changed

.github/workflows/test.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
- name: Use Node.js
1515
uses: actions/setup-node@v3
1616
with:
17-
node-version: 14.x
17+
node-version: 16.x
1818
cache: yarn
1919
- run: yarn --frozen-lockfile
2020
- run: yarn lint
@@ -23,7 +23,7 @@ jobs:
2323
fail-fast: false
2424
matrix:
2525
os: [ubuntu-latest, windows-latest, macos-latest]
26-
node-version: [10.x, 12.x, 14.x, 16.x, 17.x]
26+
node-version: [10.x, 12.x, 14.x, 16.x, 18.x, 20.x]
2727
runs-on: ${{ matrix.os }}
2828
steps:
2929
- uses: actions/checkout@v3
@@ -32,8 +32,14 @@ jobs:
3232
with:
3333
node-version: ${{ matrix.node-version }}
3434
cache: yarn
35+
- name: Install dependencies
36+
run: |
37+
yarn upgrade typescript@^4 --ignore-engines
38+
yarn --frozen-lockfile
39+
if: matrix.node-version == '10.x'
3540
- name: Install dependencies
3641
run: yarn --frozen-lockfile
42+
if: matrix.node-version != '10.x'
3743
- name: Run tests with coverage
3844
run: npm run test:coverage -- --ci
3945
- if: ${{ matrix.os != 'windows-latest' }}

declarations.d.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/AliasFieldPlugin.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const DescriptionFileUtils = require("./DescriptionFileUtils");
99
const getInnerRequest = require("./getInnerRequest");
1010

1111
/** @typedef {import("./Resolver")} Resolver */
12+
/** @typedef {import("./Resolver").JsonPrimitive} JsonPrimitive */
1213
/** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
1314
/** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
1415

@@ -49,13 +50,18 @@ module.exports = class AliasFieldPlugin {
4950
);
5051
return callback();
5152
}
53+
/** @type {JsonPrimitive | undefined} */
5254
const data = Object.prototype.hasOwnProperty.call(
5355
fieldData,
5456
innerRequest
5557
)
56-
? fieldData[innerRequest]
58+
? /** @type {{[Key in string]: JsonPrimitive}} */ (fieldData)[
59+
innerRequest
60+
]
5761
: innerRequest.startsWith("./")
58-
? fieldData[innerRequest.slice(2)]
62+
? /** @type {{[Key in string]: JsonPrimitive}} */ (fieldData)[
63+
innerRequest.slice(2)
64+
]
5965
: undefined;
6066
if (data === innerRequest) return callback();
6167
if (data === undefined) return callback();
@@ -71,10 +77,11 @@ module.exports = class AliasFieldPlugin {
7177
}
7278
return callback(null, ignoreObj);
7379
}
80+
/** @type {ResolveRequest} */
7481
const obj = {
7582
...request,
76-
path: request.descriptionFileRoot,
77-
request: data,
83+
path: /** @type {string} */ (request.descriptionFileRoot),
84+
request: /** @type {string} */ (data),
7885
fullySpecified: false
7986
};
8087
resolver.doResolve(
@@ -85,7 +92,7 @@ module.exports = class AliasFieldPlugin {
8592
" with mapping '" +
8693
innerRequest +
8794
"' to '" +
88-
data +
95+
/** @type {string} */ (data) +
8996
"'",
9097
resolveContext,
9198
(err, result) => {

lib/AliasPlugin.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ const { PathType, getType } = require("./util/path");
1111
/** @typedef {import("./Resolver")} Resolver */
1212
/** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
1313
/** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
14-
/** @typedef {{alias: string|Array<string>|false, name: string, onlyModule?: boolean}} AliasOption */
14+
/** @typedef {string | Array<string> | false} Alias */
15+
/** @typedef {{alias: Alias, name: string, onlyModule?: boolean}} AliasOption */
1516

1617
module.exports = class AliasPlugin {
1718
/**
@@ -31,13 +32,22 @@ module.exports = class AliasPlugin {
3132
*/
3233
apply(resolver) {
3334
const target = resolver.ensureHook(this.target);
35+
/**
36+
* @param {string} maybeAbsolutePath path
37+
* @returns {null|string} absolute path with slash ending
38+
*/
3439
const getAbsolutePathWithSlashEnding = maybeAbsolutePath => {
3540
const type = getType(maybeAbsolutePath);
3641
if (type === PathType.AbsolutePosix || type === PathType.AbsoluteWin) {
3742
return resolver.join(maybeAbsolutePath, "_").slice(0, -1);
3843
}
3944
return null;
4045
};
46+
/**
47+
* @param {string} path path
48+
* @param {string} maybeSubPath sub path
49+
* @returns {boolean} true, if path is sub path
50+
*/
4151
const isSubPath = (path, maybeSubPath) => {
4252
const absolutePath = getAbsolutePathWithSlashEnding(maybeSubPath);
4353
if (!absolutePath) return false;
@@ -51,6 +61,7 @@ module.exports = class AliasPlugin {
5161
forEachBail(
5262
this.options,
5363
(item, callback) => {
64+
/** @type {boolean} */
5465
let shouldStop = false;
5566
if (
5667
innerRequest === item.name ||
@@ -59,7 +70,13 @@ module.exports = class AliasPlugin {
5970
? innerRequest.startsWith(`${item.name}/`)
6071
: isSubPath(innerRequest, item.name)))
6172
) {
73+
/** @type {string} */
6274
const remainingRequest = innerRequest.slice(item.name.length);
75+
/**
76+
* @param {Alias} alias alias
77+
* @param {(err?: null|Error, result?: null|ResolveRequest) => void} callback callback
78+
* @returns {void}
79+
*/
6380
const resolveWithAlias = (alias, callback) => {
6481
if (alias === false) {
6582
/** @type {ResolveRequest} */
@@ -79,6 +96,7 @@ module.exports = class AliasPlugin {
7996
) {
8097
shouldStop = true;
8198
const newRequestStr = alias + remainingRequest;
99+
/** @type {ResolveRequest} */
82100
const obj = {
83101
...request,
84102
request: newRequestStr,
@@ -104,6 +122,11 @@ module.exports = class AliasPlugin {
104122
}
105123
return callback();
106124
};
125+
/**
126+
* @param {null|Error} [err] error
127+
* @param {null|ResolveRequest} [result] result
128+
* @returns {void}
129+
*/
107130
const stoppingCallback = (err, result) => {
108131
if (err) return callback(err);
109132

lib/AppendPlugin.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"use strict";
77

88
/** @typedef {import("./Resolver")} Resolver */
9+
/** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
910
/** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
1011

1112
module.exports = class AppendPlugin {
@@ -29,6 +30,7 @@ module.exports = class AppendPlugin {
2930
resolver
3031
.getHook(this.source)
3132
.tapAsync("AppendPlugin", (request, resolveContext, callback) => {
33+
/** @type {ResolveRequest} */
3234
const obj = {
3335
...request,
3436
path: request.path + this.appending,

0 commit comments

Comments
 (0)