Skip to content

Commit 1c294b7

Browse files
alexander-akaitclaude
authored andcommitted
refactor: replace EXPECTED_ANY with precise types across lib
Type the loader runner, constant evaluation in the syntax printer, serializer generics, stats JSON values, comparator caches and placeholder casts. EXPECTED_ANY in lib/ goes from 304 to 180.
1 parent 0351622 commit 1c294b7

39 files changed

Lines changed: 632 additions & 368 deletions
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Replace `any` with precise types in stats, serializer and dependency typings.

‎lib/Compilation.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ const { toError } = ErrorHelpers;
247247
* @returns {void}
248248
*/
249249

250-
/** @typedef {new (...args: EXPECTED_ANY[]) => Dependency} DependencyConstructor */
250+
/** @typedef {abstract new (...args: never[]) => Dependency} DependencyConstructor */
251251

252252
/** @typedef {Record<string, Source>} CompilationAssets */
253253

‎lib/cache/PackFileCacheStrategy.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,9 @@ class Pack {
328328
let ignoreNextTimeTick = false;
329329
const createNextPack = () => {
330330
const loc = this._findLocation();
331-
this.content[loc] = /** @type {EXPECTED_ANY} */ (null); // reserve
331+
this.content[loc] = /** @type {PackContent} */ (
332+
/** @type {PackContent | null} */ (null)
333+
); // reserve
332334
/** @type {PackItem} */
333335
const pack = {
334336
items: new Set(),

‎lib/config/defaults.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ const A = (obj, prop, factory) => {
279279
if (value === undefined) {
280280
obj[prop] = factory();
281281
} else if (Array.isArray(value)) {
282-
/** @type {EXPECTED_ANY[] | undefined} */
282+
/** @type {(T[P] extends (infer E)[] ? E : never)[] | undefined} */
283283
let newArray;
284284
for (let i = 0; i < value.length; i++) {
285285
const item = value[i];
@@ -289,7 +289,7 @@ const A = (obj, prop, factory) => {
289289
obj[prop] = /** @type {T[P]} */ (/** @type {unknown} */ (newArray));
290290
}
291291
const items =
292-
/** @type {EXPECTED_ANY[]} */
292+
/** @type {(T[P] extends (infer E)[] ? E : never)[]} */
293293
(/** @type {unknown} */ (factory()));
294294
if (items !== undefined) {
295295
for (const item of items) {

‎lib/debug/ProfilingPlugin.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class Profiler {
119119
* Returns promise for the result.
120120
* @param {string} method method name
121121
* @param {import("inspector").Profiler.SetSamplingIntervalParameterType=} params params
122-
* @returns {Promise<EXPECTED_ANY | void>} Promise for the result
122+
* @returns {Promise<Partial<import("inspector").Profiler.StopReturnType> | void>} Promise for the result, `{}` for commands without one
123123
*/
124124
sendCommand(method, params) {
125125
if (this.hasSession()) {
@@ -151,7 +151,9 @@ class Profiler {
151151
* @returns {Promise<{ profile: { startTime: number, endTime: number } }>} profile result
152152
*/
153153
stopProfiling() {
154-
return this.sendCommand("Profiler.stop").then(({ profile }) => {
154+
return /** @type {Promise<import("inspector").Profiler.StopReturnType>} */ (
155+
this.sendCommand("Profiler.stop")
156+
).then(({ profile }) => {
155157
const hrtime = process.hrtime();
156158
const endTime = hrtime[0] * 1000000 + Math.round(hrtime[1] / 1000);
157159
// Avoid coverage problems due indirect changes

‎lib/define/DefinePlugin.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ const dropTrailingMembers = (node, count) => {
255255

256256
/**
257257
* Returns code converted to string that evaluates.
258-
* @param {EXPECTED_ANY[] | { [k: string]: EXPECTED_ANY }} obj obj
258+
* @param {CodeValue[] | Record<string, CodeValue>} obj obj
259259
* @param {JavascriptParser} parser Parser
260260
* @param {ValueCacheVersions} valueCacheVersions valueCacheVersions
261261
* @param {string} key the defined key
@@ -376,7 +376,7 @@ const toCode = (
376376
}
377377
if (typeof code === "object") {
378378
return stringifyObj(
379-
code,
379+
/** @type {CodeValue[] | Record<string, CodeValue>} */ (code),
380380
parser,
381381
valueCacheVersions,
382382
key,

‎lib/dependencies/amd/AMDRequireDependenciesBlockParserPlugin.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const getUnsupportedFeatureWarning = memoize(() =>
3939
* } from "../../../declarations/WebpackOptions"
4040
*/
4141
/** @import Dependency, { DependencyLocation } from "../../graph/Dependency" */
42+
/** @import DependenciesBlock from "../../graph/DependenciesBlock" */
4243
/** @import BasicEvaluatedExpression from "../../javascript/BasicEvaluatedExpression" */
4344
/** @import JavascriptParser, { Range } from "../../javascript/JavascriptParser" */
4445
/** @import LocalModule from "./LocalModule" */
@@ -323,7 +324,9 @@ class AMDRequireDependenciesBlockParserPlugin {
323324
dep.loc = parser.getLocation(expr);
324325
depBlock.addDependency(dep);
325326

326-
parser.state.current = /** @type {EXPECTED_ANY} */ (depBlock);
327+
parser.state.current = /** @type {typeof parser.state.current} */ (
328+
/** @type {DependenciesBlock} */ (depBlock)
329+
);
327330
}
328331

329332
if (expr.arguments.length === 1) {

‎lib/dependencies/commonjs/RequireEnsureDependenciesBlockParserPlugin.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const RequireEnsureItemDependency = require("./RequireEnsureItemDependency");
1212

1313
/** @import { Expression, SpreadElement } from "estree" */
1414
/** @import { GroupOptions } from "../../graph/AsyncDependenciesBlock" */
15+
/** @import DependenciesBlock from "../../graph/DependenciesBlock" */
1516
/** @import BasicEvaluatedExpression from "../../javascript/BasicEvaluatedExpression" */
1617
/** @import JavascriptParser, { Range } from "../../javascript/JavascriptParser" */
1718
/** @import { FunctionExpressionResult } from "../helpers/getFunctionExpression" */
@@ -87,7 +88,9 @@ module.exports = class RequireEnsureDependenciesBlockParserPlugin {
8788
dep.loc = parser.getLocation(expr);
8889
depBlock.addDependency(dep);
8990
const old = parser.state.current;
90-
parser.state.current = /** @type {EXPECTED_ANY} */ (depBlock);
91+
parser.state.current = /** @type {typeof parser.state.current} */ (
92+
/** @type {DependenciesBlock} */ (depBlock)
93+
);
9194
try {
9295
let failed = false;
9396
parser.inFunctionScope(true, [], () => {

‎lib/dll/DelegatedModule.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,9 @@ class DelegatedModule extends Module {
326326
*/
327327
cleanupForCache() {
328328
super.cleanupForCache();
329-
this.delegateData =
330-
/** @type {EXPECTED_ANY} */
331-
(undefined);
329+
this.delegateData = /** @type {DelegatedModuleData} */ (
330+
/** @type {DelegatedModuleData | undefined} */ (undefined)
331+
);
332332
}
333333
}
334334

‎lib/dll/DllModule.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,9 @@ class DllModule extends Module {
187187
*/
188188
cleanupForCache() {
189189
super.cleanupForCache();
190-
this.dependencies = /** @type {EXPECTED_ANY} */ (undefined);
190+
this.dependencies = /** @type {Dependency[]} */ (
191+
/** @type {Dependency[] | undefined} */ (undefined)
192+
);
191193
}
192194
}
193195

0 commit comments

Comments
 (0)