Skip to content

Commit

Permalink
refactor(types): more
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jun 21, 2023
1 parent a911bd9 commit e226101
Show file tree
Hide file tree
Showing 14 changed files with 116 additions and 52 deletions.
2 changes: 1 addition & 1 deletion lib/ContextModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const makeSerializable = require("./util/makeSerializable");
* @property {RawChunkGroupOptions=} groupOptions
* @property {string=} typePrefix
* @property {string=} category
* @property {string[][]=} referencedExports exports referenced from modules (won't be mangled)
* @property {(string[][] | null)=} referencedExports exports referenced from modules (won't be mangled)
* @property {string=} layer
*/

Expand Down
2 changes: 1 addition & 1 deletion lib/Entrypoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class Entrypoint extends ChunkGroup {
* @returns {Chunk} chunk
*/
getEntrypointChunk() {
return this._entrypointChunk;
return /** @type {Chunk} */ (this._entrypointChunk);
}

/**
Expand Down
38 changes: 34 additions & 4 deletions lib/dependencies/AMDDefineDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ AMDDefineDependency.Template = class AMDDefineDependencyTemplate extends (
this.replace(dep, source, definition, content);
}

/**
* @param {AMDDefineDependency} dependency dependency
* @returns {string} variable name
*/
localModuleVar(dependency) {
return (
dependency.localModule &&
Expand All @@ -188,6 +192,10 @@ AMDDefineDependency.Template = class AMDDefineDependencyTemplate extends (
);
}

/**
* @param {AMDDefineDependency} dependency dependency
* @returns {string} branch
*/
branch(dependency) {
const localModuleVar = this.localModuleVar(dependency) ? "l" : "";
const arrayRange = dependency.arrayRange ? "a" : "";
Expand All @@ -196,6 +204,12 @@ AMDDefineDependency.Template = class AMDDefineDependencyTemplate extends (
return localModuleVar + arrayRange + objectRange + functionRange;
}

/**
* @param {AMDDefineDependency} dependency dependency
* @param {ReplaceSource} source source
* @param {string} definition definition
* @param {string} text text
*/
replace(dependency, source, definition, text) {
const localModuleVar = this.localModuleVar(dependency);
if (localModuleVar) {
Expand All @@ -216,18 +230,34 @@ AMDDefineDependency.Template = class AMDDefineDependencyTemplate extends (

let current = dependency.range[0];
if (dependency.arrayRange) {
source.replace(current, dependency.arrayRange[0] - 1, texts.shift());
source.replace(
current,
dependency.arrayRange[0] - 1,
/** @type {string} */ (texts.shift())
);
current = dependency.arrayRange[1];
}

if (dependency.objectRange) {
source.replace(current, dependency.objectRange[0] - 1, texts.shift());
source.replace(
current,
dependency.objectRange[0] - 1,
/** @type {string} */ (texts.shift())
);
current = dependency.objectRange[1];
} else if (dependency.functionRange) {
source.replace(current, dependency.functionRange[0] - 1, texts.shift());
source.replace(
current,
dependency.functionRange[0] - 1,
/** @type {string} */ (texts.shift())
);
current = dependency.functionRange[1];
}
source.replace(current, dependency.range[1] - 1, texts.shift());
source.replace(
current,
dependency.range[1] - 1,
/** @type {string} */ (texts.shift())
);
if (texts.length > 0) throw new Error("Implementation error");
}
};
Expand Down
5 changes: 5 additions & 0 deletions lib/dependencies/AMDDefineDependencyParserPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ const DynamicExports = require("./DynamicExports");
const LocalModuleDependency = require("./LocalModuleDependency");
const { addLocalModule, getLocalModule } = require("./LocalModulesHelpers");

/** @typedef {import("estree").CallExpression} CallExpression */
/** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */

/**
* @param {CallExpression} expr expression
* @returns {boolean} true if it's a bound function expression
*/
const isBoundFunctionExpression = expr => {
if (expr.type !== "CallExpression") return false;
if (expr.callee.type !== "MemberExpression") return false;
Expand Down
12 changes: 8 additions & 4 deletions lib/dependencies/CommonJsPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const CommonJsExportRequireDependency = require("./CommonJsExportRequireDependen
/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
/** @typedef {import("../Compilation")} Compilation */
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
/** @typedef {import("../Module").BuildInfo} BuildInfo */
/** @typedef {import("../javascript/JavascriptParser")} Parser */

const PLUGIN_NAME = "CommonJsPlugin";
Expand Down Expand Up @@ -200,25 +202,27 @@ class CommonJsPlugin {
parser.hooks.expression
.for(RuntimeGlobals.moduleLoaded)
.tap(PLUGIN_NAME, expr => {
parser.state.module.buildInfo.moduleConcatenationBailout =
/** @type {BuildInfo} */
(parser.state.module.buildInfo).moduleConcatenationBailout =
RuntimeGlobals.moduleLoaded;
const dep = new RuntimeRequirementsDependency([
RuntimeGlobals.moduleLoaded
]);
dep.loc = expr.loc;
dep.loc = /** @type {DependencyLocation} */ (expr.loc);
parser.state.module.addPresentationalDependency(dep);
return true;
});

parser.hooks.expression
.for(RuntimeGlobals.moduleId)
.tap(PLUGIN_NAME, expr => {
parser.state.module.buildInfo.moduleConcatenationBailout =
/** @type {BuildInfo} */
(parser.state.module.buildInfo).moduleConcatenationBailout =
RuntimeGlobals.moduleId;
const dep = new RuntimeRequirementsDependency([
RuntimeGlobals.moduleId
]);
dep.loc = expr.loc;
dep.loc = /** @type {DependencyLocation} */ (expr.loc);
parser.state.module.addPresentationalDependency(dep);
return true;
});
Expand Down
2 changes: 2 additions & 0 deletions lib/dependencies/HarmonyExportDependencyParserPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const {
} = require("./HarmonyImportDependencyParserPlugin");
const HarmonyImportSideEffectDependency = require("./HarmonyImportSideEffectDependency");

/** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */

const { HarmonyStarExportsList } = HarmonyExportImportedSpecifierDependency;

module.exports = class HarmonyExportDependencyParserPlugin {
Expand Down
4 changes: 2 additions & 2 deletions lib/dependencies/HarmonyExportImportedSpecifierDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,9 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
* @param {string[]} ids the requested export name of the imported module
* @param {string | null} name the export name of for this module
* @param {Set<string>} activeExports other named exports in the module
* @param {ReadonlyArray<HarmonyExportImportedSpecifierDependency> | Iterable<HarmonyExportImportedSpecifierDependency>} otherStarExports other star exports in the module before this import
* @param {ReadonlyArray<HarmonyExportImportedSpecifierDependency> | Iterable<HarmonyExportImportedSpecifierDependency> | null} otherStarExports other star exports in the module before this import
* @param {number} exportPresenceMode mode of checking export names
* @param {HarmonyStarExportsList} allStarExports all star exports in the module
* @param {HarmonyStarExportsList | null} allStarExports all star exports in the module
* @param {Assertions=} assertions import assertions
*/
constructor(
Expand Down
3 changes: 1 addition & 2 deletions lib/dependencies/HarmonyImportSideEffectDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const HarmonyImportDependency = require("./HarmonyImportDependency");
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
/** @typedef {import("../Dependency")} Dependency */
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
/** @typedef {import("../InitFragment")} InitFragment */
/** @typedef {import("../Module")} Module */
/** @typedef {import("../ModuleGraph")} ModuleGraph */
/** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */
Expand Down Expand Up @@ -74,7 +73,7 @@ HarmonyImportSideEffectDependency.Template = class HarmonyImportSideEffectDepend
apply(dependency, source, templateContext) {
const { moduleGraph, concatenationScope } = templateContext;
if (concatenationScope) {
const module = moduleGraph.getModule(dependency);
const module = /** @type {Module} */ (moduleGraph.getModule(dependency));
if (concatenationScope.isModuleInScope(module)) {
return;
}
Expand Down
23 changes: 16 additions & 7 deletions lib/dependencies/HarmonyImportSpecifierDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const HarmonyImportDependency = require("./HarmonyImportDependency");
/** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
/** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
/** @typedef {import("../Module")} Module */
/** @typedef {import("../Module").BuildMeta} BuildMeta */
/** @typedef {import("../ModuleGraph")} ModuleGraph */
/** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */
/** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */
Expand Down Expand Up @@ -66,9 +68,9 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
this.directImport = undefined;
this.shorthand = undefined;
this.asiSafe = undefined;
/** @type {Set<string> | boolean} */
/** @type {Set<string> | boolean | undefined} */
this.usedByExports = undefined;
/** @type {Set<string>} */
/** @type {Set<string> | undefined} */
this.referencedPropertiesInDestructuring = undefined;
}

Expand Down Expand Up @@ -143,11 +145,14 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
let namespaceObjectAsContext = this.namespaceObjectAsContext;
if (ids[0] === "default") {
const selfModule = moduleGraph.getParentModule(this);
const importedModule = moduleGraph.getModule(this);
const importedModule =
/** @type {Module} */
(moduleGraph.getModule(this));
switch (
importedModule.getExportsType(
moduleGraph,
selfModule.buildMeta.strictHarmonyModule
/** @type {BuildMeta} */
(selfModule.buildMeta).strictHarmonyModule
)
) {
case "default-only":
Expand Down Expand Up @@ -201,7 +206,10 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
_getEffectiveExportPresenceLevel(moduleGraph) {
if (this.exportPresenceMode !== ExportPresenceModes.AUTO)
return this.exportPresenceMode;
return moduleGraph.getParentModule(this).buildMeta.strictHarmonyModule
const buildMeta = /** @type {BuildMeta} */ (
moduleGraph.getParentModule(this).buildMeta
);
return buildMeta.strictHarmonyModule
? ExportPresenceModes.ERROR
: ExportPresenceModes.WARN;
}
Expand Down Expand Up @@ -362,9 +370,10 @@ HarmonyImportSpecifierDependency.Template = class HarmonyImportSpecifierDependen
* @returns {string[]} generated code
*/
_trimIdsToThoseImported(ids, moduleGraph, dependency) {
/** @type {string[]} */
let trimmedIds = [];
const exportsInfo = moduleGraph.getExportsInfo(
moduleGraph.getModule(dependency)
/** @type {Module} */ (moduleGraph.getModule(dependency))
);
let currentExportsInfo = /** @type {ExportsInfo=} */ exportsInfo;
for (let i = 0; i < ids.length; i++) {
Expand Down Expand Up @@ -437,7 +446,7 @@ HarmonyImportSpecifierDependency.Template = class HarmonyImportSpecifierDependen

exportExpr = runtimeTemplate.exportFromImport({
moduleGraph,
module: moduleGraph.getModule(dep),
module: /** @type {Module} */ (moduleGraph.getModule(dep)),
request: dep.request,
exportName: ids,
originModule: module,
Expand Down
8 changes: 5 additions & 3 deletions lib/dependencies/ImportDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const ModuleDependency = require("./ModuleDependency");
/** @typedef {import("../AsyncDependenciesBlock")} AsyncDependenciesBlock */
/** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
/** @typedef {import("../Module")} Module */
/** @typedef {import("../Module").BuildMeta} BuildMeta */
/** @typedef {import("../ModuleGraph")} ModuleGraph */
/** @typedef {import("../javascript/JavascriptParser").Range} Range */
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
Expand All @@ -23,7 +25,7 @@ class ImportDependency extends ModuleDependency {
/**
* @param {string} request the request
* @param {Range} range expression range
* @param {string[][]=} referencedExports list of referenced exports
* @param {(string[][] | null)=} referencedExports list of referenced exports
*/
constructor(request, range, referencedExports) {
super(request);
Expand Down Expand Up @@ -96,9 +98,9 @@ ImportDependency.Template = class ImportDependencyTemplate extends (
const content = runtimeTemplate.moduleNamespacePromise({
chunkGraph,
block: block,
module: moduleGraph.getModule(dep),
module: /** @type {Module} */ (moduleGraph.getModule(dep)),
request: dep.request,
strict: module.buildMeta.strictHarmonyModule,
strict: /** @type {BuildMeta} */ (module.buildMeta).strictHarmonyModule,
message: "import()",
runtimeRequirements
});
Expand Down
2 changes: 1 addition & 1 deletion lib/dependencies/ImportEagerDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ImportEagerDependency extends ImportDependency {
/**
* @param {string} request the request
* @param {Range} range expression range
* @param {string[][]=} referencedExports list of referenced exports
* @param {(string[][] | null)=} referencedExports list of referenced exports
*/
constructor(request, range, referencedExports) {
super(request, range, referencedExports);
Expand Down
Loading

0 comments on commit e226101

Please sign in to comment.