Skip to content

Commit 808b604

Browse files
refactor: type more public and internal values precisely
Second pass on remaining EXPECTED_ANY and unknown: printer public types, profiling hook wrappers, loader entries, serializer tuple bounds, AST walks and plugin options. EXPECTED_ANY in lib/ 169 -> 118, unknown 32 -> 21.
1 parent 9c47d22 commit 808b604

26 files changed

Lines changed: 364 additions & 174 deletions

‎.changeset/400-precise-parser-types.md‎

Lines changed: 0 additions & 5 deletions
This file was deleted.

‎.changeset/400-precise-public-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 `any` with precise types in stats, serializer and dependency typings.
5+
Replace `any` and `unknown` with precise types in stats, serializer, parser and runtime typings.

‎lib/Compilation.js‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ const { toError } = ErrorHelpers;
189189
* @import {
190190
* StatsAsset,
191191
* StatsError,
192+
* StatsJsonValue,
192193
* StatsModule
193194
* } from "./stats/DefaultStatsFactoryPlugin"
194195
*/
@@ -527,7 +528,7 @@ const { toError } = ErrorHelpers;
527528
* @property {false | "none" | "error" | "warn" | "info" | "log" | "verbose"} logging
528529
* @property {((value: string) => boolean)[]} loggingDebug
529530
* @property {boolean} loggingTrace
530-
* @property {EXPECTED_ANY} _env
531+
* @property {StatsJsonValue} _env
531532
*/
532533

533534
/** @typedef {KnownNormalizedStatsOptions & Omit<StatsOptions, keyof KnownNormalizedStatsOptions> & Record<string, EXPECTED_ANY>} NormalizedStatsOptions */
@@ -909,7 +910,11 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
909910
processAssetsHook.tapAsync(
910911
getOptions(options),
911912
(assets, callback) =>
912-
/** @type {EXPECTED_ANY} */ (fn)(...getArgs(), callback)
913+
/** @type {(...args: [...AsArray<T>, typeof callback]) => void} */ (
914+
/** @type {typeof fn | ((...args: [...AsArray<T>, typeof callback]) => void)} */ (
915+
fn
916+
)
917+
)(...getArgs(), callback)
913918
);
914919
},
915920
/** @type {AsyncSeriesHook<T>["tapPromise"]} */

‎lib/config/defaults.js‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ const {
8787
*/
8888
/** @import { WasmLoadingTypes } from "../wasm/EnableWasmLoadingPlugin" */
8989
/** @import { PlatformTargetProperties, TargetProperties } from "./target" */
90+
/** @import Compiler from "../Compiler" */
91+
/** @typedef {Parameters<import("minimizer-webpack-plugin")["apply"]>[0]} MinimizerCompiler */
9092

9193
/**
9294
* Defines the recursive non nullable type used by this module.
@@ -2616,7 +2618,11 @@ const applyOptimizationDefaults = (
26162618
test: assetNameTest(["[cm]?js"]),
26172619
minify: javascriptMinify,
26182620
minimizerOptions: javascriptOptions
2619-
}).apply(/** @type {EXPECTED_ANY} */ (compiler));
2621+
}).apply(
2622+
/** @type {MinimizerCompiler} */ (
2623+
/** @type {Compiler | MinimizerCompiler} */ (compiler)
2624+
)
2625+
);
26202626
return;
26212627
}
26222628
/** @type {(typeof javascriptMinify | string)[]} */
@@ -2706,7 +2712,11 @@ const applyOptimizationDefaults = (
27062712
test: assetNameTest(extensions),
27072713
minify,
27082714
minimizerOptions
2709-
}).apply(/** @type {EXPECTED_ANY} */ (compiler));
2715+
}).apply(
2716+
/** @type {MinimizerCompiler} */ (
2717+
/** @type {Compiler | MinimizerCompiler} */ (compiler)
2718+
)
2719+
);
27102720
}
27112721
}
27122722
]);

‎lib/config/getClaimedAssetTypes.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const { matchObject } = require("../devtool/ModuleFilenameHelpers");
99

1010
/** @import Compiler from "../Compiler" */
1111
/** @import { MatchObject } from "../devtool/ModuleFilenameHelpers" */
12-
/** @typedef {MatchObject & { minimizer?: import("minimizer-webpack-plugin").InternalPluginOptions<EXPECTED_ANY>["minimizer"] }} MinimizerOptions */
12+
/** @typedef {MatchObject & { minimizer?: import("minimizer-webpack-plugin").InternalPluginOptions<import("terser").MinifyOptions>["minimizer"] }} MinimizerOptions */
1313

1414
/**
1515
* @param {NonNullable<Compiler["options"]["optimization"]["minimizer"]>[number] | Compiler["options"]["plugins"][number]} candidate a configured plugin or minimizer entry

‎lib/debug/ProfilingPlugin.js‎

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,7 @@ const { fileUrlToPath } = require("../util/identifier");
3434
* @property {AbsolutePath=} outputPath Path to the output file e.g. `path.resolve(__dirname, 'profiling/events.json')`. Defaults to `events.json`.
3535
*/
3636

37-
/**
38-
* Defines the hook type used by this module.
39-
* @template T, R
40-
* @typedef {import("tapable").Hook<T, R>} Hook
41-
*/
42-
43-
/**
44-
* Defines the fake hook type used by this module.
45-
* @template T
46-
* @typedef {import("../util/deprecation").FakeHook<T>} FakeHook
47-
*/
48-
49-
/**
50-
* Defines the hook map type used by this module.
51-
* @template T
52-
* @typedef {import("tapable").HookMap<T>} HookMap
53-
*/
37+
/** @import { FakeHookMarker } from "../util/deprecation" */
5438

5539
/**
5640
* Defines the hook interceptor type used by this module.
@@ -408,27 +392,43 @@ class ProfilingPlugin {
408392
}
409393
}
410394

411-
/** @typedef {Record<string, Hook<EXPECTED_ANY, EXPECTED_ANY> | FakeHook<EXPECTED_ANY> | HookMap<EXPECTED_ANY>>} Hooks */
395+
/**
396+
* The part of a tapable `HookInterceptor` this plugin sets: it rewraps each tap.
397+
* @typedef {{ register?(tap: FullTap): FullTap }} TapInterceptor
398+
*/
399+
400+
/**
401+
* What intercepting a tapable `Hook` needs of it. Methods, so every hook fits
402+
* whatever its arguments, result and additional tap options.
403+
* @typedef {{ intercept(interceptor: TapInterceptor): void, _fakeHook?: undefined }} HookWithInterceptor
404+
*/
405+
406+
/**
407+
* What intercepting a tapable `HookMap` needs of it, whatever hook it holds.
408+
* @typedef {{ for(key: string): HookWithInterceptor, intercept(interceptor: { factory?(key: string, hook: HookWithInterceptor): HookWithInterceptor }): void, _fakeHook?: undefined }} HookMapWithInterceptor
409+
*/
410+
411+
/** @typedef {Record<string, HookWithInterceptor | HookMapWithInterceptor | FakeHookMarker>} Hooks */
412412

413413
/**
414414
* Intercepts the taps of a hook or every cached and future hook in a hook map.
415-
* @param {Hooks[string]} hook the hook or hook map
416-
* @param {HookInterceptor<EXPECTED_ANY, EXPECTED_ANY>} interceptor what intercepts each tap
415+
* @param {HookWithInterceptor | HookMapWithInterceptor} hook the hook or hook map
416+
* @param {TapInterceptor} interceptor what intercepts each tap
417417
* @returns {void}
418418
*/
419419
const interceptTaps = (hook, interceptor) => {
420420
if ("for" in hook) {
421421
const { _map } =
422-
/** @type {HookMap<Hook<EXPECTED_ANY, EXPECTED_ANY>> & { _map: Map<string, Hook<EXPECTED_ANY, EXPECTED_ANY>> }} */
422+
/** @type {HookMapWithInterceptor & { _map: Map<string, HookWithInterceptor> }} */
423423
(hook);
424424
for (const cached of _map.values()) {
425425
cached.intercept(interceptor);
426426
}
427427
hook.intercept({
428428
/**
429429
* @param {string} key the key the hook is created for
430-
* @param {Hook<EXPECTED_ANY, EXPECTED_ANY>} created the hook created
431-
* @returns {Hook<EXPECTED_ANY, EXPECTED_ANY>} the same hook, intercepted
430+
* @param {HookWithInterceptor} created the hook created
431+
* @returns {HookWithInterceptor} the same hook, intercepted
432432
*/
433433
factory: (key, created) => {
434434
created.intercept(interceptor);

‎lib/define/DefinePlugin.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1115,7 +1115,9 @@ class DefinePlugin {
11151115
// so scanning that text cannot find every reference.
11161116
const hasPreparsedAst =
11171117
/** @type {{ _ast: PreparsedAst | null }} */ (
1118-
/** @type {unknown} */ (module)
1118+
/** @type {NormalModule | { _ast: PreparsedAst | null }} */ (
1119+
module
1120+
)
11191121
)._ast !== null;
11201122
await resolveAsyncRuntimeValues(
11211123
compilation,

‎lib/define/EnvironmentPlugin.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,25 @@ const DefinePlugin = require("./DefinePlugin");
1010

1111
/** @import Compiler from "../Compiler" */
1212
/** @import { CodeValue } from "./DefinePlugin" */
13+
/** @import { JsonObject } from "../fs/fs" */
1314

1415
const PLUGIN_NAME = "EnvironmentPlugin";
1516

1617
class EnvironmentPlugin {
1718
/**
1819
* Creates an instance of EnvironmentPlugin.
19-
* @param {(string | string[] | Record<string, EXPECTED_ANY>)[]} keys keys
20+
* @param {(string | string[] | JsonObject)[]} keys keys
2021
*/
2122
constructor(...keys) {
2223
if (keys.length === 1 && Array.isArray(keys[0])) {
2324
/** @type {string[]} */
2425
this.keys = keys[0];
26+
/** @type {JsonObject} */
2527
this.defaultValues = {};
2628
} else if (keys.length === 1 && keys[0] && typeof keys[0] === "object") {
2729
this.keys = Object.keys(keys[0]);
2830
this.defaultValues =
29-
/** @type {Record<string, EXPECTED_ANY>} */
31+
/** @type {JsonObject} */
3032
(keys[0]);
3133
} else {
3234
this.keys = /** @type {string[]} */ (keys);

‎lib/dependencies/esm/ESMExportExpressionDependency.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ const DEFAULT_EXPORT_NAME = "default";
3030
/** @import { InlinedValue } from "../../optimize/InlineExports" */
3131
/** @import { Range } from "../../javascript/JavascriptParser" */
3232
/** @typedef {string | { id?: string | undefined, range: Range, prefix: string, suffix: string }} DeclarationId */
33-
/** @typedef {import("../../serialization/ObjectMiddleware").ObjectDeserializerContext<[Range, Range, string, DeclarationId | undefined, boolean, boolean]>} ObjectDeserializerContext */
34-
/** @typedef {import("../../serialization/ObjectMiddleware").ObjectSerializerContext<[Range, Range, string, DeclarationId | undefined, boolean, boolean]>} ObjectSerializerContext */
33+
/** @typedef {import("../../serialization/ObjectMiddleware").ObjectDeserializerContext<[Range, Range, string, DeclarationId | undefined, boolean, boolean, InlinedValue | undefined]>} ObjectDeserializerContext */
34+
/** @typedef {import("../../serialization/ObjectMiddleware").ObjectSerializerContext<[Range, Range, string, DeclarationId | undefined, boolean, boolean, InlinedValue | undefined]>} ObjectSerializerContext */
3535
/** @import { ExportMap } from "./ESMExportInitFragment" */
3636
/**
3737
* @import {

‎lib/errors/ErrorHelpers.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ const cutOffWrappingFrames = (stack) => {
134134
/**
135135
* Returns the value as an error, naming whatever a tap, a loader or a plugin
136136
* failed with when that is not one. Everything reading a failure expects one.
137-
* @param {EXPECTED_ANY} value value something failed with
137+
* @template T
138+
* @param {T} value value something failed with
138139
* @returns {Error} the value, or an error naming it
139140
*/
140141
const toError = (value) => {

0 commit comments

Comments
 (0)