Skip to content

Commit 9894226

Browse files
refactor(types): more
1 parent 568f28b commit 9894226

61 files changed

Lines changed: 841 additions & 399 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/CaseSensitiveModulesWarning.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ const createModulesListMessage = (modules, moduleGraph) => {
4242

4343
if (validReasons.length > 0) {
4444
message += `\n Used by ${validReasons.length} module(s), i. e.`;
45-
message += `\n ${validReasons[0].identifier()}`;
45+
message += `\n ${
46+
/** @type {Module[]} */ (validReasons)[0].identifier()
47+
}`;
4648
}
4749
return message;
4850
})

‎lib/ConcatenationScope.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class ConcatenationScope {
108108
module,
109109
{ ids = undefined, call = false, directImport = false, asiSafe = false }
110110
) {
111-
const info = this._modulesMap.get(module);
111+
const info = /** @type {ModuleInfo} */ (this._modulesMap.get(module));
112112
const callFlag = call ? "_call" : "";
113113
const directImportFlag = directImport ? "_directImport" : "";
114114
const asiSafeFlag = asiSafe
@@ -133,7 +133,7 @@ class ConcatenationScope {
133133

134134
/**
135135
* @param {string} name the identifier
136-
* @returns {ModuleReferenceOptions & { index: number }} parsed options and index
136+
* @returns {ModuleReferenceOptions & { index: number } | null} parsed options and index
137137
*/
138138
static matchModuleReference(name) {
139139
const match = MODULE_REFERENCE_REGEXP.exec(name);

‎lib/ConditionalInitFragment.js‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ const { mergeRuntime } = require("./util/runtime");
1414
/** @typedef {import("./Generator").GenerateContext} GenerateContext */
1515
/** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
1616

17+
/**
18+
* @param {string} condition condition
19+
* @param {string | Source} source source
20+
* @returns {string | Source} wrapped source
21+
*/
1722
const wrapInCondition = (condition, source) => {
1823
if (typeof source === "string") {
1924
return Template.asString([
@@ -33,13 +38,14 @@ const wrapInCondition = (condition, source) => {
3338

3439
/**
3540
* @typedef {GenerateContext} Context
41+
* @extends {InitFragment<Context>}
3642
*/
3743
class ConditionalInitFragment extends InitFragment {
3844
/**
3945
* @param {string|Source} content the source code that will be included as initialization code
4046
* @param {number} stage category of initialization code (contribute to order)
4147
* @param {number} position position in the category (contribute to order)
42-
* @param {string} key unique key to avoid emitting the same initialization code twice
48+
* @param {string | undefined} key unique key to avoid emitting the same initialization code twice
4349
* @param {RuntimeSpec | boolean} runtimeCondition in which runtime this fragment should be executed
4450
* @param {string|Source=} endContent the source code that will be included at the end of the module
4551
*/
@@ -89,6 +95,10 @@ class ConditionalInitFragment extends InitFragment {
8995
return wrapInCondition(expr, this.endContent);
9096
}
9197

98+
/**
99+
* @param {ConditionalInitFragment} other fragment to merge with
100+
* @returns {ConditionalInitFragment} merged fragment
101+
*/
92102
merge(other) {
93103
if (this.runtimeCondition === true) return this;
94104
if (other.runtimeCondition === true) return other;

‎lib/ConstPlugin.js‎

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

18-
/** @typedef {import("estree").Expression} ExpressionNode */
19-
/** @typedef {import("estree").Super} SuperNode */
18+
/** @typedef {import("estree").Expression} Expression */
19+
/** @typedef {import("estree").SourceLocation} SourceLocation */
20+
/** @typedef {import("estree").Statement} Statement */
21+
/** @typedef {import("estree").Super} Super */
2022
/** @typedef {import("./Compiler")} Compiler */
23+
/** @typedef {import("./javascript/BasicEvaluatedExpression")} BasicEvaluatedExpression */
24+
/** @typedef {import("./javascript/JavascriptParser")} JavascriptParser */
25+
/** @typedef {import("./javascript/JavascriptParser").Range} Range */
2126

2227
const collectDeclaration = (declarations, pattern) => {
2328
const stack = [pattern];
@@ -136,15 +141,21 @@ class ConstPlugin {
136141
new CachedConstDependency.Template()
137142
);
138143

144+
/**
145+
* @param {JavascriptParser} parser the parser
146+
*/
139147
const handler = parser => {
140148
parser.hooks.statementIf.tap(PLUGIN_NAME, statement => {
141149
if (parser.scope.isAsmJs) return;
142150
const param = parser.evaluateExpression(statement.test);
143151
const bool = param.asBool();
144152
if (typeof bool === "boolean") {
145153
if (!param.couldHaveSideEffects()) {
146-
const dep = new ConstDependency(`${bool}`, param.range);
147-
dep.loc = statement.loc;
154+
const dep = new ConstDependency(
155+
`${bool}`,
156+
/** @type {Range} */ (param.range)
157+
);
158+
dep.loc = /** @type {SourceLocation} */ (statement.loc);
148159
parser.state.module.addPresentationalDependency(dep);
149160
} else {
150161
parser.walkExpression(statement.test);
@@ -200,9 +211,9 @@ class ConstPlugin {
200211
}
201212
const dep = new ConstDependency(
202213
replacement,
203-
branchToRemove.range
214+
/** @type {Range} */ (branchToRemove.range)
204215
);
205-
dep.loc = branchToRemove.loc;
216+
dep.loc = /** @type {SourceLocation} */ (branchToRemove.loc);
206217
parser.state.module.addPresentationalDependency(dep);
207218
}
208219
return bool;
@@ -216,8 +227,11 @@ class ConstPlugin {
216227
const bool = param.asBool();
217228
if (typeof bool === "boolean") {
218229
if (!param.couldHaveSideEffects()) {
219-
const dep = new ConstDependency(` ${bool}`, param.range);
220-
dep.loc = expression.loc;
230+
const dep = new ConstDependency(
231+
` ${bool}`,
232+
/** @type {Range} */ (param.range)
233+
);
234+
dep.loc = /** @type {SourceLocation} */ (expression.loc);
221235
parser.state.module.addPresentationalDependency(dep);
222236
} else {
223237
parser.walkExpression(expression.test);
@@ -236,8 +250,11 @@ class ConstPlugin {
236250
const branchToRemove = bool
237251
? expression.alternate
238252
: expression.consequent;
239-
const dep = new ConstDependency("0", branchToRemove.range);
240-
dep.loc = branchToRemove.loc;
253+
const dep = new ConstDependency(
254+
"0",
255+
/** @type {Range} */ (branchToRemove.range)
256+
);
257+
dep.loc = /** @type {SourceLocation} */ (branchToRemove.loc);
241258
parser.state.module.addPresentationalDependency(dep);
242259
return bool;
243260
}
@@ -313,18 +330,21 @@ class ConstPlugin {
313330
//
314331
// returnfalse&&'foo'
315332
//
316-
const dep = new ConstDependency(` ${bool}`, param.range);
317-
dep.loc = expression.loc;
333+
const dep = new ConstDependency(
334+
` ${bool}`,
335+
/** @type {Range} */ (param.range)
336+
);
337+
dep.loc = /** @type {SourceLocation} */ (expression.loc);
318338
parser.state.module.addPresentationalDependency(dep);
319339
} else {
320340
parser.walkExpression(expression.left);
321341
}
322342
if (!keepRight) {
323343
const dep = new ConstDependency(
324344
"0",
325-
expression.right.range
345+
/** @type {Range} */ (expression.right.range)
326346
);
327-
dep.loc = expression.loc;
347+
dep.loc = /** @type {SourceLocation} */ (expression.loc);
328348
parser.state.module.addPresentationalDependency(dep);
329349
}
330350
return keepRight;
@@ -363,15 +383,18 @@ class ConstPlugin {
363383
//
364384
// returnnull??'foo'
365385
//
366-
const dep = new ConstDependency(" null", param.range);
367-
dep.loc = expression.loc;
386+
const dep = new ConstDependency(
387+
" null",
388+
/** @type {Range} */ (param.range)
389+
);
390+
dep.loc = /** @type {SourceLocation} */ (expression.loc);
368391
parser.state.module.addPresentationalDependency(dep);
369392
} else {
370393
const dep = new ConstDependency(
371394
"0",
372-
expression.right.range
395+
/** @type {Range} */ (expression.right.range)
373396
);
374-
dep.loc = expression.loc;
397+
dep.loc = /** @type {SourceLocation} */ (expression.loc);
375398
parser.state.module.addPresentationalDependency(dep);
376399
parser.walkExpression(expression.left);
377400
}
@@ -382,9 +405,9 @@ class ConstPlugin {
382405
}
383406
);
384407
parser.hooks.optionalChaining.tap(PLUGIN_NAME, expr => {
385-
/** @type {ExpressionNode[]} */
408+
/** @type {Expression[]} */
386409
const optionalExpressionsStack = [];
387-
/** @type {ExpressionNode|SuperNode} */
410+
/** @type {Expression | Super} */
388411
let next = expr.expression;
389412

390413
while (
@@ -395,15 +418,15 @@ class ConstPlugin {
395418
if (next.optional) {
396419
// SuperNode can not be optional
397420
optionalExpressionsStack.push(
398-
/** @type {ExpressionNode} */ (next.object)
421+
/** @type {Expression} */ (next.object)
399422
);
400423
}
401424
next = next.object;
402425
} else {
403426
if (next.optional) {
404427
// SuperNode can not be optional
405428
optionalExpressionsStack.push(
406-
/** @type {ExpressionNode} */ (next.callee)
429+
/** @type {Expression} */ (next.callee)
407430
);
408431
}
409432
next = next.callee;
@@ -412,7 +435,9 @@ class ConstPlugin {
412435

413436
while (optionalExpressionsStack.length) {
414437
const expression = optionalExpressionsStack.pop();
415-
const evaluated = parser.evaluateExpression(expression);
438+
const evaluated = parser.evaluateExpression(
439+
/** @type {Expression} */ (expression)
440+
);
416441

417442
if (evaluated.asNullish()) {
418443
// ------------------------------------------
@@ -427,8 +452,11 @@ class ConstPlugin {
427452
//
428453
// ------------------------------------------
429454
//
430-
const dep = new ConstDependency(" undefined", expr.range);
431-
dep.loc = expr.loc;
455+
const dep = new ConstDependency(
456+
" undefined",
457+
/** @type {Range} */ (expr.range)
458+
);
459+
dep.loc = /** @type {SourceLocation} */ (expr.loc);
432460
parser.state.module.addPresentationalDependency(dep);
433461
return true;
434462
}
@@ -452,10 +480,10 @@ class ConstPlugin {
452480
JSON.stringify(
453481
cachedParseResource(parser.state.module.resource).query
454482
),
455-
expr.range,
483+
/** @type {Range} */ (expr.range),
456484
"__resourceQuery"
457485
);
458-
dep.loc = expr.loc;
486+
dep.loc = /** @type {SourceLocation} */ (expr.loc);
459487
parser.state.module.addPresentationalDependency(dep);
460488
return true;
461489
});
@@ -478,10 +506,10 @@ class ConstPlugin {
478506
JSON.stringify(
479507
cachedParseResource(parser.state.module.resource).fragment
480508
),
481-
expr.range,
509+
/** @type {Range} */ (expr.range),
482510
"__resourceFragment"
483511
);
484-
dep.loc = expr.loc;
512+
dep.loc = /** @type {SourceLocation} */ (expr.loc);
485513
parser.state.module.addPresentationalDependency(dep);
486514
return true;
487515
});

‎lib/FlagEntryExportAsUsedPlugin.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ const { getEntryRuntime } = require("./util/runtime");
1212
const PLUGIN_NAME = "FlagEntryExportAsUsedPlugin";
1313

1414
class FlagEntryExportAsUsedPlugin {
15+
/**
16+
* @param {boolean} nsObjectUsed true, if the ns object is used
17+
* @param {string} explanation explanation for the reason
18+
*/
1519
constructor(nsObjectUsed, explanation) {
1620
this.nsObjectUsed = nsObjectUsed;
1721
this.explanation = explanation;

‎lib/ModuleDependencyError.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ModuleDependencyError extends WebpackError {
2323
this.name = "ModuleDependencyError";
2424
this.details =
2525
err && !(/** @type {any} */ (err).hideStack)
26-
? err.stack.split("\n").slice(1).join("\n")
26+
? /** @type {string} */ (err.stack).split("\n").slice(1).join("\n")
2727
: undefined;
2828
this.module = module;
2929
this.loc = loc;
@@ -32,7 +32,9 @@ class ModuleDependencyError extends WebpackError {
3232

3333
if (err && /** @type {any} */ (err).hideStack) {
3434
this.stack =
35-
err.stack.split("\n").slice(1).join("\n") + "\n\n" + this.stack;
35+
/** @type {string} */ (err.stack).split("\n").slice(1).join("\n") +
36+
"\n\n" +
37+
this.stack;
3638
}
3739
}
3840
}

‎lib/ModuleDependencyWarning.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ModuleDependencyWarning extends WebpackError {
2323
this.name = "ModuleDependencyWarning";
2424
this.details =
2525
err && !(/** @type {any} */ (err).hideStack)
26-
? err.stack.split("\n").slice(1).join("\n")
26+
? /** @type {string} */ (err.stack).split("\n").slice(1).join("\n")
2727
: undefined;
2828
this.module = module;
2929
this.loc = loc;
@@ -32,7 +32,9 @@ class ModuleDependencyWarning extends WebpackError {
3232

3333
if (err && /** @type {any} */ (err).hideStack) {
3434
this.stack =
35-
err.stack.split("\n").slice(1).join("\n") + "\n\n" + this.stack;
35+
/** @type {string} */ (err.stack).split("\n").slice(1).join("\n") +
36+
"\n\n" +
37+
this.stack;
3638
}
3739
}
3840
}

0 commit comments

Comments
 (0)