Skip to content

Commit 3092f7c

Browse files
refactor(types): more
1 parent cc734af commit 3092f7c

File tree

11 files changed

+129
-41
lines changed

11 files changed

+129
-41
lines changed

lib/BannerPlugin.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
1111
const Template = require("./Template");
1212
const createSchemaValidation = require("./util/create-schema-validation");
1313

14+
/** @typedef {import("../declarations/plugins/BannerPlugin").BannerFunction} BannerFunction */
1415
/** @typedef {import("../declarations/plugins/BannerPlugin").BannerPluginArgument} BannerPluginArgument */
1516
/** @typedef {import("../declarations/plugins/BannerPlugin").BannerPluginOptions} BannerPluginOptions */
1617
/** @typedef {import("./Compiler")} Compiler */
@@ -60,7 +61,7 @@ class BannerPlugin {
6061
const getBanner = bannerOption;
6162
this.banner = this.options.raw
6263
? getBanner
63-
: data => wrapComment(getBanner(data));
64+
: /** @type {BannerFunction} */ data => wrapComment(getBanner(data));
6465
} else {
6566
const banner = this.options.raw
6667
? bannerOption

lib/FileSystemInfo.js

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class SnapshotIterable {
143143
let state = 0;
144144
/** @type {IterableIterator<string>} */
145145
let it;
146-
/** @type {(Snapshot) => (Map<string, any> | Set<string>)[]} */
146+
/** @type {(snapshot: Snapshot) => (Map<string, any> | Set<string>)[]} */
147147
let getMaps;
148148
/** @type {(Map<string, any> | Set<string>)[]} */
149149
let maps;
@@ -882,6 +882,11 @@ const getResolvedHash = entry => {
882882
return entry.symlinks === undefined ? entry.hash : undefined;
883883
};
884884

885+
/**
886+
* @template T
887+
* @param {Set<T>} source source
888+
* @param {Set<T>} target target
889+
*/
885890
const addAll = (source, target) => {
886891
for (const key of source) target.add(key);
887892
};
@@ -1150,6 +1155,11 @@ class FileSystemInfo {
11501155
);
11511156
}
11521157

1158+
/**
1159+
* @param {string} path path
1160+
* @param {string} reason reason
1161+
* @param {string[]} args arguments
1162+
*/
11531163
_log(path, reason, ...args) {
11541164
const key = path + reason;
11551165
if (this._loggedPaths.has(key)) return;
@@ -1263,7 +1273,7 @@ class FileSystemInfo {
12631273

12641274
/**
12651275
* @param {string} path file path
1266-
* @param {function((WebpackError | null)=, string=): void} callback callback function
1276+
* @param {function((WebpackError | null)=, (string | null)=): void} callback callback function
12671277
* @returns {void}
12681278
*/
12691279
getFileHash(path, callback) {
@@ -1294,7 +1304,7 @@ class FileSystemInfo {
12941304

12951305
/**
12961306
* @param {string} path context path
1297-
* @param {function((WebpackError | null)=, ContextHash=): void} callback callback function
1307+
* @param {function((WebpackError | null)=, (ContextHash | null)=): void} callback callback function
12981308
* @returns {void}
12991309
*/
13001310
_getUnresolvedContextHash(path, callback) {
@@ -1325,7 +1335,7 @@ class FileSystemInfo {
13251335

13261336
/**
13271337
* @param {string} path context path
1328-
* @param {function((WebpackError | null)=, ContextTimestampAndHash=): void} callback callback function
1338+
* @param {function((WebpackError | null)=, (ContextTimestampAndHash | null)=): void} callback callback function
13291339
* @returns {void}
13301340
*/
13311341
_getUnresolvedContextTsh(path, callback) {
@@ -1396,6 +1406,10 @@ class FileSystemInfo {
13961406
contextDependencies: resolveDirectories,
13971407
missingDependencies: resolveMissing
13981408
};
1409+
/**
1410+
* @param {string} expected expected result
1411+
* @returns {string} expected result
1412+
*/
13991413
const expectedToString = expected => {
14001414
return expected ? ` (expected ${expected})` : "";
14011415
};
@@ -2056,6 +2070,9 @@ class FileSystemInfo {
20562070
}
20572071
return capturedItems;
20582072
};
2073+
/**
2074+
* @param {Set<string>} capturedFiles captured files
2075+
*/
20592076
const processCapturedFiles = capturedFiles => {
20602077
switch (mode) {
20612078
case 3:
@@ -2928,7 +2945,7 @@ class FileSystemInfo {
29282945

29292946
const hash = createHash(this._hashFunction);
29302947

2931-
hash.update(content);
2948+
hash.update(/** @type {string | Buffer} */ (content));
29322949

29332950
const digest = /** @type {string} */ (hash.digest("hex"));
29342951

@@ -2992,7 +3009,7 @@ class FileSystemInfo {
29923009
* @param {function(string, IStats, function(Error=, ItemType=): void): void} options.fromFile called when context item is a file
29933010
* @param {function(string, IStats, function(Error=, ItemType=): void): void} options.fromDirectory called when context item is a directory
29943011
* @param {function(string[], ItemType[]): T} options.reduce called from all context items
2995-
* @param {function((Error | null)=, (T)=): void} callback callback
3012+
* @param {function((Error | null)=, (T | null)=): void} callback callback
29963013
*/
29973014
_readContext(
29983015
{
@@ -3179,6 +3196,7 @@ class FileSystemInfo {
31793196
* @returns {void}
31803197
*/
31813198
_resolveContextTimestamp(entry, callback) {
3199+
/** @type {string[]} */
31823200
const hashes = [];
31833201
let safeTime = 0;
31843202
processAsyncTree(
@@ -3287,6 +3305,7 @@ class FileSystemInfo {
32873305
* @returns {void}
32883306
*/
32893307
_resolveContextHash(entry, callback) {
3308+
/** @type {string[]} */
32903309
const hashes = [];
32913310
processAsyncTree(
32923311
entry.symlinks,
@@ -3443,7 +3462,9 @@ class FileSystemInfo {
34433462
* @returns {void}
34443463
*/
34453464
_resolveContextTsh(entry, callback) {
3465+
/** @type {string[]} */
34463466
const hashes = [];
3467+
/** @type {string[]} */
34473468
const tsHashes = [];
34483469
let safeTime = 0;
34493470
processAsyncTree(
@@ -3561,7 +3582,7 @@ class FileSystemInfo {
35613582
}
35623583
let data;
35633584
try {
3564-
data = JSON.parse(content.toString("utf-8"));
3585+
data = JSON.parse(/** @type {Buffer} */ (content).toString("utf-8"));
35653586
} catch (e) {
35663587
return callback(e);
35673588
}

lib/buildChunkGraph.js

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const { getEntryRuntime, mergeRuntime } = require("./util/runtime");
1616
/** @typedef {import("./Compilation")} Compilation */
1717
/** @typedef {import("./DependenciesBlock")} DependenciesBlock */
1818
/** @typedef {import("./Dependency")} Dependency */
19+
/** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
1920
/** @typedef {import("./Entrypoint")} Entrypoint */
2021
/** @typedef {import("./Module")} Module */
2122
/** @typedef {import("./ModuleGraph")} ModuleGraph */
@@ -39,15 +40,15 @@ const { getEntryRuntime, mergeRuntime } = require("./util/runtime");
3940
* @typedef {Object} ChunkGroupInfo
4041
* @property {ChunkGroup} chunkGroup the chunk group
4142
* @property {RuntimeSpec} runtime the runtimes
42-
* @property {ModuleSetPlus} minAvailableModules current minimal set of modules available at this point
43-
* @property {boolean} minAvailableModulesOwned true, if minAvailableModules is owned and can be modified
43+
* @property {ModuleSetPlus | undefined} minAvailableModules current minimal set of modules available at this point
44+
* @property {boolean | undefined} minAvailableModulesOwned true, if minAvailableModules is owned and can be modified
4445
* @property {ModuleSetPlus[]} availableModulesToBeMerged enqueued updates to the minimal set of available modules
4546
* @property {Set<Module>=} skippedItems modules that were skipped because module is already available in parent chunks (need to reconsider when minAvailableModules is shrinking)
4647
* @property {Set<[Module, ConnectionState]>=} skippedModuleConnections referenced modules that where skipped because they were not active in this runtime
47-
* @property {ModuleSetPlus} resultingAvailableModules set of modules available including modules from this chunk group
48-
* @property {Set<ChunkGroupInfo>} children set of children chunk groups, that will be revisited when availableModules shrink
49-
* @property {Set<ChunkGroupInfo>} availableSources set of chunk groups that are the source for minAvailableModules
50-
* @property {Set<ChunkGroupInfo>} availableChildren set of chunk groups which depend on the this chunk group as availableSource
48+
* @property {ModuleSetPlus | undefined} resultingAvailableModules set of modules available including modules from this chunk group
49+
* @property {Set<ChunkGroupInfo> | undefined} children set of children chunk groups, that will be revisited when availableModules shrink
50+
* @property {Set<ChunkGroupInfo> | undefined} availableSources set of chunk groups that are the source for minAvailableModules
51+
* @property {Set<ChunkGroupInfo> | undefined} availableChildren set of chunk groups which depend on the this chunk group as availableSource
5152
* @property {number} preOrderIndex next pre order index
5253
* @property {number} postOrderIndex next post order index
5354
* @property {boolean} chunkLoading has a chunk loading mechanism
@@ -199,6 +200,7 @@ const visitModules = (
199200

200201
/** @type {RuntimeSpec | false} */
201202
let blockModulesMapRuntime = false;
203+
/** @type {Map<DependenciesBlock, (Module | ConnectionState)[]>} */
202204
let blockModulesMap;
203205

204206
/**
@@ -239,7 +241,7 @@ const visitModules = (
239241
extractBlockModules(module, moduleGraph, runtime, blockModulesMap);
240242
blockModules = blockModulesMap.get(block);
241243
logger.timeAggregate("visitModules: prepare");
242-
return blockModules;
244+
return /** @type {(Module | ConnectionState)[]} */ (blockModules);
243245
}
244246
};
245247

@@ -290,7 +292,7 @@ const visitModules = (
290292
for (const [chunkGroup, modules] of inputEntrypointsAndModules) {
291293
const runtime = getEntryRuntime(
292294
compilation,
293-
chunkGroup.name,
295+
/** @type {string} */ (chunkGroup.name),
294296
chunkGroup.options
295297
);
296298
/** @type {ChunkGroupInfo} */
@@ -352,7 +354,9 @@ const visitModules = (
352354
const { chunkGroup } = chunkGroupInfo;
353355
chunkGroupInfo.availableSources = new Set();
354356
for (const parent of chunkGroup.parentsIterable) {
355-
const parentChunkGroupInfo = chunkGroupInfoMap.get(parent);
357+
const parentChunkGroupInfo =
358+
/** @type {ChunkGroupInfo} */
359+
(chunkGroupInfoMap.get(parent));
356360
chunkGroupInfo.availableSources.add(parentChunkGroupInfo);
357361
if (parentChunkGroupInfo.availableChildren === undefined) {
358362
parentChunkGroupInfo.availableChildren = new Set();
@@ -399,15 +403,15 @@ const visitModules = (
399403
// 1. We create a chunk group with single chunk in it for this Block
400404
// but only once (blockChunkGroups map)
401405
let cgi = blockChunkGroups.get(b);
402-
/** @type {ChunkGroup} */
406+
/** @type {ChunkGroup | undefined} */
403407
let c;
404-
/** @type {Entrypoint} */
408+
/** @type {Entrypoint | undefined} */
405409
let entrypoint;
406410
const entryOptions = b.groupOptions && b.groupOptions.entryOptions;
407411
if (cgi === undefined) {
408412
const chunkName = (b.groupOptions && b.groupOptions.name) || b.chunkName;
409413
if (entryOptions) {
410-
cgi = namedAsyncEntrypoints.get(chunkName);
414+
cgi = namedAsyncEntrypoints.get(/** @type {string} */ (chunkName));
411415
if (!cgi) {
412416
entrypoint = compilation.addAsyncEntrypoint(
413417
entryOptions,
@@ -505,7 +509,11 @@ const visitModules = (
505509
c = cgi.chunkGroup;
506510
if (c.isInitial()) {
507511
compilation.errors.push(
508-
new AsyncDependencyToInitialChunkError(chunkName, module, b.loc)
512+
new AsyncDependencyToInitialChunkError(
513+
/** @type {string} */ (chunkName),
514+
module,
515+
b.loc
516+
)
509517
);
510518
c = chunkGroup;
511519
} else {
@@ -515,7 +523,7 @@ const visitModules = (
515523
}
516524
blockConnections.set(b, []);
517525
}
518-
blockChunkGroups.set(b, cgi);
526+
blockChunkGroups.set(b, /** @type {ChunkGroupInfo} */ (cgi));
519527
} else if (entryOptions) {
520528
entrypoint = /** @type {Entrypoint} */ (cgi.chunkGroup);
521529
} else {
@@ -536,7 +544,7 @@ const visitModules = (
536544
connectList = new Set();
537545
queueConnect.set(chunkGroupInfo, connectList);
538546
}
539-
connectList.add(cgi);
547+
connectList.add(/** @type {ChunkGroupInfo} */ (cgi));
540548

541549
// TODO check if this really need to be done for each traversal
542550
// or if it is enough when it's queued when created
@@ -547,7 +555,7 @@ const visitModules = (
547555
module: module,
548556
chunk: c.chunks[0],
549557
chunkGroup: c,
550-
chunkGroupInfo: cgi
558+
chunkGroupInfo: /** @type {ChunkGroupInfo} */ (cgi)
551559
});
552560
} else if (entrypoint !== undefined) {
553561
chunkGroupInfo.chunkGroup.addAsyncEntrypoint(entrypoint);
@@ -690,7 +698,7 @@ const visitModules = (
690698
const processQueue = () => {
691699
while (queue.length) {
692700
statProcessedQueueItems++;
693-
const queueItem = queue.pop();
701+
const queueItem = /** @type {QueueItem} */ (queue.pop());
694702
module = queueItem.module;
695703
block = queueItem.block;
696704
chunk = queueItem.chunk;
@@ -1087,7 +1095,9 @@ const visitModules = (
10871095

10881096
const processChunkGroupsForCombining = () => {
10891097
for (const info of chunkGroupsForCombining) {
1090-
for (const source of info.availableSources) {
1098+
for (const source of /** @type {Set<ChunkGroupInfo>} */ (
1099+
info.availableSources
1100+
)) {
10911101
if (!source.minAvailableModules) {
10921102
chunkGroupsForCombining.delete(info);
10931103
break;
@@ -1106,7 +1116,9 @@ const visitModules = (
11061116
}
11071117
};
11081118
// combine minAvailableModules from all resultingAvailableModules
1109-
for (const source of info.availableSources) {
1119+
for (const source of /** @type {Set<ChunkGroupInfo>} */ (
1120+
info.availableSources
1121+
)) {
11101122
const resultingAvailableModules =
11111123
calculateResultingAvailableModules(source);
11121124
mergeSet(resultingAvailableModules);
@@ -1126,7 +1138,9 @@ const visitModules = (
11261138
for (const info of outdatedChunkGroupInfo) {
11271139
// 1. Reconsider skipped items
11281140
if (info.skippedItems !== undefined) {
1129-
const { minAvailableModules } = info;
1141+
const minAvailableModules =
1142+
/** @type {ModuleSetPlus} */
1143+
(info.minAvailableModules);
11301144
for (const module of info.skippedItems) {
11311145
if (
11321146
!minAvailableModules.has(module) &&
@@ -1147,7 +1161,9 @@ const visitModules = (
11471161

11481162
// 2. Reconsider skipped connections
11491163
if (info.skippedModuleConnections !== undefined) {
1150-
const { minAvailableModules } = info;
1164+
const minAvailableModules =
1165+
/** @type {ModuleSetPlus} */
1166+
(info.minAvailableModules);
11511167
for (const entry of info.skippedModuleConnections) {
11521168
const [module, activeState] = entry;
11531169
if (activeState === false) continue;

lib/javascript/JavascriptModulesPlugin.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,27 @@ const chunkHasJs = (chunk, chunkGraph) => {
6060
: false;
6161
};
6262

63+
/**
64+
* @param {Module} module a module
65+
* @param {string} code the code
66+
* @returns {string} generated code for the stack
67+
*/
6368
const printGeneratedCodeForStack = (module, code) => {
6469
const lines = code.split("\n");
6570
const n = `${lines.length}`.length;
6671
return `\n\nGenerated code for ${module.identifier()}\n${lines
67-
.map((line, i, lines) => {
68-
const iStr = `${i + 1}`;
69-
return `${" ".repeat(n - iStr.length)}${iStr} | ${line}`;
70-
})
72+
.map(
73+
/**
74+
* @param {string} line the line
75+
* @param {number} i the index
76+
* @param {string[]} lines the lines
77+
* @returns {string} the line with line number
78+
*/
79+
(line, i, lines) => {
80+
const iStr = `${i + 1}`;
81+
return `${" ".repeat(n - iStr.length)}${iStr} | ${line}`;
82+
}
83+
)
7184
.join("\n")}`;
7285
};
7386

@@ -1007,6 +1020,9 @@ class JavascriptModulesPlugin {
10071020
const useRequire =
10081021
requireFunction || interceptModuleExecution || moduleUsed;
10091022

1023+
/**
1024+
* @type {{startup: string[], beforeStartup: string[], header: string[], afterStartup: string[], allowInlineStartup: boolean}}
1025+
*/
10101026
const result = {
10111027
header: [],
10121028
beforeStartup: [],

lib/library/AmdLibraryPlugin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ class AmdLibraryPlugin extends AbstractLibraryPlugin {
6868
}
6969
}
7070
return {
71-
name: /** @type {string=} */ (name),
72-
amdContainer: /** @type {string=} */ (amdContainer)
71+
name: /** @type {string} */ (name),
72+
amdContainer: /** @type {string} */ (amdContainer)
7373
};
7474
}
7575

0 commit comments

Comments
 (0)