Skip to content

Commit

Permalink
Merge pull request #17365 from webpack/refactor-types
Browse files Browse the repository at this point in the history
refactor(types): more
  • Loading branch information
TheLarkInn committed Jun 14, 2023
2 parents fc703e2 + 5efc30a commit a2a0e01
Show file tree
Hide file tree
Showing 83 changed files with 1,052 additions and 474 deletions.
4 changes: 3 additions & 1 deletion lib/CaseSensitiveModulesWarning.js
Expand Up @@ -42,7 +42,9 @@ const createModulesListMessage = (modules, moduleGraph) => {

if (validReasons.length > 0) {
message += `\n Used by ${validReasons.length} module(s), i. e.`;
message += `\n ${validReasons[0].identifier()}`;
message += `\n ${
/** @type {Module[]} */ (validReasons)[0].identifier()
}`;
}
return message;
})
Expand Down
4 changes: 2 additions & 2 deletions lib/ConcatenationScope.js
Expand Up @@ -108,7 +108,7 @@ class ConcatenationScope {
module,
{ ids = undefined, call = false, directImport = false, asiSafe = false }
) {
const info = this._modulesMap.get(module);
const info = /** @type {ModuleInfo} */ (this._modulesMap.get(module));
const callFlag = call ? "_call" : "";
const directImportFlag = directImport ? "_directImport" : "";
const asiSafeFlag = asiSafe
Expand All @@ -133,7 +133,7 @@ class ConcatenationScope {

/**
* @param {string} name the identifier
* @returns {ModuleReferenceOptions & { index: number }} parsed options and index
* @returns {ModuleReferenceOptions & { index: number } | null} parsed options and index
*/
static matchModuleReference(name) {
const match = MODULE_REFERENCE_REGEXP.exec(name);
Expand Down
12 changes: 11 additions & 1 deletion lib/ConditionalInitFragment.js
Expand Up @@ -14,6 +14,11 @@ const { mergeRuntime } = require("./util/runtime");
/** @typedef {import("./Generator").GenerateContext} GenerateContext */
/** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */

/**
* @param {string} condition condition
* @param {string | Source} source source
* @returns {string | Source} wrapped source
*/
const wrapInCondition = (condition, source) => {
if (typeof source === "string") {
return Template.asString([
Expand All @@ -33,13 +38,14 @@ const wrapInCondition = (condition, source) => {

/**
* @typedef {GenerateContext} Context
* @extends {InitFragment<Context>}
*/
class ConditionalInitFragment extends InitFragment {
/**
* @param {string|Source} content the source code that will be included as initialization code
* @param {number} stage category of initialization code (contribute to order)
* @param {number} position position in the category (contribute to order)
* @param {string} key unique key to avoid emitting the same initialization code twice
* @param {string | undefined} key unique key to avoid emitting the same initialization code twice
* @param {RuntimeSpec | boolean} runtimeCondition in which runtime this fragment should be executed
* @param {string|Source=} endContent the source code that will be included at the end of the module
*/
Expand Down Expand Up @@ -89,6 +95,10 @@ class ConditionalInitFragment extends InitFragment {
return wrapInCondition(expr, this.endContent);
}

/**
* @param {ConditionalInitFragment} other fragment to merge with
* @returns {ConditionalInitFragment} merged fragment
*/
merge(other) {
if (this.runtimeCondition === true) return this;
if (other.runtimeCondition === true) return other;
Expand Down
86 changes: 57 additions & 29 deletions lib/ConstPlugin.js
Expand Up @@ -15,9 +15,14 @@ const ConstDependency = require("./dependencies/ConstDependency");
const { evaluateToString } = require("./javascript/JavascriptParserHelpers");
const { parseResource } = require("./util/identifier");

/** @typedef {import("estree").Expression} ExpressionNode */
/** @typedef {import("estree").Super} SuperNode */
/** @typedef {import("estree").Expression} Expression */
/** @typedef {import("estree").SourceLocation} SourceLocation */
/** @typedef {import("estree").Statement} Statement */
/** @typedef {import("estree").Super} Super */
/** @typedef {import("./Compiler")} Compiler */
/** @typedef {import("./javascript/BasicEvaluatedExpression")} BasicEvaluatedExpression */
/** @typedef {import("./javascript/JavascriptParser")} JavascriptParser */
/** @typedef {import("./javascript/JavascriptParser").Range} Range */

const collectDeclaration = (declarations, pattern) => {
const stack = [pattern];
Expand Down Expand Up @@ -136,15 +141,21 @@ class ConstPlugin {
new CachedConstDependency.Template()
);

/**
* @param {JavascriptParser} parser the parser
*/
const handler = parser => {
parser.hooks.statementIf.tap(PLUGIN_NAME, statement => {
if (parser.scope.isAsmJs) return;
const param = parser.evaluateExpression(statement.test);
const bool = param.asBool();
if (typeof bool === "boolean") {
if (!param.couldHaveSideEffects()) {
const dep = new ConstDependency(`${bool}`, param.range);
dep.loc = statement.loc;
const dep = new ConstDependency(
`${bool}`,
/** @type {Range} */ (param.range)
);
dep.loc = /** @type {SourceLocation} */ (statement.loc);
parser.state.module.addPresentationalDependency(dep);
} else {
parser.walkExpression(statement.test);
Expand Down Expand Up @@ -200,9 +211,9 @@ class ConstPlugin {
}
const dep = new ConstDependency(
replacement,
branchToRemove.range
/** @type {Range} */ (branchToRemove.range)
);
dep.loc = branchToRemove.loc;
dep.loc = /** @type {SourceLocation} */ (branchToRemove.loc);
parser.state.module.addPresentationalDependency(dep);
}
return bool;
Expand All @@ -216,8 +227,11 @@ class ConstPlugin {
const bool = param.asBool();
if (typeof bool === "boolean") {
if (!param.couldHaveSideEffects()) {
const dep = new ConstDependency(` ${bool}`, param.range);
dep.loc = expression.loc;
const dep = new ConstDependency(
` ${bool}`,
/** @type {Range} */ (param.range)
);
dep.loc = /** @type {SourceLocation} */ (expression.loc);
parser.state.module.addPresentationalDependency(dep);
} else {
parser.walkExpression(expression.test);
Expand All @@ -236,8 +250,11 @@ class ConstPlugin {
const branchToRemove = bool
? expression.alternate
: expression.consequent;
const dep = new ConstDependency("0", branchToRemove.range);
dep.loc = branchToRemove.loc;
const dep = new ConstDependency(
"0",
/** @type {Range} */ (branchToRemove.range)
);
dep.loc = /** @type {SourceLocation} */ (branchToRemove.loc);
parser.state.module.addPresentationalDependency(dep);
return bool;
}
Expand Down Expand Up @@ -313,18 +330,21 @@ class ConstPlugin {
//
// returnfalse&&'foo'
//
const dep = new ConstDependency(` ${bool}`, param.range);
dep.loc = expression.loc;
const dep = new ConstDependency(
` ${bool}`,
/** @type {Range} */ (param.range)
);
dep.loc = /** @type {SourceLocation} */ (expression.loc);
parser.state.module.addPresentationalDependency(dep);
} else {
parser.walkExpression(expression.left);
}
if (!keepRight) {
const dep = new ConstDependency(
"0",
expression.right.range
/** @type {Range} */ (expression.right.range)
);
dep.loc = expression.loc;
dep.loc = /** @type {SourceLocation} */ (expression.loc);
parser.state.module.addPresentationalDependency(dep);
}
return keepRight;
Expand Down Expand Up @@ -363,15 +383,18 @@ class ConstPlugin {
//
// returnnull??'foo'
//
const dep = new ConstDependency(" null", param.range);
dep.loc = expression.loc;
const dep = new ConstDependency(
" null",
/** @type {Range} */ (param.range)
);
dep.loc = /** @type {SourceLocation} */ (expression.loc);
parser.state.module.addPresentationalDependency(dep);
} else {
const dep = new ConstDependency(
"0",
expression.right.range
/** @type {Range} */ (expression.right.range)
);
dep.loc = expression.loc;
dep.loc = /** @type {SourceLocation} */ (expression.loc);
parser.state.module.addPresentationalDependency(dep);
parser.walkExpression(expression.left);
}
Expand All @@ -382,9 +405,9 @@ class ConstPlugin {
}
);
parser.hooks.optionalChaining.tap(PLUGIN_NAME, expr => {
/** @type {ExpressionNode[]} */
/** @type {Expression[]} */
const optionalExpressionsStack = [];
/** @type {ExpressionNode|SuperNode} */
/** @type {Expression | Super} */
let next = expr.expression;

while (
Expand All @@ -395,15 +418,15 @@ class ConstPlugin {
if (next.optional) {
// SuperNode can not be optional
optionalExpressionsStack.push(
/** @type {ExpressionNode} */ (next.object)
/** @type {Expression} */ (next.object)
);
}
next = next.object;
} else {
if (next.optional) {
// SuperNode can not be optional
optionalExpressionsStack.push(
/** @type {ExpressionNode} */ (next.callee)
/** @type {Expression} */ (next.callee)
);
}
next = next.callee;
Expand All @@ -412,7 +435,9 @@ class ConstPlugin {

while (optionalExpressionsStack.length) {
const expression = optionalExpressionsStack.pop();
const evaluated = parser.evaluateExpression(expression);
const evaluated = parser.evaluateExpression(
/** @type {Expression} */ (expression)
);

if (evaluated.asNullish()) {
// ------------------------------------------
Expand All @@ -427,8 +452,11 @@ class ConstPlugin {
//
// ------------------------------------------
//
const dep = new ConstDependency(" undefined", expr.range);
dep.loc = expr.loc;
const dep = new ConstDependency(
" undefined",
/** @type {Range} */ (expr.range)
);
dep.loc = /** @type {SourceLocation} */ (expr.loc);
parser.state.module.addPresentationalDependency(dep);
return true;
}
Expand All @@ -452,10 +480,10 @@ class ConstPlugin {
JSON.stringify(
cachedParseResource(parser.state.module.resource).query
),
expr.range,
/** @type {Range} */ (expr.range),
"__resourceQuery"
);
dep.loc = expr.loc;
dep.loc = /** @type {SourceLocation} */ (expr.loc);
parser.state.module.addPresentationalDependency(dep);
return true;
});
Expand All @@ -478,10 +506,10 @@ class ConstPlugin {
JSON.stringify(
cachedParseResource(parser.state.module.resource).fragment
),
expr.range,
/** @type {Range} */ (expr.range),
"__resourceFragment"
);
dep.loc = expr.loc;
dep.loc = /** @type {SourceLocation} */ (expr.loc);
parser.state.module.addPresentationalDependency(dep);
return true;
});
Expand Down
4 changes: 4 additions & 0 deletions lib/FlagEntryExportAsUsedPlugin.js
Expand Up @@ -12,6 +12,10 @@ const { getEntryRuntime } = require("./util/runtime");
const PLUGIN_NAME = "FlagEntryExportAsUsedPlugin";

class FlagEntryExportAsUsedPlugin {
/**
* @param {boolean} nsObjectUsed true, if the ns object is used
* @param {string} explanation explanation for the reason
*/
constructor(nsObjectUsed, explanation) {
this.nsObjectUsed = nsObjectUsed;
this.explanation = explanation;
Expand Down
6 changes: 4 additions & 2 deletions lib/ModuleDependencyError.js
Expand Up @@ -23,7 +23,7 @@ class ModuleDependencyError extends WebpackError {
this.name = "ModuleDependencyError";
this.details =
err && !(/** @type {any} */ (err).hideStack)
? err.stack.split("\n").slice(1).join("\n")
? /** @type {string} */ (err.stack).split("\n").slice(1).join("\n")
: undefined;
this.module = module;
this.loc = loc;
Expand All @@ -32,7 +32,9 @@ class ModuleDependencyError extends WebpackError {

if (err && /** @type {any} */ (err).hideStack) {
this.stack =
err.stack.split("\n").slice(1).join("\n") + "\n\n" + this.stack;
/** @type {string} */ (err.stack).split("\n").slice(1).join("\n") +
"\n\n" +
this.stack;
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions lib/ModuleDependencyWarning.js
Expand Up @@ -23,7 +23,7 @@ class ModuleDependencyWarning extends WebpackError {
this.name = "ModuleDependencyWarning";
this.details =
err && !(/** @type {any} */ (err).hideStack)
? err.stack.split("\n").slice(1).join("\n")
? /** @type {string} */ (err.stack).split("\n").slice(1).join("\n")
: undefined;
this.module = module;
this.loc = loc;
Expand All @@ -32,7 +32,9 @@ class ModuleDependencyWarning extends WebpackError {

if (err && /** @type {any} */ (err).hideStack) {
this.stack =
err.stack.split("\n").slice(1).join("\n") + "\n\n" + this.stack;
/** @type {string} */ (err.stack).split("\n").slice(1).join("\n") +
"\n\n" +
this.stack;
}
}
}
Expand Down

0 comments on commit a2a0e01

Please sign in to comment.