Skip to content

Commit

Permalink
fix: types
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Oct 24, 2024
1 parent 0d5476d commit 6589de0
Show file tree
Hide file tree
Showing 35 changed files with 518 additions and 282 deletions.
5 changes: 5 additions & 0 deletions declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,3 +436,8 @@ declare module "watchpack" {
}
export = Watchpack;
}

declare module "eslint-scope/lib/referencer" {
class Referencer {}
export = Referencer;
}
2 changes: 1 addition & 1 deletion lib/AsyncDependenciesBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class AsyncDependenciesBlock extends DependenciesBlock {
}

/**
* @returns {string | undefined} The name of the chunk
* @returns {string | null | undefined} The name of the chunk
*/
get chunkName() {
return this.groupOptions.name;
Expand Down
5 changes: 4 additions & 1 deletion lib/ChunkGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ const modulesBySourceType = sourceTypesByModule => set => {
};
const defaultModulesBySourceType = modulesBySourceType(undefined);

/** @type {WeakMap<Function, any>} */
/**
* @template T
* @type {WeakMap<Function, any>}
*/
const createOrderedArrayFunctionMap = new WeakMap();

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/ChunkGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const {
* @property {("low" | "high" | "auto")=} fetchPriority
*/

/** @typedef {RawChunkGroupOptions & { name?: string }} ChunkGroupOptions */
/** @typedef {RawChunkGroupOptions & { name?: string | null }} ChunkGroupOptions */

let debugId = 5000;

Expand Down Expand Up @@ -137,7 +137,7 @@ class ChunkGroup {

/**
* returns the name of current ChunkGroup
* @returns {string | undefined} returns the ChunkGroup name
* @returns {string | null | undefined} returns the ChunkGroup name
*/
get name() {
return this.options.name;
Expand Down
6 changes: 3 additions & 3 deletions lib/CleanPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const processAsyncTree = require("./util/processAsyncTree");
/**
* @callback KeepFn
* @param {string} path path
* @returns {boolean} true, if the path should be kept
* @returns {boolean | void} true, if the path should be kept
*/

const validate = createSchemaValidation(
Expand Down Expand Up @@ -149,7 +149,7 @@ const doStat = (fs, filename, callback) => {
* @param {boolean} dry only log instead of fs modification
* @param {Logger} logger logger
* @param {Set<string>} diff filenames of the assets that shouldn't be there
* @param {function(string): boolean} isKept check if the entry is ignored
* @param {function(string): boolean | void} isKept check if the entry is ignored
* @param {function(Error=, Assets=): void} callback callback
* @returns {void}
*/
Expand Down Expand Up @@ -392,7 +392,7 @@ class CleanPlugin {

/**
* @param {string} path path
* @returns {boolean} true, if needs to be kept
* @returns {boolean | void} true, if needs to be kept
*/
const isKept = path => {
const result = hooks.keep.call(path);
Expand Down
90 changes: 55 additions & 35 deletions lib/Compilation.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,18 @@ const { isSourceEqual } = require("./util/source");
/** @typedef {import("./ChunkGroup").ChunkGroupOptions} ChunkGroupOptions */
/** @typedef {import("./Compiler")} Compiler */
/** @typedef {import("./Compiler").CompilationParams} CompilationParams */
/** @typedef {import("./Compiler").ModuleMemCachesItem} ModuleMemCachesItem */
/** @typedef {import("./DependenciesBlock")} DependenciesBlock */
/** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
/** @typedef {import("./Dependency").ReferencedExport} ReferencedExport */
/** @typedef {import("./DependencyTemplate")} DependencyTemplate */
/** @typedef {import("./Entrypoint").EntryOptions} EntryOptions */
/** @typedef {import("./Module").BuildInfo} BuildInfo */
/** @typedef {import("./Module").ValueCacheVersions} ValueCacheVersions */
/** @typedef {import("./NormalModule").NormalModuleCompilationHooks} NormalModuleCompilationHooks */
/** @typedef {import("./Module").CodeGenerationResult} CodeGenerationResult */
/** @typedef {import("./ModuleFactory")} ModuleFactory */
/** @typedef {import("./ChunkGraph").ModuleId} ModuleId */
/** @typedef {import("./ModuleGraphConnection")} ModuleGraphConnection */
/** @typedef {import("./ModuleFactory").ModuleFactoryCreateDataContextInfo} ModuleFactoryCreateDataContextInfo */
/** @typedef {import("./ModuleFactory").ModuleFactoryResult} ModuleFactoryResult */
Expand All @@ -119,6 +122,7 @@ const { isSourceEqual } = require("./util/source");
/** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsModule} StatsModule */
/** @typedef {import("./TemplatedPathPlugin").TemplatePath} TemplatePath */
/** @typedef {import("./util/Hash")} Hash */
/** @typedef {import("./util/createHash").Algorithm} Algorithm */
/**
* @template T
* @typedef {import("./util/deprecation").FakeHook<T>} FakeHook<T>
Expand Down Expand Up @@ -366,8 +370,6 @@ const { isSourceEqual } = require("./util/source");

/** @typedef {Set<Module>} NotCodeGeneratedModules */

/** @typedef {string | Set<string> | undefined} ValueCacheVersion */

/** @type {AssetInfo} */
const EMPTY_ASSET_INFO = Object.freeze({});

Expand Down Expand Up @@ -925,7 +927,7 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
true
);
}
/** @type {Map<string, ValueCacheVersion>} */
/** @type {ValueCacheVersions} */
this.valueCacheVersions = new Map();
this.requestShortener = compiler.requestShortener;
this.compilerPath = compiler.compilerPath;
Expand Down Expand Up @@ -1078,11 +1080,6 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
this.codeGeneratedModules = new WeakSet();
/** @type {WeakSet<Module>} */
this.buildTimeExecutedModules = new WeakSet();
/**
* @private
* @type {Map<Module, Callback[]>}
*/
this._rebuildingModules = new Map();
/** @type {Set<string>} */
this.emittedAssets = new Set();
/** @type {Set<string>} */
Expand Down Expand Up @@ -1508,7 +1505,8 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
let factoryCacheKey;
/** @type {ModuleFactory} */
let factoryCacheKey2;
/** @type {Map<string, Dependency[]>} */
/** @typedef {Map<string, Dependency[]>} FactoryCacheValue */
/** @type {FactoryCacheValue | undefined} */
let factoryCacheValue;
/** @type {string} */
let listCacheKey1;
Expand Down Expand Up @@ -1712,7 +1710,10 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
if (factoryCacheKey2 !== undefined) {
// Archive last cache entry
if (dependencies === undefined) dependencies = new Map();
dependencies.set(factoryCacheKey2, factoryCacheValue);
dependencies.set(
factoryCacheKey2,
/** @type {FactoryCacheValue} */ (factoryCacheValue)
);
factoryCacheValue = dependencies.get(factory);
if (factoryCacheValue === undefined) {
factoryCacheValue = new Map();
Expand All @@ -1731,9 +1732,12 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
category === esmDependencyCategory
? resourceIdent
: `${category}${resourceIdent}`;
let list = factoryCacheValue.get(cacheKey);
let list = /** @type {FactoryCacheValue} */ (factoryCacheValue).get(
cacheKey
);
if (list === undefined) {
factoryCacheValue.set(cacheKey, (list = []));
/** @type {FactoryCacheValue} */
(factoryCacheValue).set(cacheKey, (list = []));
sortedDependencies.push({
factory: factoryCacheKey2,
dependencies: list,
Expand Down Expand Up @@ -1763,7 +1767,7 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
}
} while (queue.length !== 0);
} catch (err) {
return callback(err);
return callback(/** @type {WebpackError} */ (err));
}

if (--inProgressSorting === 0) onDependenciesSorted();
Expand Down Expand Up @@ -1859,7 +1863,7 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
(err, factoryResult) => {
const applyFactoryResultDependencies = () => {
const { fileDependencies, contextDependencies, missingDependencies } =
factoryResult;
/** @type {ModuleFactoryResult} */ (factoryResult);
if (fileDependencies) {
this.fileDependencies.addAll(fileDependencies);
}
Expand All @@ -1880,7 +1884,9 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
return callback(err);
}

const newModule = factoryResult.module;
const newModule =
/** @type {ModuleFactoryResult} */
(factoryResult).module;

if (!newModule) {
applyFactoryResultDependencies();
Expand Down Expand Up @@ -1908,7 +1914,8 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si

if (
this._unsafeCache &&
factoryResult.cacheable !== false &&
/** @type {ModuleFactoryResult} */
(factoryResult).cacheable !== false &&
module.restoreFromUnsafeCache &&
this._unsafeCachePredicate(module)
) {
Expand Down Expand Up @@ -2116,7 +2123,8 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
const notFoundError = new ModuleNotFoundError(
originModule,
err,
dependencies.map(d => d.loc).find(Boolean)
/** @type {DependencyLocation} */
(dependencies.map(d => d.loc).find(Boolean))
);
return callback(notFoundError, factoryResult ? result : undefined);
}
Expand Down Expand Up @@ -2515,7 +2523,9 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
affectedModules.add(referencingModule);
}
const memCache = new WeakTupleMap();
const cache = moduleMemCacheCache.get(referencingModule);
const cache =
/** @type {ModuleMemCachesItem} */
(moduleMemCacheCache.get(referencingModule));
cache.memCache = memCache;
moduleMemCaches.set(referencingModule, memCache);
}
Expand Down Expand Up @@ -2544,10 +2554,10 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
let statNew = 0;
/**
* @param {Module} module module
* @returns {{ id: string | number, modules?: Map<Module, string | number | undefined>, blocks?: (string | number | null)[] }} references
* @returns {{ id: ModuleId, modules?: Map<Module, string | number | undefined>, blocks?: (string | number | null)[] }} references
*/
const computeReferences = module => {
const id = chunkGraph.getModuleId(module);
const id = /** @type {ModuleId} */ (chunkGraph.getModuleId(module));
/** @type {Map<Module, string | number | undefined> | undefined} */
let modules;
/** @type {(string | number | null)[] | undefined} */
Expand All @@ -2557,7 +2567,7 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
for (const m of outgoing.keys()) {
if (!m) continue;
if (modules === undefined) modules = new Map();
modules.set(m, chunkGraph.getModuleId(m));
modules.set(m, /** @type {ModuleId} */ (chunkGraph.getModuleId(m)));
}
}
if (module.blocks.length > 0) {
Expand Down Expand Up @@ -3746,7 +3756,6 @@ Or do you want to use the entrypoints '${name}' and '${runtime}' independently o
if (name) {
const chunkGroup = this.namedChunkGroups.get(name);
if (chunkGroup !== undefined) {
chunkGroup.addOptions(groupOptions);
if (module) {
chunkGroup.addOrigin(
module,
Expand Down Expand Up @@ -4017,10 +4026,13 @@ Or do you want to use the entrypoints '${name}' and '${runtime}' independently o

assignRuntimeIds() {
const { chunkGraph } = this;
/**
* @param {Entrypoint} ep an entrypoint
*/
const processEntrypoint = ep => {
const runtime = ep.options.runtime || ep.name;
const chunk = ep.getRuntimeChunk();
chunkGraph.setRuntimeId(runtime, chunk.id);
const runtime = /** @type {string} */ (ep.options.runtime || ep.name);
const chunk = /** @type {Chunk} */ (ep.getRuntimeChunk());
chunkGraph.setRuntimeId(runtime, /** @type {ChunkId} */ (chunk.id));
};
for (const ep of this.entrypoints.values()) {
processEntrypoint(ep);
Expand Down Expand Up @@ -4142,7 +4154,7 @@ Or do you want to use the entrypoints '${name}' and '${runtime}' independently o
) {
let moduleHashDigest;
try {
const moduleHash = createHash(hashFunction);
const moduleHash = createHash(/** @type {Algorithm} */ (hashFunction));
module.updateHash(moduleHash, {
chunkGraph,
runtime,
Expand Down Expand Up @@ -4170,15 +4182,15 @@ Or do you want to use the entrypoints '${name}' and '${runtime}' independently o
const hashFunction = outputOptions.hashFunction;
const hashDigest = outputOptions.hashDigest;
const hashDigestLength = outputOptions.hashDigestLength;
const hash = createHash(hashFunction);
const hash = createHash(/** @type {Algorithm} */ (hashFunction));
if (outputOptions.hashSalt) {
hash.update(outputOptions.hashSalt);
}
this.logger.timeEnd("hashing: initialize hash");
if (this.children.length > 0) {
this.logger.time("hashing: hash child compilations");
for (const child of this.children) {
hash.update(child.hash);
hash.update(/** @type {string} */ (child.hash));
}
this.logger.timeEnd("hashing: hash child compilations");
}
Expand Down Expand Up @@ -4239,7 +4251,9 @@ Or do you want to use the entrypoints '${name}' and '${runtime}' independently o
e => e.chunks[e.chunks.length - 1]
)
)) {
const otherInfo = runtimeChunksMap.get(other);
const otherInfo =
/** @type {RuntimeChunkInfo} */
(runtimeChunksMap.get(other));
otherInfo.referencedBy.push(info);
info.remaining++;
remaining++;
Expand Down Expand Up @@ -4351,7 +4365,7 @@ This prevents using hashes of each other and should be avoided.`);
this.logger.timeAggregate("hashing: hash runtime modules");
try {
this.logger.time("hashing: hash chunks");
const chunkHash = createHash(hashFunction);
const chunkHash = createHash(/** @type {Algorithm} */ (hashFunction));
if (outputOptions.hashSalt) {
chunkHash.update(outputOptions.hashSalt);
}
Expand Down Expand Up @@ -4404,7 +4418,7 @@ This prevents using hashes of each other and should be avoided.`);
for (const module of /** @type {Iterable<RuntimeModule>} */ (
chunkGraph.getChunkFullHashModulesIterable(chunk)
)) {
const moduleHash = createHash(hashFunction);
const moduleHash = createHash(/** @type {Algorithm} */ (hashFunction));
module.updateHash(moduleHash, {
chunkGraph,
runtime: chunk.runtime,
Expand All @@ -4422,7 +4436,7 @@ This prevents using hashes of each other and should be avoided.`);
);
codeGenerationJobsMap.get(oldHash).get(module).hash = moduleHashDigest;
}
const chunkHash = createHash(hashFunction);
const chunkHash = createHash(/** @type {Algorithm} */ (hashFunction));
chunkHash.update(chunk.hash);
chunkHash.update(this.hash);
const chunkHashDigest =
Expand Down Expand Up @@ -4467,6 +4481,12 @@ This prevents using hashes of each other and should be avoided.`);
this._setAssetInfo(file, assetInfo, undefined);
}

/**
* @private
* @param {string} file file name
* @param {AssetInfo} newInfo new asset information
* @param {AssetInfo=} oldInfo old asset information
*/
_setAssetInfo(file, newInfo, oldInfo = this.assetsInfo.get(file)) {
if (newInfo === undefined) {
this.assetsInfo.delete(file);
Expand Down Expand Up @@ -4754,8 +4774,8 @@ This prevents using hashes of each other and should be avoided.`);
try {
manifest = this.getRenderManifest({
chunk,
hash: this.hash,
fullHash: this.fullHash,
hash: /** @type {string} */ (this.hash),
fullHash: /** @type {string} */ (this.fullHash),
outputOptions,
codeGenerationResults: this.codeGenerationResults,
moduleTemplates: this.moduleTemplates,
Expand Down Expand Up @@ -5377,7 +5397,7 @@ This prevents using hashes of each other and should be avoided.`);

/**
* @typedef {object} FactorizeModuleOptions
* @property {ModuleProfile} currentProfile
* @property {ModuleProfile=} currentProfile
* @property {ModuleFactory} factory
* @property {Dependency[]} dependencies
* @property {boolean=} factoryResult return full ModuleFactoryResult instead of only module
Expand Down
4 changes: 3 additions & 1 deletion lib/Compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ const { isSourceEqual } = require("./util/source");
/** @typedef {{ sizeOnlySource: SizeOnlySource | undefined, writtenTo: Map<string, number> }} CacheEntry */
/** @typedef {{ path: string, source: Source, size: number | undefined, waiting: ({ cacheEntry: any, file: string }[] | undefined) }} SimilarEntry */

/** @typedef {{ buildInfo: BuildInfo, references: References | undefined, memCache: WeakTupleMap<any, any> }} ModuleMemCachesItem */

/**
* @param {string[]} array an array
* @returns {boolean} true, if the array is sorted
Expand Down Expand Up @@ -288,7 +290,7 @@ class Compiler {

this.cache = new Cache();

/** @type {Map<Module, { buildInfo: BuildInfo, references: References | undefined, memCache: WeakTupleMap<any, any> }> | undefined} */
/** @type {Map<Module, ModuleMemCachesItem> | undefined} */
this.moduleMemCaches = undefined;

this.compilerPath = "";
Expand Down
Loading

0 comments on commit 6589de0

Please sign in to comment.