Skip to content

Commit aee6035

Browse files
authored
Merge pull request #17414 from webpack/types-more-again
refactor: types more
2 parents 1e18b1d + 158e044 commit aee6035

25 files changed

+406
-160
lines changed

declarations/WebpackOptions.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ export type ExternalItem =
184184
| (
185185
| ((
186186
data: ExternalItemFunctionData,
187-
callback: (err?: Error, result?: ExternalItemValue) => void
187+
callback: (err?: Error | null, result?: ExternalItemValue) => void
188188
) => void)
189189
| ((data: ExternalItemFunctionData) => Promise<ExternalItemValue>)
190190
);

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/Compiler.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ class Compiler {
208208
this.root = this;
209209
/** @type {string} */
210210
this.outputPath = "";
211-
/** @type {Watching} */
211+
/** @type {Watching | undefined} */
212212
this.watching = undefined;
213213

214214
/** @type {OutputFileSystem} */
@@ -230,15 +230,15 @@ class Compiler {
230230
/** @type {Set<string | RegExp>} */
231231
this.immutablePaths = new Set();
232232

233-
/** @type {ReadonlySet<string>} */
233+
/** @type {ReadonlySet<string> | undefined} */
234234
this.modifiedFiles = undefined;
235-
/** @type {ReadonlySet<string>} */
235+
/** @type {ReadonlySet<string> | undefined} */
236236
this.removedFiles = undefined;
237-
/** @type {ReadonlyMap<string, FileSystemInfoEntry | "ignore" | null>} */
237+
/** @type {ReadonlyMap<string, FileSystemInfoEntry | "ignore" | null> | undefined} */
238238
this.fileTimestamps = undefined;
239-
/** @type {ReadonlyMap<string, FileSystemInfoEntry | "ignore" | null>} */
239+
/** @type {ReadonlyMap<string, FileSystemInfoEntry | "ignore" | null> | undefined} */
240240
this.contextTimestamps = undefined;
241-
/** @type {number} */
241+
/** @type {number | undefined} */
242242
this.fsStartTime = undefined;
243243

244244
/** @type {ResolverFactory} */

lib/ConstPlugin.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ const ConstDependency = require("./dependencies/ConstDependency");
1515
const { evaluateToString } = require("./javascript/JavascriptParserHelpers");
1616
const { parseResource } = require("./util/identifier");
1717

18+
/** @typedef {import("estree").AssignmentProperty} AssignmentProperty */
1819
/** @typedef {import("estree").Expression} Expression */
20+
/** @typedef {import("estree").Identifier} Identifier */
21+
/** @typedef {import("estree").Pattern} Pattern */
1922
/** @typedef {import("estree").SourceLocation} SourceLocation */
2023
/** @typedef {import("estree").Statement} Statement */
2124
/** @typedef {import("estree").Super} Super */
@@ -24,10 +27,14 @@ const { parseResource } = require("./util/identifier");
2427
/** @typedef {import("./javascript/JavascriptParser")} JavascriptParser */
2528
/** @typedef {import("./javascript/JavascriptParser").Range} Range */
2629

30+
/**
31+
* @param {Set<string>} declarations set of declarations
32+
* @param {Identifier | Pattern} pattern pattern to collect declarations from
33+
*/
2734
const collectDeclaration = (declarations, pattern) => {
2835
const stack = [pattern];
2936
while (stack.length > 0) {
30-
const node = stack.pop();
37+
const node = /** @type {Pattern} */ (stack.pop());
3138
switch (node.type) {
3239
case "Identifier":
3340
declarations.add(node.name);
@@ -44,7 +51,7 @@ const collectDeclaration = (declarations, pattern) => {
4451
break;
4552
case "ObjectPattern":
4653
for (const property of node.properties) {
47-
stack.push(property.value);
54+
stack.push(/** @type {AssignmentProperty} */ (property).value);
4855
}
4956
break;
5057
case "RestElement":
@@ -54,8 +61,14 @@ const collectDeclaration = (declarations, pattern) => {
5461
}
5562
};
5663

64+
/**
65+
* @param {Statement} branch branch to get hoisted declarations from
66+
* @param {boolean} includeFunctionDeclarations whether to include function declarations
67+
* @returns {Array<string>} hoisted declarations
68+
*/
5769
const getHoistedDeclarations = (branch, includeFunctionDeclarations) => {
5870
const declarations = new Set();
71+
/** @type {Array<TODO | null | undefined>} */
5972
const stack = [branch];
6073
while (stack.length > 0) {
6174
const node = stack.pop();
@@ -103,7 +116,7 @@ const getHoistedDeclarations = (branch, includeFunctionDeclarations) => {
103116
break;
104117
case "FunctionDeclaration":
105118
if (includeFunctionDeclarations) {
106-
collectDeclaration(declarations, node.id);
119+
collectDeclaration(declarations, /** @type {Identifier} */ (node.id));
107120
}
108121
break;
109122
case "VariableDeclaration":

lib/DependencyTemplates.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class DependencyTemplates {
2727

2828
/**
2929
* @param {DependencyConstructor} dependency Constructor of Dependency
30-
* @returns {DependencyTemplate} template for this dependency
30+
* @returns {DependencyTemplate | undefined} template for this dependency
3131
*/
3232
get(dependency) {
3333
return this._map.get(dependency);

lib/ExportsInfo.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1364,7 +1364,11 @@ class ExportInfo {
13641364
if (t === null) return undefined;
13651365
if (t.module !== target.module) return undefined;
13661366
if (!t.export !== !target.export) return undefined;
1367-
if (target.export && !equals(t.export, target.export)) return undefined;
1367+
if (
1368+
target.export &&
1369+
!equals(/** @type {ArrayLike<string>} */ (t.export), target.export)
1370+
)
1371+
return undefined;
13681372
result = values.next();
13691373
}
13701374
return target;

lib/FileSystemInfo.js

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ const INVALID = Symbol("invalid");
9999
* @typedef {Object} SnapshotOptimizationEntry
100100
* @property {Snapshot} snapshot
101101
* @property {number} shared
102-
* @property {Set<string>} snapshotContent
103-
* @property {Set<SnapshotOptimizationEntry>} children
102+
* @property {Set<string> | undefined} snapshotContent
103+
* @property {Set<SnapshotOptimizationEntry> | undefined} children
104104
*/
105105

106106
/**
@@ -115,6 +115,12 @@ const INVALID = Symbol("invalid");
115115
* @property {Set<string>} resolveDependencies.missing list of missing entries
116116
*/
117117

118+
/**
119+
* @typedef {Object} SnapshotOptions
120+
* @property {boolean=} hash should use hash to snapshot
121+
* @property {boolean=} timestamp should use timestamp to snapshot
122+
*/
123+
118124
const DONE_ITERATOR_RESULT = new Set().keys().next();
119125

120126
// cspell:word tshs
@@ -137,7 +143,7 @@ class SnapshotIterable {
137143
let state = 0;
138144
/** @type {IterableIterator<string>} */
139145
let it;
140-
/** @type {(Snapshot) => (Map<string, any> | Set<string>)[]} */
146+
/** @type {(snapshot: Snapshot) => (Map<string, any> | Set<string>)[]} */
141147
let getMaps;
142148
/** @type {(Map<string, any> | Set<string>)[]} */
143149
let maps;
@@ -552,7 +558,7 @@ class SnapshotOptimization {
552558
}
553559
};
554560

555-
/** @type {SnapshotOptimizationEntry} */
561+
/** @type {SnapshotOptimizationEntry | undefined} */
556562
let newOptimizationEntry = undefined;
557563

558564
const capturedFilesSize = capturedFiles.size;
@@ -757,10 +763,9 @@ const mergeMaps = (a, b) => {
757763

758764
/**
759765
* @template T
760-
* @template K
761-
* @param {Set<T, K>} a source map
762-
* @param {Set<T, K>} b joining map
763-
* @returns {Set<T, K>} joined map
766+
* @param {Set<T>} a source map
767+
* @param {Set<T>} b joining map
768+
* @returns {Set<T>} joined map
764769
*/
765770
const mergeSets = (a, b) => {
766771
if (!b || b.size === 0) return a;
@@ -858,8 +863,8 @@ const getManagedItem = (managedPath, path) => {
858863

859864
/**
860865
* @template {ContextFileSystemInfoEntry | ContextTimestampAndHash} T
861-
* @param {T} entry entry
862-
* @returns {T["resolved"] | undefined} the resolved entry
866+
* @param {T | null} entry entry
867+
* @returns {T["resolved"] | null | undefined} the resolved entry
863868
*/
864869
const getResolvedTimestamp = entry => {
865870
if (entry === null) return null;
@@ -868,15 +873,20 @@ const getResolvedTimestamp = entry => {
868873
};
869874

870875
/**
871-
* @param {ContextHash} entry entry
872-
* @returns {string | undefined} the resolved entry
876+
* @param {ContextHash | null} entry entry
877+
* @returns {string | null | undefined} the resolved entry
873878
*/
874879
const getResolvedHash = entry => {
875880
if (entry === null) return null;
876881
if (entry.resolved !== undefined) return entry.resolved;
877882
return entry.symlinks === undefined ? entry.hash : undefined;
878883
};
879884

885+
/**
886+
* @template T
887+
* @param {Set<T>} source source
888+
* @param {Set<T>} target target
889+
*/
880890
const addAll = (source, target) => {
881891
for (const key of source) target.add(key);
882892
};
@@ -1145,6 +1155,11 @@ class FileSystemInfo {
11451155
);
11461156
}
11471157

1158+
/**
1159+
* @param {string} path path
1160+
* @param {string} reason reason
1161+
* @param {any[]} args arguments
1162+
*/
11481163
_log(path, reason, ...args) {
11491164
const key = path + reason;
11501165
if (this._loggedPaths.has(key)) return;
@@ -1258,7 +1273,7 @@ class FileSystemInfo {
12581273

12591274
/**
12601275
* @param {string} path file path
1261-
* @param {function((WebpackError | null)=, string=): void} callback callback function
1276+
* @param {function((WebpackError | null)=, (string | null)=): void} callback callback function
12621277
* @returns {void}
12631278
*/
12641279
getFileHash(path, callback) {
@@ -1289,7 +1304,7 @@ class FileSystemInfo {
12891304

12901305
/**
12911306
* @param {string} path context path
1292-
* @param {function((WebpackError | null)=, ContextHash=): void} callback callback function
1307+
* @param {function((WebpackError | null)=, (ContextHash | null)=): void} callback callback function
12931308
* @returns {void}
12941309
*/
12951310
_getUnresolvedContextHash(path, callback) {
@@ -1320,7 +1335,7 @@ class FileSystemInfo {
13201335

13211336
/**
13221337
* @param {string} path context path
1323-
* @param {function((WebpackError | null)=, ContextTimestampAndHash=): void} callback callback function
1338+
* @param {function((WebpackError | null)=, (ContextTimestampAndHash | null)=): void} callback callback function
13241339
* @returns {void}
13251340
*/
13261341
_getUnresolvedContextTsh(path, callback) {
@@ -1383,14 +1398,18 @@ class FileSystemInfo {
13831398
const resolveDirectories = new Set();
13841399
/** @type {Set<string>} */
13851400
const resolveMissing = new Set();
1386-
/** @type {Map<string, string | false>} */
1401+
/** @type {Map<string, string | false | undefined>} */
13871402
const resolveResults = new Map();
13881403
const invalidResolveResults = new Set();
13891404
const resolverContext = {
13901405
fileDependencies: resolveFiles,
13911406
contextDependencies: resolveDirectories,
13921407
missingDependencies: resolveMissing
13931408
};
1409+
/**
1410+
* @param {string} expected expected result
1411+
* @returns {string} expected result
1412+
*/
13941413
const expectedToString = expected => {
13951414
return expected ? ` (expected ${expected})` : "";
13961415
};
@@ -1610,7 +1629,7 @@ class FileSystemInfo {
16101629
break;
16111630
}
16121631
// Check commonjs cache for the module
1613-
/** @type {NodeModule} */
1632+
/** @type {NodeModule | undefined} */
16141633
const module = require.cache[path];
16151634
if (module && Array.isArray(module.children)) {
16161635
children: for (const child of module.children) {
@@ -1910,13 +1929,11 @@ class FileSystemInfo {
19101929

19111930
/**
19121931
*
1913-
* @param {number} startTime when processing the files has started
1932+
* @param {number | null | undefined} startTime when processing the files has started
19141933
* @param {Iterable<string>} files all files
19151934
* @param {Iterable<string>} directories all directories
19161935
* @param {Iterable<string>} missing all missing files or directories
1917-
* @param {Object} options options object (for future extensions)
1918-
* @param {boolean=} options.hash should use hash to snapshot
1919-
* @param {boolean=} options.timestamp should use timestamp to snapshot
1936+
* @param {SnapshotOptions | null | undefined} options options object (for future extensions)
19201937
* @param {function((WebpackError | null)=, (Snapshot | null)=): void} callback callback function
19211938
* @returns {void}
19221939
*/
@@ -2053,6 +2070,9 @@ class FileSystemInfo {
20532070
}
20542071
return capturedItems;
20552072
};
2073+
/**
2074+
* @param {Set<string>} capturedFiles captured files
2075+
*/
20562076
const processCapturedFiles = capturedFiles => {
20572077
switch (mode) {
20582078
case 3:
@@ -2639,6 +2659,10 @@ class FileSystemInfo {
26392659
}
26402660
}
26412661
}
2662+
/**
2663+
* @param {string} path file path
2664+
* @param {string} hash hash
2665+
*/
26422666
const processFileHashSnapshot = (path, hash) => {
26432667
const cache = this._fileHashes.get(path);
26442668
if (cache !== undefined) {
@@ -2921,7 +2945,7 @@ class FileSystemInfo {
29212945

29222946
const hash = createHash(this._hashFunction);
29232947

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

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

@@ -2985,7 +3009,7 @@ class FileSystemInfo {
29853009
* @param {function(string, IStats, function(Error=, ItemType=): void): void} options.fromFile called when context item is a file
29863010
* @param {function(string, IStats, function(Error=, ItemType=): void): void} options.fromDirectory called when context item is a directory
29873011
* @param {function(string[], ItemType[]): T} options.reduce called from all context items
2988-
* @param {function((Error | null)=, (T)=): void} callback callback
3012+
* @param {function((Error | null)=, (T | null)=): void} callback callback
29893013
*/
29903014
_readContext(
29913015
{
@@ -3172,6 +3196,7 @@ class FileSystemInfo {
31723196
* @returns {void}
31733197
*/
31743198
_resolveContextTimestamp(entry, callback) {
3199+
/** @type {string[]} */
31753200
const hashes = [];
31763201
let safeTime = 0;
31773202
processAsyncTree(
@@ -3280,6 +3305,7 @@ class FileSystemInfo {
32803305
* @returns {void}
32813306
*/
32823307
_resolveContextHash(entry, callback) {
3308+
/** @type {string[]} */
32833309
const hashes = [];
32843310
processAsyncTree(
32853311
entry.symlinks,
@@ -3436,7 +3462,9 @@ class FileSystemInfo {
34363462
* @returns {void}
34373463
*/
34383464
_resolveContextTsh(entry, callback) {
3465+
/** @type {string[]} */
34393466
const hashes = [];
3467+
/** @type {string[]} */
34403468
const tsHashes = [];
34413469
let safeTime = 0;
34423470
processAsyncTree(
@@ -3554,7 +3582,7 @@ class FileSystemInfo {
35543582
}
35553583
let data;
35563584
try {
3557-
data = JSON.parse(content.toString("utf-8"));
3585+
data = JSON.parse(/** @type {Buffer} */ (content).toString("utf-8"));
35583586
} catch (e) {
35593587
return callback(e);
35603588
}

0 commit comments

Comments
 (0)