Skip to content

Commit d201a1c

Browse files
refactor(types): narrow the remaining EXPECTED_ANY in stats, templates and tooling plugins
1 parent 42cbb9d commit d201a1c

19 files changed

Lines changed: 194 additions & 113 deletions

File tree

‎.changeset/107-precise-object-function-types.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"webpack": patch
33
---
44

5-
Replace `object` and `Function` with precise types in the type definitions.
5+
Replace `any`, `object` and `Function` with precise types in the type definitions.

‎lib/debug/ProfilingPlugin.js‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,10 +510,9 @@ const interceptAllCssModulesPluginHooks = (compilation, tracer) => {
510510

511511
/**
512512
* Creates interceptor for.
513-
* @template T
514513
* @param {string} instance instance
515514
* @param {Trace} tracer tracer
516-
* @returns {(hookName: string) => HookInterceptor<EXPECTED_ANY, EXPECTED_ANY>} interceptor
515+
* @returns {<T, R>(hookName: string) => HookInterceptor<T, R>} interceptor
517516
*/
518517
const makeInterceptorFor = (instance, tracer) => (hookName) => ({
519518
/**

‎lib/devtool/SourceMapDevToolPlugin.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,10 @@ const getTaskForFile = (
307307

308308
const PLUGIN_NAME = "SourceMapDevToolPlugin";
309309

310+
/** @typedef {SourceMapDevToolPluginOptions["filename"] | SourceMapDevToolPluginOptions["append"] | Rules | DevtoolModuleFilenameTemplate | DevtoolFallbackModuleFilenameTemplate | DevtoolNamespace} CacheKeyOption */
311+
/** @typedef {CacheKeyValue[]} CacheKeyValues */
312+
/** @typedef {string | false | null | undefined | CacheKeyValues} CacheKeyValue */
313+
310314
/**
311315
* Maps a configuration value (string, RegExp, function, nullish, or array of
312316
* such) into a JSON-serializable form. Functions and RegExps are turned into
@@ -318,8 +322,8 @@ const PLUGIN_NAME = "SourceMapDevToolPlugin";
318322
* we deliberately avoid any homemade `|` / `,` separators that could collide
319323
* with characters appearing inside user-provided values such as `publicPath`,
320324
* template strings, or `sourceRoot`.
321-
* @param {EXPECTED_ANY} value option value
322-
* @returns {EXPECTED_ANY} JSON-serializable representation
325+
* @param {CacheKeyOption} value option value
326+
* @returns {CacheKeyValue} JSON-serializable representation
323327
*/
324328
const toCacheKeyValue = (value) => {
325329
if (value === undefined || value === null) return value;

‎lib/logging/ProgressPlugin.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -838,8 +838,10 @@ class ProgressPlugin {
838838
});
839839
/**
840840
* Processes the provided hook.
841-
* @template {Hook<EXPECTED_ANY, EXPECTED_ANY, EXPECTED_ANY>} T
842-
* @param {T} hook hook
841+
* @template T
842+
* @template R
843+
* @template AdditionalOptions
844+
* @param {Hook<T, R, AdditionalOptions>} hook hook
843845
* @param {number} progress progress from 0 to 1
844846
* @param {string} category category
845847
* @param {string} name name

‎lib/optimize/TupleQueue.js‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const TupleSet = require("../util/TupleSet");
1616
class TupleQueue {
1717
/**
1818
* Seeds the queue with an optional iterable of tuples to visit.
19-
* @param {Iterable<[T, V]>=} items The initial elements.
19+
* @param {Iterable<[T, V, ...EXPECTED_ANY]>=} items The initial elements.
2020
*/
2121
constructor(items) {
2222
/**
@@ -26,7 +26,7 @@ class TupleQueue {
2626
this._set = new TupleSet(items);
2727
/**
2828
* @private
29-
* @type {Iterator<[T, V]>}
29+
* @type {Iterator<[T, V, ...EXPECTED_ANY]>}
3030
*/
3131
this._iterator = this._set[Symbol.iterator]();
3232
}
@@ -41,7 +41,7 @@ class TupleQueue {
4141

4242
/**
4343
* Enqueues a tuple if it is not already present in the underlying set.
44-
* @param {[T, V]} item The element to add.
44+
* @param {[T, V, ...EXPECTED_ANY]} item The element to add.
4545
* @returns {void}
4646
*/
4747
enqueue(...item) {
@@ -51,22 +51,22 @@ class TupleQueue {
5151
/**
5252
* Removes and returns the next queued tuple, rebuilding the iterator when
5353
* the underlying tuple set has changed since the last full pass.
54-
* @returns {[T, V] | undefined} The head of the queue of `undefined` if this queue is empty.
54+
* @returns {[T, V, ...EXPECTED_ANY] | undefined} The head of the queue of `undefined` if this queue is empty.
5555
*/
5656
dequeue() {
5757
const result = this._iterator.next();
5858
if (result.done) {
5959
if (this._set.size > 0) {
6060
this._iterator = this._set[Symbol.iterator]();
6161
const value =
62-
/** @type {[T, V]} */
62+
/** @type {[T, V, ...EXPECTED_ANY]} */
6363
(this._iterator.next().value);
6464
this._set.delete(...value);
6565
return value;
6666
}
6767
return;
6868
}
69-
this._set.delete(.../** @type {[T, V]} */ (result.value));
69+
this._set.delete(.../** @type {[T, V, ...EXPECTED_ANY]} */ (result.value));
7070
return result.value;
7171
}
7272
}

‎lib/performance/HotspotsPlugin.js‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ const { contextify } = require("../util/identifier");
1414
/** @typedef {import("tapable").FullTap} FullTap */
1515
/** @typedef {import("../../declarations/WebpackOptions").PerformanceOptions} PerformanceOptions */
1616
/** @typedef {import("../Compiler")} Compiler */
17+
/** @typedef {import("../Compilation")} Compilation */
18+
/** @typedef {import("../module/NormalModuleFactory")} NormalModuleFactory */
19+
/** @typedef {import("../context/ContextModuleFactory")} ContextModuleFactory */
1720
/** @typedef {import("../errors/HotspotsWarning").HookDetails} HookDetails */
1821
/** @typedef {import("../errors/HotspotsWarning").HotspotDetails} HotspotDetails */
1922

@@ -176,7 +179,7 @@ class HotspotsPlugin {
176179
};
177180

178181
/**
179-
* @param {EXPECTED_ANY} instance anything carrying tapable hooks
182+
* @param {Compiler | Compilation | NormalModuleFactory | ContextModuleFactory} instance anything carrying tapable hooks
180183
* @returns {void}
181184
*/
182185
const interceptAll = (instance) => {
@@ -221,12 +224,13 @@ class HotspotsPlugin {
221224
NormalModule.getCompilationHooks(compilation).loader.tap(
222225
PLUGIN_NAME,
223226
(loaderContext) => {
224-
/** @type {EXPECTED_ANY} */
227+
/** @type {{ [LOADER_TIMING]?: <T>(loader: { path: string }, run: () => T) => T }} */
225228
(loaderContext)[LOADER_TIMING] =
226229
/**
230+
* @template T
227231
* @param {{ path: string }} loader the loader about to run
228-
* @param {() => EXPECTED_ANY} run its own execution
229-
* @returns {EXPECTED_ANY} whatever it returned
232+
* @param {() => T} run its own execution
233+
* @returns {T} whatever it returned
230234
*/
231235
(loader, run) =>
232236
measure(

‎lib/performance/TopLevelThisPlugin.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const {
1414
const { getBuildDiagnostics } = require("../util/buildDiagnostics");
1515
const { compareStrings } = require("../util/comparators");
1616

17-
/** @import { Program } from "estree" */
17+
/** @import { Expression, PrivateIdentifier, Program } from "estree" */
1818
/** @import { PerformanceOptions } from "../../declarations/WebpackOptions" */
1919
/** @import Compiler from "../Compiler" */
2020
/** @import Module, { BuildInfo } from "../module/Module" */
@@ -70,7 +70,9 @@ const countTopLevelThis = (ast) => {
7070
if (CLASS_TYPES.has(type)) {
7171
queue.push(fields.superClass);
7272

73-
const body = /** @type {EXPECTED_ANY} */ (fields.body);
73+
const body =
74+
/** @type {{ body: { computed?: boolean, key?: Expression | PrivateIdentifier }[] } | null | undefined} */
75+
(fields.body);
7476

7577
for (const element of (body && body.body) || []) {
7678
if (element.computed) queue.push(element.key);

‎lib/prefetch/ResourceHintPlugin.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const parseResourceHintOptions = require("./parseResourceHintOptions");
5959
*/
6060
/** @import { HtmlResourceHint } from "../dependencies/html/HtmlEntryDependency" */
6161
/** @import { ResourceHintOptions } from "./parseResourceHintOptions" */
62+
/** @import { MatchObject } from "../devtool/ModuleFilenameHelpers" */
6263

6364
/**
6465
* @typedef {object} ResolvedResourceHints
@@ -216,7 +217,7 @@ const matchUrlHints = (rules, request) => {
216217
rule.include !== undefined ||
217218
rule.exclude !== undefined) &&
218219
!ModuleFilenameHelpers.matchObject(
219-
/** @type {EXPECTED_ANY} */ ({
220+
/** @type {MatchObject} */ ({
220221
test: rule.test,
221222
include: rule.include,
222223
exclude: rule.exclude

‎lib/stats/DefaultStatsFactoryPlugin.js‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2610,8 +2610,9 @@ const sortOrderRegular = (field) => {
26102610

26112611
/**
26122612
* Returns zero.
2613-
* @param {EXPECTED_ANY} a first
2614-
* @param {EXPECTED_ANY} b second
2613+
* @template T
2614+
* @param {T} a first
2615+
* @param {T} b second
26152616
* @returns {-1 | 0 | 1} zero
26162617
*/
26172618
const noSort = (a, b) => 0;
@@ -2711,7 +2712,7 @@ const RESULT_SORTERS = {
27112712

27122713
/**
27132714
* Processes the provided config.
2714-
* @template {Record<string, Record<string, EXPECTED_ANY>>} T
2715+
* @template {Record<string, Record<string, (...args: never[]) => void>>} T
27152716
* @param {T} config the config see above
27162717
* @param {NormalizedStatsOptions} options stats options
27172718
* @param {(hookFor: keyof T, fn: ExtractFunction<T>) => void} fn handler function called for every active line in config
@@ -2735,7 +2736,7 @@ const iterateConfig = (config, options, fn) => {
27352736
}
27362737
}
27372738
}
2738-
fn(hookFor, subConfig[option]);
2739+
fn(hookFor, /** @type {ExtractFunction<T>} */ (subConfig[option]));
27392740
}
27402741
}
27412742
};

‎lib/stats/DefaultStatsPrinterPlugin.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,8 +1508,8 @@ const AVAILABLE_COLORS = {
15081508

15091509
/**
15101510
* Defines the tail parameters type used by this module.
1511-
* @template {(...args: EXPECTED_ANY[]) => EXPECTED_ANY} T
1512-
* @typedef {T extends (firstArg: EXPECTED_ANY, ...rest: infer R) => EXPECTED_ANY ? R : never} TailParameters
1511+
* @template {(...args: never[]) => string} T
1512+
* @typedef {T extends (firstArg: never, ...rest: infer R) => string ? R : never} TailParameters
15131513
*/
15141514

15151515
/** @typedef {{ [Key in keyof KnownStatsPrinterFormatters]: (value: Parameters<NonNullable<KnownStatsPrinterFormatters[Key]>>[0], options: Required<KnownStatsPrinterColorFunctions> & StatsPrinterContextWithExtra, ...args: TailParameters<NonNullable<KnownStatsPrinterFormatters[Key]>>) => string }} AvailableFormats */

0 commit comments

Comments
 (0)