Skip to content

Commit d15c734

Browse files
authored
Merge pull request #17394 from webpack/refactor-more-types
refactor(types): more
2 parents d1f1433 + cff406c commit d15c734

142 files changed

Lines changed: 1214 additions & 474 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.

lib/APIPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const GetFullHashRuntimeModule = require("./runtime/GetFullHashRuntimeModule");
2929
/**
3030
* @param {boolean} module true if ES module
3131
* @param {string} importMetaName `import.meta` name
32-
* @returns {Record<string, {expr: string, req: string[], type?: string, assign: boolean}>} replacements
32+
* @returns {Record<string, {expr: string, req: string[] | null, type?: string, assign: boolean}>} replacements
3333
*/
3434
function getReplacements(module, importMetaName) {
3535
return {

lib/AsyncDependenciesBlock.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ const makeSerializable = require("./util/makeSerializable");
2121

2222
class AsyncDependenciesBlock extends DependenciesBlock {
2323
/**
24-
* @param {ChunkGroupOptions & { entryOptions?: EntryOptions }} groupOptions options for the group
25-
* @param {DependencyLocation=} loc the line of code
26-
* @param {string=} request the request
24+
* @param {(ChunkGroupOptions & { entryOptions?: EntryOptions }) | null} groupOptions options for the group
25+
* @param {(DependencyLocation | null)=} loc the line of code
26+
* @param {(string | null)=} request the request
2727
*/
2828
constructor(groupOptions, loc, request) {
2929
super();

lib/ChunkGraph.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ class ChunkGraphModule {
188188
this.entryInChunks = undefined;
189189
/** @type {Set<Chunk> | undefined} */
190190
this.runtimeInChunks = undefined;
191-
/** @type {RuntimeSpecMap<ModuleHashInfo>} */
191+
/** @type {RuntimeSpecMap<ModuleHashInfo> | undefined} */
192192
this.hashes = undefined;
193193
/** @type {string | number} */
194194
this.id = null;
@@ -1388,7 +1388,7 @@ Caller might not support runtime-dependent code generation (opt-out via optimiza
13881388
*/
13891389
hasModuleHashes(module, runtime) {
13901390
const cgm = this._getChunkGraphModule(module);
1391-
const hashes = cgm.hashes;
1391+
const hashes = /** @type {RuntimeSpecMap<ModuleHashInfo>} */ (cgm.hashes);
13921392
return hashes && hashes.has(runtime);
13931393
}
13941394

@@ -1399,7 +1399,7 @@ Caller might not support runtime-dependent code generation (opt-out via optimiza
13991399
*/
14001400
getModuleHash(module, runtime) {
14011401
const cgm = this._getChunkGraphModule(module);
1402-
const hashes = cgm.hashes;
1402+
const hashes = /** @type {RuntimeSpecMap<ModuleHashInfo>} */ (cgm.hashes);
14031403
return this._getModuleHashInfo(module, hashes, runtime).hash;
14041404
}
14051405

@@ -1410,7 +1410,7 @@ Caller might not support runtime-dependent code generation (opt-out via optimiza
14101410
*/
14111411
getRenderedModuleHash(module, runtime) {
14121412
const cgm = this._getChunkGraphModule(module);
1413-
const hashes = cgm.hashes;
1413+
const hashes = /** @type {RuntimeSpecMap<ModuleHashInfo>} */ (cgm.hashes);
14141414
return this._getModuleHashInfo(module, hashes, runtime).renderedHash;
14151415
}
14161416

lib/ChunkGroup.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,15 @@ class ChunkGroup {
109109
*/
110110
addOptions(options) {
111111
for (const key of Object.keys(options)) {
112-
if (this.options[key] === undefined) {
113-
this.options[key] = options[key];
114-
} else if (this.options[key] !== options[key]) {
112+
if (
113+
this.options[/** @type {keyof ChunkGroupOptions} */ (key)] === undefined
114+
) {
115+
this.options[key] =
116+
options[/** @type {keyof ChunkGroupOptions} */ (key)];
117+
} else if (
118+
this.options[/** @type {keyof ChunkGroupOptions} */ (key)] !==
119+
options[/** @type {keyof ChunkGroupOptions} */ (key)]
120+
) {
115121
if (key.endsWith("Order")) {
116122
this.options[key] = Math.max(this.options[key], options[key]);
117123
} else {

lib/CompatibilityPlugin.js

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ const {
1313
const RuntimeGlobals = require("./RuntimeGlobals");
1414
const ConstDependency = require("./dependencies/ConstDependency");
1515

16+
/** @typedef {import("estree").CallExpression} CallExpression */
1617
/** @typedef {import("./Compiler")} Compiler */
18+
/** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
1719
/** @typedef {import("./javascript/JavascriptParser")} JavascriptParser */
20+
/** @typedef {import("./javascript/JavascriptParser").Range} Range */
1821

1922
const nestedWebpackIdentifierTag = Symbol("nested webpack identifier");
2023
const PLUGIN_NAME = "CompatibilityPlugin";
@@ -43,31 +46,41 @@ class CompatibilityPlugin {
4346
)
4447
return;
4548

46-
parser.hooks.call.for("require").tap(PLUGIN_NAME, expr => {
47-
// support for browserify style require delegator: "require(o, !0)"
48-
if (expr.arguments.length !== 2) return;
49-
const second = parser.evaluateExpression(expr.arguments[1]);
50-
if (!second.isBoolean()) return;
51-
if (second.asBool() !== true) return;
52-
const dep = new ConstDependency("require", expr.callee.range);
53-
dep.loc = expr.loc;
54-
if (parser.state.current.dependencies.length > 0) {
55-
const last =
56-
parser.state.current.dependencies[
57-
parser.state.current.dependencies.length - 1
58-
];
59-
if (
60-
last.critical &&
61-
last.options &&
62-
last.options.request === "." &&
63-
last.userRequest === "." &&
64-
last.options.recursive
65-
)
66-
parser.state.current.dependencies.pop();
49+
parser.hooks.call.for("require").tap(
50+
PLUGIN_NAME,
51+
/**
52+
* @param {CallExpression} expr call expression
53+
* @returns {boolean | void} true when need to handle
54+
*/
55+
expr => {
56+
// support for browserify style require delegator: "require(o, !0)"
57+
if (expr.arguments.length !== 2) return;
58+
const second = parser.evaluateExpression(expr.arguments[1]);
59+
if (!second.isBoolean()) return;
60+
if (second.asBool() !== true) return;
61+
const dep = new ConstDependency(
62+
"require",
63+
/** @type {Range} */ (expr.callee.range)
64+
);
65+
dep.loc = /** @type {DependencyLocation} */ (expr.loc);
66+
if (parser.state.current.dependencies.length > 0) {
67+
const last =
68+
parser.state.current.dependencies[
69+
parser.state.current.dependencies.length - 1
70+
];
71+
if (
72+
last.critical &&
73+
last.options &&
74+
last.options.request === "." &&
75+
last.userRequest === "." &&
76+
last.options.recursive
77+
)
78+
parser.state.current.dependencies.pop();
79+
}
80+
parser.state.module.addPresentationalDependency(dep);
81+
return true;
6782
}
68-
parser.state.module.addPresentationalDependency(dep);
69-
return true;
70-
});
83+
);
7184
});
7285

7386
/**
@@ -82,7 +95,9 @@ class CompatibilityPlugin {
8295
statement.id &&
8396
statement.id.name === RuntimeGlobals.require
8497
) {
85-
const newName = `__nested_webpack_require_${statement.range[0]}__`;
98+
const newName = `__nested_webpack_require_${
99+
/** @type {Range} */ (statement.range)[0]
100+
}__`;
86101
parser.tagVariable(
87102
statement.id.name,
88103
nestedWebpackIdentifierTag,
@@ -101,7 +116,9 @@ class CompatibilityPlugin {
101116
parser.hooks.pattern
102117
.for(RuntimeGlobals.require)
103118
.tap(PLUGIN_NAME, pattern => {
104-
const newName = `__nested_webpack_require_${pattern.range[0]}__`;
119+
const newName = `__nested_webpack_require_${
120+
/** @type {Range} */ (pattern.range)[0]
121+
}__`;
105122
parser.tagVariable(pattern.name, nestedWebpackIdentifierTag, {
106123
name: newName,
107124
declaration: {
@@ -135,8 +152,11 @@ class CompatibilityPlugin {
135152
parser.state.module.addPresentationalDependency(dep);
136153
declaration.updated = true;
137154
}
138-
const dep = new ConstDependency(name, expr.range);
139-
dep.loc = expr.loc;
155+
const dep = new ConstDependency(
156+
name,
157+
/** @type {Range} */ (expr.range)
158+
);
159+
dep.loc = /** @type {DependencyLocation} */ (expr.loc);
140160
parser.state.module.addPresentationalDependency(dep);
141161
return true;
142162
});
@@ -145,11 +165,11 @@ class CompatibilityPlugin {
145165
parser.hooks.program.tap(PLUGIN_NAME, (program, comments) => {
146166
if (comments.length === 0) return;
147167
const c = comments[0];
148-
if (c.type === "Line" && c.range[0] === 0) {
168+
if (c.type === "Line" && /** @type {Range} */ (c.range)[0] === 0) {
149169
if (parser.state.source.slice(0, 2).toString() !== "#!") return;
150170
// this is a hashbang comment
151171
const dep = new ConstDependency("//", 0);
152-
dep.loc = c.loc;
172+
dep.loc = /** @type {DependencyLocation} */ (c.loc);
153173
parser.state.module.addPresentationalDependency(dep);
154174
}
155175
});

lib/Compilation.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3208,6 +3208,7 @@ Or do you want to use the entrypoints '${name}' and '${runtime}' independently o
32083208
const { chunkGraph, moduleGraph, dependencyTemplates, runtimeTemplate } =
32093209
this;
32103210
const results = this.codeGenerationResults;
3211+
/** @type {WebpackError[]} */
32113212
const errors = [];
32123213
/** @type {Set<Module> | undefined} */
32133214
let notCodeGeneratedModules = undefined;
@@ -3303,7 +3304,7 @@ Or do you want to use the entrypoints '${name}' and '${runtime}' independently o
33033304
* @param {RuntimeTemplate} runtimeTemplate runtimeTemplate
33043305
* @param {WebpackError[]} errors errors
33053306
* @param {CodeGenerationResults} results results
3306-
* @param {function(WebpackError=, boolean=): void} callback callback
3307+
* @param {function((WebpackError | null)=, boolean=): void} callback callback
33073308
*/
33083309
_codeGenerationModule(
33093310
module,

lib/ContextModule.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const makeSerializable = require("./util/makeSerializable");
6363
* @property {RawChunkGroupOptions=} groupOptions
6464
* @property {string=} typePrefix
6565
* @property {string=} category
66-
* @property {string[][]=} referencedExports exports referenced from modules (won't be mangled)
66+
* @property {(string[][] | null)=} referencedExports exports referenced from modules (won't be mangled)
6767
* @property {string=} layer
6868
*/
6969

lib/ContextReplacementPlugin.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@
88
const ContextElementDependency = require("./dependencies/ContextElementDependency");
99
const { join } = require("./util/fs");
1010

11+
/** @typedef {import("./Compiler")} Compiler */
12+
1113
class ContextReplacementPlugin {
14+
/**
15+
* @param {RegExp} resourceRegExp A regular expression that determines which files will be selected
16+
* @param {TODO=} newContentResource A new resource to replace the match
17+
* @param {TODO=} newContentRecursive If true, all subdirectories are searched for matches
18+
* @param {TODO=} newContentRegExp A regular expression that determines which files will be selected
19+
*/
1220
constructor(
1321
resourceRegExp,
1422
newContentResource,
@@ -49,6 +57,11 @@ class ContextReplacementPlugin {
4957
}
5058
}
5159

60+
/**
61+
* Apply the plugin
62+
* @param {Compiler} compiler the compiler instance
63+
* @returns {void}
64+
*/
5265
apply(compiler) {
5366
const resourceRegExp = this.resourceRegExp;
5467
const newContentCallback = this.newContentCallback;

lib/DelegatedModule.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ const RUNTIME_REQUIREMENTS = new Set([
4242
]);
4343

4444
class DelegatedModule extends Module {
45+
/**
46+
* @param {string} sourceRequest source request
47+
* @param {TODO} data data
48+
* @param {"require" | "object"} type type
49+
* @param {string} userRequest user request
50+
* @param {string | Module} originalRequest original request
51+
*/
4552
constructor(sourceRequest, data, type, userRequest, originalRequest) {
4653
super(JAVASCRIPT_MODULE_TYPE_DYNAMIC, null);
4754

@@ -51,7 +58,7 @@ class DelegatedModule extends Module {
5158
this.delegationType = type;
5259
this.userRequest = userRequest;
5360
this.originalRequest = originalRequest;
54-
/** @type {ManifestModuleData} */
61+
/** @type {ManifestModuleData | undefined} */
5562
this.delegateData = data;
5663

5764
// Build info
@@ -110,15 +117,16 @@ class DelegatedModule extends Module {
110117
* @returns {void}
111118
*/
112119
build(options, compilation, resolver, fs, callback) {
113-
this.buildMeta = { ...this.delegateData.buildMeta };
120+
const delegateData = /** @type {ManifestModuleData} */ (this.delegateData);
121+
this.buildMeta = { ...delegateData.buildMeta };
114122
this.buildInfo = {};
115123
this.dependencies.length = 0;
116124
this.delegatedSourceDependency = new DelegatedSourceDependency(
117125
this.sourceRequest
118126
);
119127
this.addDependency(this.delegatedSourceDependency);
120128
this.addDependency(
121-
new StaticExportsDependency(this.delegateData.exports || true, false)
129+
new StaticExportsDependency(delegateData.exports || true, false)
122130
);
123131
callback();
124132
}
@@ -202,6 +210,10 @@ class DelegatedModule extends Module {
202210
super.serialize(context);
203211
}
204212

213+
/**
214+
* @param {ObjectDeserializerContext} context context\
215+
* @returns {DelegatedModule} DelegatedModule
216+
*/
205217
static deserialize(context) {
206218
const { read } = context;
207219
const obj = new DelegatedModule(

lib/DelegatedModuleFactoryPlugin.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
const DelegatedModule = require("./DelegatedModule");
99

10+
/** @typedef {import("./NormalModuleFactory")} NormalModuleFactory */
11+
1012
// options.source
1113
// options.type
1214
// options.context
@@ -20,6 +22,10 @@ class DelegatedModuleFactoryPlugin {
2022
options.extensions = options.extensions || ["", ".js", ".json", ".wasm"];
2123
}
2224

25+
/**
26+
* @param {NormalModuleFactory} normalModuleFactory the normal module factory
27+
* @returns {void}
28+
*/
2329
apply(normalModuleFactory) {
2430
const scope = this.options.scope;
2531
if (scope) {

0 commit comments

Comments
 (0)