Skip to content

Commit c4a6a92

Browse files
refactor: more types and increase types coverage
1 parent 5ecc58d commit c4a6a92

96 files changed

Lines changed: 480 additions & 159 deletions

File tree

Some content is hidden

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

bin/webpack.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,11 @@ const isInstalled = (packageName) => {
5757
} while (dir !== (dir = path.dirname(dir)));
5858

5959
// https://github.com/nodejs/node/blob/v18.9.1/lib/internal/modules/cjs/loader.js#L1274
60-
// eslint-disable-next-line no-warning-comments
61-
// @ts-ignore
62-
for (const internalPath of require("module").globalPaths) {
60+
const { globalPaths } =
61+
/** @type {typeof import("module") & { globalPaths: string[] }} */
62+
(require("module"));
63+
64+
for (const internalPath of globalPaths) {
6365
try {
6466
if (fs.statSync(path.join(internalPath, packageName)).isDirectory()) {
6567
return true;
@@ -81,6 +83,7 @@ const runCli = (cli) => {
8183

8284
const pkgPath = require.resolve(`${cli.package}/package.json`);
8385

86+
/** @type {Record<string, EXPECTED_ANY> & { type: string, bin: Record<string, string> }} */
8487
const pkg = require(pkgPath);
8588

8689
if (pkg.type === "module" || /\.mjs/i.test(pkg.bin[cli.binName])) {

cspell.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"cacheable",
3737
"callme",
3838
"camelcase",
39+
"carryforward",
3940
"chainable",
4041
"chunkfilename",
4142
"chunkhash",
@@ -241,6 +242,7 @@
241242
"sourcemapped",
242243
"splitted",
243244
"stylesheet",
245+
"stringable",
244246
"slsh",
245247
"subdir",
246248
"subfolder",

lib/AbstractMethodError.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ function Message() {
4848
class AbstractMethodError extends WebpackError {
4949
constructor() {
5050
super(new Message().message);
51+
/** @type {string} */
5152
this.name = "AbstractMethodError";
5253
}
5354
}

lib/AsyncDependencyToInitialChunkError.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class AsyncDependencyToInitialChunkError extends WebpackError {
2222
`It's not allowed to load an initial chunk on demand. The chunk name "${chunkName}" is already used by an entrypoint.`
2323
);
2424

25+
/** @type {string} */
2526
this.name = "AsyncDependencyToInitialChunkError";
2627
this.module = module;
2728
this.loc = loc;

lib/CacheFacade.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const mergeEtags = require("./cache/mergeEtags");
1313
/** @typedef {import("./Cache")} Cache */
1414
/** @typedef {import("./Cache").Etag} Etag */
1515
/** @typedef {import("./cache/getLazyHashedEtag").HashableObject} HashableObject */
16-
/** @typedef {typeof import("./util/Hash")} HashConstructor */
16+
/** @typedef {import("./util/Hash").HashFunction} HashFunction */
1717

1818
/**
1919
* @template T
@@ -196,7 +196,7 @@ class CacheFacade {
196196
/**
197197
* @param {Cache} cache the root cache
198198
* @param {string} name the child cache name
199-
* @param {(string | HashConstructor)=} hashFunction the hash function to use
199+
* @param {HashFunction=} hashFunction the hash function to use
200200
*/
201201
constructor(cache, name, hashFunction) {
202202
this._cache = cache;

lib/CaseSensitiveModulesWarning.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ This can lead to unexpected behavior when compiling on a filesystem with other c
6363
Use equal casing. Compare these module identifiers:
6464
${modulesList}`);
6565

66+
/** @type {string} */
6667
this.name = "CaseSensitiveModulesWarning";
6768
this.module = sortedModules[0];
6869
}

lib/ChunkGraph.js

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const {
4343
/** @typedef {import("./ModuleGraph")} ModuleGraph */
4444
/** @typedef {import("./ModuleGraphConnection").ConnectionState} ConnectionState */
4545
/** @typedef {import("./RuntimeModule")} RuntimeModule */
46-
/** @typedef {typeof import("./util/Hash")} Hash */
46+
/** @typedef {import("./util/Hash").HashFunction} HashFunction */
4747
/** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
4848

4949
/** @type {ReadonlySet<string>} */
@@ -69,7 +69,9 @@ class ModuleHashInfo {
6969
* @param {string} renderedHash rendered hash
7070
*/
7171
constructor(hash, renderedHash) {
72+
/** @type {string} */
7273
this.hash = hash;
74+
/** @type {string} */
7375
this.renderedHash = renderedHash;
7476
}
7577
}
@@ -95,10 +97,11 @@ const getModuleRuntimes = (chunks) => {
9597

9698
/**
9799
* @param {SourceTypesByModule | undefined} sourceTypesByModule sourceTypesByModule
98-
* @returns {(set: SortableSet<Module>) => Map<string, SortableSet<Module>>} modules by source type
100+
* @returns {ModulesBySourceType} modules by source type
99101
*/
100102
const modulesBySourceType = (sourceTypesByModule) => (set) => {
101-
/** @type {Map<SourceType, SortableSet<Module>>} */
103+
/** @typedef {SortableSet<Module>} ModuleSortableSet */
104+
/** @type {Map<SourceType, ModuleSortableSet>} */
102105
const map = new Map();
103106
for (const module of set) {
104107
const sourceTypes =
@@ -107,6 +110,7 @@ const modulesBySourceType = (sourceTypesByModule) => (set) => {
107110
for (const sourceType of sourceTypes) {
108111
let innerSet = map.get(sourceType);
109112
if (innerSet === undefined) {
113+
/** @type {ModuleSortableSet} */
110114
innerSet = new SortableSet();
111115
map.set(sourceType, innerSet);
112116
}
@@ -122,6 +126,10 @@ const modulesBySourceType = (sourceTypesByModule) => (set) => {
122126
}
123127
return map;
124128
};
129+
130+
/** @typedef {(set: SortableSet<Module>) => Map<string, SortableSet<Module>>} ModulesBySourceType */
131+
132+
/** @type {ModulesBySourceType} */
125133
const defaultModulesBySourceType = modulesBySourceType(undefined);
126134

127135
/**
@@ -164,11 +172,14 @@ const getModulesSize = (modules) => {
164172
return size;
165173
};
166174

175+
/** @typedef {Record<string, number>} SizesOfModules */
176+
167177
/**
168178
* @param {Iterable<Module>} modules the sortable Set to get the size of
169-
* @returns {Record<string, number>} the sizes of the modules
179+
* @returns {SizesOfModules} the sizes of the modules
170180
*/
171181
const getModulesSizes = (modules) => {
182+
/** @type {SizesOfModules} */
172183
const sizes = Object.create(null);
173184
for (const module of modules) {
174185
for (const type of module.getSourceTypes()) {
@@ -199,6 +210,7 @@ const isAvailableChunk = (a, b) => {
199210
/** @typedef {Set<Chunk>} EntryInChunks */
200211
/** @typedef {Set<Chunk>} RuntimeInChunks */
201212
/** @typedef {string | number} ModuleId */
213+
/** @typedef {RuntimeSpecMap<Set<string>, RuntimeRequirements>} ChunkGraphRuntimeRequirements */
202214

203215
class ChunkGraphModule {
204216
constructor() {
@@ -212,7 +224,7 @@ class ChunkGraphModule {
212224
this.hashes = undefined;
213225
/** @type {ModuleId | null} */
214226
this.id = null;
215-
/** @type {RuntimeSpecMap<Set<string>, RuntimeRequirements> | undefined} */
227+
/** @type {ChunkGraphRuntimeRequirements | undefined} */
216228
this.runtimeRequirements = undefined;
217229
/** @type {RuntimeSpecMap<string, bigint> | undefined} */
218230
this.graphHashes = undefined;
@@ -242,7 +254,7 @@ class ChunkGraphChunk {
242254
this.runtimeRequirements = undefined;
243255
/** @type {Set<string>} */
244256
this.runtimeRequirementsInTree = new Set();
245-
257+
/** @type {ModulesBySourceType} */
246258
this._modulesBySourceType = defaultModulesBySourceType;
247259
}
248260
}
@@ -251,13 +263,14 @@ class ChunkGraphChunk {
251263
/** @typedef {Record<ModuleId, string>} IdToHashMap */
252264
/** @typedef {Record<ChunkId, IdToHashMap>} ChunkModuleHashMap */
253265
/** @typedef {Record<ChunkId, ModuleId[]>} ChunkModuleIdMap */
266+
/** @typedef {Record<ChunkId, boolean>} ChunkConditionMap */
254267

255268
/** @typedef {(a: Module, b: Module) => -1 | 0 | 1} ModuleComparator */
256269

257270
class ChunkGraph {
258271
/**
259272
* @param {ModuleGraph} moduleGraph the module graph
260-
* @param {string | Hash} hashFunction the hash function to use
273+
* @param {HashFunction} hashFunction the hash function to use
261274
*/
262275
constructor(moduleGraph, hashFunction = DEFAULTS.HASH_FUNCTION) {
263276
/**
@@ -809,6 +822,7 @@ class ChunkGraph {
809822
)) {
810823
if (filterFn(module)) {
811824
if (idToHashMap === undefined) {
825+
/** @type {IdToHashMap} */
812826
idToHashMap = Object.create(null);
813827
chunkModuleHashMap[/** @type {ChunkId} */ (asyncChunk.id)] =
814828
/** @type {IdToHashMap} */
@@ -830,9 +844,10 @@ class ChunkGraph {
830844
/**
831845
* @param {Chunk} chunk the chunk
832846
* @param {ChunkFilterPredicate} filterFn function used to filter chunks
833-
* @returns {Record<ChunkId, boolean>} chunk map
847+
* @returns {ChunkConditionMap} chunk condition map
834848
*/
835849
getChunkConditionMap(chunk, filterFn) {
850+
/** @type {ChunkConditionMap} */
836851
const map = Object.create(null);
837852
for (const c of chunk.getAllReferencedChunks()) {
838853
map[/** @type {ChunkId} */ (c.id)] = filterFn(c, this);
@@ -848,6 +863,7 @@ class ChunkGraph {
848863
*/
849864
hasModuleInGraph(chunk, filterFn, filterChunkFn) {
850865
const queue = new Set(chunk.groupsIterable);
866+
/** @type {Set<Chunk>} */
851867
const chunksProcessed = new Set();
852868

853869
for (const chunkGroup of queue) {
@@ -1556,6 +1572,7 @@ Caller might not support runtime-dependent code generation (opt-out via optimiza
15561572
const cgm = this._getChunkGraphModule(module);
15571573
const runtimeRequirementsMap = cgm.runtimeRequirements;
15581574
if (runtimeRequirementsMap === undefined) {
1575+
/** @type {ChunkGraphRuntimeRequirements} */
15591576
const map = new RuntimeSpecMap();
15601577
// TODO avoid cloning item and track ownership instead
15611578
map.set(runtime, transferOwnership ? items : new Set(items));

lib/ChunkGroup.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,15 @@ class ChunkGroup {
7979
}
8080
/** @type {number} */
8181
this.groupDebugId = debugId++;
82-
this.options = /** @type {ChunkGroupOptions} */ (options);
82+
/** @type {ChunkGroupOptions} */
83+
this.options = options;
8384
/** @type {SortableSet<ChunkGroup>} */
8485
this._children = new SortableSet(undefined, sortById);
8586
/** @type {SortableSet<ChunkGroup>} */
8687
this._parents = new SortableSet(undefined, sortById);
8788
/** @type {SortableSet<ChunkGroup>} */
8889
this._asyncEntrypoints = new SortableSet(undefined, sortById);
90+
/** @type {SortableSet<AsyncDependenciesBlock>} */
8991
this._blocks = new SortableSet();
9092
/** @type {Chunk[]} */
9193
this.chunks = [];

lib/ChunkRenderError.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class ChunkRenderError extends WebpackError {
1919
constructor(chunk, file, error) {
2020
super();
2121

22+
/** @type {string} */
2223
this.name = "ChunkRenderError";
2324
this.error = error;
2425
this.message = error.message;
@@ -28,4 +29,5 @@ class ChunkRenderError extends WebpackError {
2829
}
2930
}
3031

32+
/** @type {typeof ChunkRenderError} */
3133
module.exports = ChunkRenderError;

lib/CodeGenerationError.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,18 @@ class CodeGenerationError extends WebpackError {
1818
constructor(module, error) {
1919
super();
2020

21+
/** @type {string} */
2122
this.name = "CodeGenerationError";
23+
/** @type {Module} */
24+
this.module = module;
25+
/** @type {Error} */
2226
this.error = error;
27+
/** @type {string} */
2328
this.message = error.message;
29+
/** @type {string} */
2430
this.details = error.stack;
25-
this.module = module;
2631
}
2732
}
2833

34+
/** @type {typeof CodeGenerationError} */
2935
module.exports = CodeGenerationError;

0 commit comments

Comments
 (0)