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 4809421 commit e381884
Show file tree
Hide file tree
Showing 32 changed files with 197 additions and 74 deletions.
16 changes: 11 additions & 5 deletions lib/Dependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ const getIgnoredModule = memoize(() => {

class Dependency {
constructor() {
/** @type {Module} */
/** @type {Module | undefined} */
this._parentModule = undefined;
/** @type {DependenciesBlock} */
/** @type {DependenciesBlock | undefined} */
this._parentDependenciesBlock = undefined;
/** @type {number} */
this._parentDependenciesBlockIndex = -1;
Expand Down Expand Up @@ -174,6 +174,12 @@ class Dependency {
this._loc = loc;
}

/**
* @param {number} startLine start line
* @param {number} startColumn start column
* @param {number} endLine end line
* @param {number} endColumn end column
*/
setLoc(startLine, startColumn, endLine, endColumn) {
this._locSL = startLine;
this._locSC = startColumn;
Expand Down Expand Up @@ -247,7 +253,7 @@ class Dependency {
/**
* Returns warnings
* @param {ModuleGraph} moduleGraph module graph
* @returns {WebpackError[]} warnings
* @returns {WebpackError[] | null | undefined} warnings
*/
getWarnings(moduleGraph) {
return null;
Expand All @@ -256,7 +262,7 @@ class Dependency {
/**
* Returns errors
* @param {ModuleGraph} moduleGraph module graph
* @returns {WebpackError[]} errors
* @returns {WebpackError[] | null | undefined} errors
*/
getErrors(moduleGraph) {
return null;
Expand Down Expand Up @@ -288,7 +294,7 @@ class Dependency {

/**
* @param {string} context context directory
* @returns {Module} a module
* @returns {Module | null} a module
*/
createIgnoredModule(context) {
return getIgnoredModule();
Expand Down
7 changes: 7 additions & 0 deletions lib/DllEntryPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const DllModuleFactory = require("./DllModuleFactory");
const DllEntryDependency = require("./dependencies/DllEntryDependency");
const EntryDependency = require("./dependencies/EntryDependency");

/** @typedef {import("./Compiler")} Compiler */

class DllEntryPlugin {
/**
* @param {string} context context
Expand All @@ -21,6 +23,11 @@ class DllEntryPlugin {
this.options = options;
}

/**
* Apply the plugin
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
compiler.hooks.compilation.tap(
"DllEntryPlugin",
Expand Down
1 change: 1 addition & 0 deletions lib/ErrorHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const cutOffMultilineMessage = (stack, message) => {
const stackSplitByLines = stack.split("\n");
const messageSplitByLines = message.split("\n");

/** @type {string[]} */
const result = [];

stackSplitByLines.forEach((line, idx) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/Module.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ class Module extends DependenciesBlock {

/**
* @param {ModuleGraph} moduleGraph the module graph
* @param {boolean} strict the importing module is strict
* @param {boolean | undefined} strict the importing module is strict
* @returns {"namespace" | "default-only" | "default-with-named" | "dynamic"} export type
* "namespace": Exports is already a namespace object. namespace = exports.
* "dynamic": Check at runtime if __esModule is set. When set: namespace = { ...exports, default: exports }. When not set: namespace = { default: exports }.
Expand Down
4 changes: 2 additions & 2 deletions lib/RuntimeTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ class RuntimeTemplate {

/**
* @param {Object} options options object
* @param {Module} options.module the module
* @param {Module | null} options.module the module
* @param {ChunkGraph} options.chunkGraph the chunk graph
* @param {string} options.request the request that should be printed as comment
* @param {boolean=} options.weak if the dependency is weak (will create a nice error message)
Expand Down Expand Up @@ -439,7 +439,7 @@ class RuntimeTemplate {

/**
* @param {Object} options options object
* @param {Module} options.module the module
* @param {Module | null} options.module the module
* @param {ChunkGraph} options.chunkGraph the chunk graph
* @param {string} options.request the request that should be printed as comment
* @param {boolean=} options.weak if the dependency is weak (will create a nice error message)
Expand Down
6 changes: 5 additions & 1 deletion lib/dependencies/ContextDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ const getCriticalDependencyWarning = memoize(() =>

/** @typedef {ContextOptions & { request: string }} ContextDependencyOptions */

/**
* @param {RegExp | null | undefined} r regexp
* @returns {string} stringified regexp
*/
const regExpToString = r => (r ? r + "" : "");

class ContextDependency extends Dependency {
Expand Down Expand Up @@ -93,7 +97,7 @@ class ContextDependency extends Dependency {
/**
* Returns warnings
* @param {ModuleGraph} moduleGraph module graph
* @returns {WebpackError[]} warnings
* @returns {WebpackError[] | null | undefined} warnings
*/
getWarnings(moduleGraph) {
let warnings = super.getWarnings(moduleGraph);
Expand Down
2 changes: 1 addition & 1 deletion lib/dependencies/CssImportDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class CssImportDependency extends ModuleDependency {

/**
* @param {string} context context directory
* @returns {Module} a module
* @returns {Module | null} a module
*/
createIgnoredModule(context) {
return null;
Expand Down
5 changes: 5 additions & 0 deletions lib/dependencies/CssLocalIdentifierDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ class CssLocalIdentifierDependency extends NullDependency {
}
}

/**
* @param {string} str string
* @param {string | boolean} omitUnderscore true if you need to omit underscore
* @returns {string} escaped css identifier
*/
const escapeCssIdentifier = (str, omitUnderscore) => {
const escaped = `${str}`.replace(
// cspell:word uffff
Expand Down
6 changes: 3 additions & 3 deletions lib/dependencies/CssUrlDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class CssUrlDependency extends ModuleDependency {

/**
* @param {string} context context directory
* @returns {Module} a module
* @returns {Module | null} a module
*/
createIgnoredModule(context) {
const RawDataUrlModule = getRawDataUrlModule();
Expand Down Expand Up @@ -133,7 +133,7 @@ CssUrlDependency.Template = class CssUrlDependencyTemplate extends (
newValue = cssEscapeString(
runtimeTemplate.assetUrl({
publicPath: "",
module: moduleGraph.getModule(dep),
module: /** @type {Module} */ (moduleGraph.getModule(dep)),
codeGenerationResults
})
);
Expand All @@ -142,7 +142,7 @@ CssUrlDependency.Template = class CssUrlDependencyTemplate extends (
newValue = `url(${cssEscapeString(
runtimeTemplate.assetUrl({
publicPath: "",
module: moduleGraph.getModule(dep),
module: /** @type {Module} */ (moduleGraph.getModule(dep)),
codeGenerationResults
})
)})`;
Expand Down
5 changes: 5 additions & 0 deletions lib/dependencies/DllEntryDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ const makeSerializable = require("../util/makeSerializable");

/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
/** @typedef {import("./EntryDependency")} EntryDependency */

class DllEntryDependency extends Dependency {
/**
* @param {EntryDependency[]} dependencies dependencies
* @param {string} name name
*/
constructor(dependencies, name) {
super();

Expand Down
16 changes: 10 additions & 6 deletions lib/dependencies/DynamicExports.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

"use strict";

/** @typedef {import("../Module").BuildMeta} BuildMeta */
/** @typedef {import("../Parser").ParserState} ParserState */

/** @type {WeakMap<ParserState, boolean>} */
Expand All @@ -18,8 +19,9 @@ exports.bailout = parserState => {
const value = parserStateExportsState.get(parserState);
parserStateExportsState.set(parserState, false);
if (value === true) {
parserState.module.buildMeta.exportsType = undefined;
parserState.module.buildMeta.defaultObject = false;
const buildMeta = /** @type {BuildMeta} */ (parserState.module.buildMeta);
buildMeta.exportsType = undefined;
buildMeta.defaultObject = false;
}
};

Expand All @@ -32,8 +34,9 @@ exports.enable = parserState => {
if (value === false) return;
parserStateExportsState.set(parserState, true);
if (value !== true) {
parserState.module.buildMeta.exportsType = "default";
parserState.module.buildMeta.defaultObject = "redirect";
const buildMeta = /** @type {BuildMeta} */ (parserState.module.buildMeta);
buildMeta.exportsType = "default";
buildMeta.defaultObject = "redirect";
}
};

Expand All @@ -44,7 +47,7 @@ exports.enable = parserState => {
exports.setFlagged = parserState => {
const value = parserStateExportsState.get(parserState);
if (value !== true) return;
const buildMeta = parserState.module.buildMeta;
const buildMeta = /** @type {BuildMeta} */ (parserState.module.buildMeta);
if (buildMeta.exportsType === "dynamic") return;
buildMeta.exportsType = "flagged";
};
Expand All @@ -56,7 +59,8 @@ exports.setFlagged = parserState => {
exports.setDynamic = parserState => {
const value = parserStateExportsState.get(parserState);
if (value !== true) return;
parserState.module.buildMeta.exportsType = "dynamic";
/** @type {BuildMeta} */
(parserState.module.buildMeta).exportsType = "dynamic";
};

/**
Expand Down
18 changes: 14 additions & 4 deletions lib/dependencies/ExportsInfoDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,23 @@ const getProperty = (moduleGraph, module, exportName, property, runtime) => {
switch (property) {
case "canMangle": {
const exportsInfo = moduleGraph.getExportsInfo(module);
const exportInfo = exportsInfo.getExportInfo(exportName);
const exportInfo = exportsInfo.getExportInfo(
/** @type {string} */ (exportName)
);
if (exportInfo) return exportInfo.canMangle;
return exportsInfo.otherExportsInfo.canMangle;
}
case "used":
return (
moduleGraph.getExportsInfo(module).getUsed(exportName, runtime) !==
moduleGraph
.getExportsInfo(module)
.getUsed(/** @type {string} */ (exportName), runtime) !==
UsageState.Unused
);
case "useInfo": {
const state = moduleGraph
.getExportsInfo(module)
.getUsed(exportName, runtime);
.getUsed(/** @type {string} */ (exportName), runtime);
switch (state) {
case UsageState.Used:
case UsageState.OnlyPropertiesUsed:
Expand All @@ -79,7 +83,9 @@ const getProperty = (moduleGraph, module, exportName, property, runtime) => {
}
}
case "provideInfo":
return moduleGraph.getExportsInfo(module).isExportProvided(exportName);
return moduleGraph
.getExportsInfo(module)
.isExportProvided(/** @type {string} */ (exportName));
}
return undefined;
};
Expand Down Expand Up @@ -108,6 +114,10 @@ class ExportsInfoDependency extends NullDependency {
super.serialize(context);
}

/**
* @param {ObjectDeserializerContext} context context
* @returns {ExportsInfoDependency} ExportsInfoDependency
*/
static deserialize(context) {
const obj = new ExportsInfoDependency(
context.read(),
Expand Down
3 changes: 2 additions & 1 deletion lib/dependencies/HarmonyCompatibilityDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const NullDependency = require("./NullDependency");
/** @typedef {import("../Dependency")} Dependency */
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
/** @typedef {import("../Module")} Module */
/** @typedef {import("../Module").BuildMeta} BuildMeta */

class HarmonyCompatibilityDependency extends NullDependency {
get type() {
Expand Down Expand Up @@ -80,7 +81,7 @@ HarmonyCompatibilityDependency.Template = class HarmonyExportDependencyTemplate
0,
undefined,
`\n__webpack_async_result__();\n} catch(e) { __webpack_async_result__(e); } }${
module.buildMeta.async ? ", 1" : ""
/** @type {BuildMeta} */ (module.buildMeta).async ? ", 1" : ""
});`
)
);
Expand Down
4 changes: 3 additions & 1 deletion lib/dependencies/HarmonyDetectionParserPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const DynamicExports = require("./DynamicExports");
const HarmonyCompatibilityDependency = require("./HarmonyCompatibilityDependency");
const HarmonyExports = require("./HarmonyExports");

/** @typedef {import("../Module").BuildMeta} BuildMeta */
/** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
/** @typedef {import("./HarmonyModulesPlugin").HarmonyModulesPluginOptions} HarmonyModulesPluginOptions */

Expand Down Expand Up @@ -72,7 +73,8 @@ module.exports = class HarmonyDetectionParserPlugin {
"Top-level-await is only supported in EcmaScript Modules"
);
}
module.buildMeta.async = true;
/** @type {BuildMeta} */
(module.buildMeta).async = true;
});

/**
Expand Down
17 changes: 12 additions & 5 deletions lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const HarmonyImportSpecifierDependency = require("./HarmonyImportSpecifierDepend
/** @typedef {import("../ChunkGraph")} ChunkGraph */
/** @typedef {import("../Dependency")} Dependency */
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
/** @typedef {import("../Module").BuildMeta} BuildMeta */
/** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */
/** @typedef {import("../javascript/JavascriptParser").Assertions} Assertions */
/** @typedef {import("../javascript/JavascriptParser").Range} Range */
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
Expand Down Expand Up @@ -85,15 +87,20 @@ HarmonyEvaluatedImportSpecifierDependency.Template = class HarmonyEvaluatedImpor
// Skip rendering depending when dependency is conditional
if (connection && !connection.isTargetActive(runtime)) return;

const exportsInfo = moduleGraph.getExportsInfo(connection.module);
const exportsInfo = moduleGraph.getExportsInfo(
/** @type {ModuleGraphConnection} */ (connection).module
);
const ids = dep.getIds(moduleGraph);

let value;

const exportsType = connection.module.getExportsType(
moduleGraph,
module.buildMeta.strictHarmonyModule
);
const exportsType =
/** @type {ModuleGraphConnection} */
(connection).module.getExportsType(
moduleGraph,
/** @type {BuildMeta} */
(module.buildMeta).strictHarmonyModule
);
switch (exportsType) {
case "default-with-named": {
if (ids[0] === "default") {
Expand Down
4 changes: 2 additions & 2 deletions lib/dependencies/HarmonyExportImportedSpecifierDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
/**
* Returns warnings
* @param {ModuleGraph} moduleGraph module graph
* @returns {WebpackError[]} warnings
* @returns {WebpackError[] | null | undefined} warnings
*/
getWarnings(moduleGraph) {
const exportsPresence = this._getEffectiveExportPresenceLevel(moduleGraph);
Expand All @@ -769,7 +769,7 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
/**
* Returns errors
* @param {ModuleGraph} moduleGraph module graph
* @returns {WebpackError[]} errors
* @returns {WebpackError[] | null | undefined} errors
*/
getErrors(moduleGraph) {
const exportsPresence = this._getEffectiveExportPresenceLevel(moduleGraph);
Expand Down
Loading

0 comments on commit e381884

Please sign in to comment.