Skip to content

Commit 2f34cea

Browse files
refactor(types): replace EXPECTED_OBJECT and EXPECTED_FUNCTION with real types (#22642)
* refactor(types): replace EXPECTED_OBJECT and EXPECTED_FUNCTION with real types Type loader functions, HMR module factories and self-accept handlers, guard frames, syntax-printer walker callbacks, source maps and parse options, and the CSS/HTML print options instead of object/Function. * refactor(types): use real types instead of unknown and Record<string, unknown> Type the minimizer options, transform switches, profiler parameters, CLI configuration values, HMR handlers and the deprecated externals function precisely. * refactor(types): let transform callbacks return a splice or the skip marker * refactor(types): let transform callbacks return null * refactor(types): type the self-accept error as unknown, as catch reads it * refactor(types): type parseTree's options as terser's parse options * fix(javascript): keep the comments of an empty statement the SyntaxTree printer drops Ported from #22649 so this branch's unit job passes on the current main.
1 parent 2b40c96 commit 2f34cea

27 files changed

Lines changed: 317 additions & 96 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 `object` and `Function` with precise types in the type definitions.

‎lib/cache/PackFileCacheStrategy.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1190,7 +1190,7 @@ class PackFileCacheStrategy {
11901190
compression,
11911191
readonly
11921192
}) {
1193-
/** @type {import("../serialization/Serializer")<PackContainer, null, EXPECTED_OBJECT>} */
1193+
/** @type {import("../serialization/Serializer")<PackContainer, null, import("../serialization/FileMiddleware").Context>} */
11941194
this.fileSerializer = createFileSerializer(
11951195
fs,
11961196
/** @type {HashFunction} */

‎lib/config/cli.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,9 @@ const getArguments = (schema = webpackSchema) => {
503503
return flags;
504504
};
505505

506-
/** @type {WeakMap<EXPECTED_OBJECT, number>} */
506+
/** @typedef {ObjectConfiguration | ParsedValue | undefined} ConfigurationValue */
507+
508+
/** @type {WeakMap<ConfigurationValue[], number>} */
507509
const cliAddedItems = new WeakMap();
508510

509511
/** @typedef {string | number} Property */
@@ -521,7 +523,7 @@ const isUnsafeKey = (name) =>
521523
* @param {ObjectConfiguration} config configuration
522524
* @param {string} schemaPath path in the config
523525
* @param {number | undefined} index index of value when multiple values are provided, otherwise undefined
524-
* @returns {{ problem?: LocalProblem, object?: ObjectConfiguration, property?: Property, value?: EXPECTED_OBJECT | EXPECTED_ANY[] }} problem or object with property and value
526+
* @returns {{ problem?: LocalProblem, object?: ObjectConfiguration, property?: Property, value?: ConfigurationValue }} problem or object with property and value
525527
*/
526528
const getObjectAndProperty = (config, schemaPath, index = 0) => {
527529
if (!schemaPath) return { value: config };

‎lib/config/defaults.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2594,7 +2594,7 @@ const applyOptimizationDefaults = (
25942594
}
25952595
/** @type {(typeof javascriptMinify | string)[]} */
25962596
const minify = [];
2597-
/** @type {EXPECTED_OBJECT[]} */
2597+
/** @type {(typeof javascriptOptions | NonNullable<Parameters<typeof import("../css/cssMinify")>[1]> | NonNullable<Parameters<typeof import("../html/htmlMinify")>[1]> | Record<string, never>)[]} */
25982598
const minimizerOptions = [];
25992599
/** @type {string[]} */
26002600
const extensions = [];

‎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?: EXPECTED_OBJECT }} MinimizerOptions */
12+
/** @typedef {MatchObject & { minimizer?: import("minimizer-webpack-plugin").InternalPluginOptions<EXPECTED_ANY>["minimizer"] }} MinimizerOptions */
1313

1414
/**
1515
* @param {unknown} candidate a configured plugin or minimizer entry

‎lib/css/syntax-parser.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7600,14 +7600,14 @@ const _keptComments = (comments) => {
76007600
* `optimization.minimize.css` names beside `environment` and the two rewrites
76017601
* that are off until asked for. Exported so the minify functions the minimizer
76027602
* plugin ships to its workers can pick them without repeating the names.
7603-
* @param {EXPECTED_OBJECT} options an options object that may carry them
7603+
* @param {CssTransformOptions} options an options object that may carry them
76047604
* @returns {CssTransformOptions | undefined} the switches, or undefined when none is set
76057605
*/
76067606
const pickTransforms = (options) => {
76077607
/** @type {EXPECTED_ANY} */
76087608
let out;
76097609
for (const name of Object.keys(_ALL_TRANSFORMS)) {
7610-
const value = /** @type {EXPECTED_ANY} */ (options)[name];
7610+
const value = options[/** @type {keyof CssTransformOptions} */ (name)];
76117611
if (value === undefined) continue;
76127612
if (out === undefined) out = {};
76137613
out[name] = value;

‎lib/css/syntax.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const SourceProcessor = require("../util/SourceProcessor");
1616
*/
1717
/** @typedef {import("./syntax-parser").Node} Node */
1818
/** @typedef {import("./syntax-parser").CssProcessOptions} CssProcessOptions */
19+
/** @typedef {import("./syntax-parser").CssPrintOptions} CssPrintOptions */
1920
/** @typedef {import("./syntax-parser").CssPath} CssPath */
2021

2122
/** @type {SyntaxParser | undefined} */
@@ -47,7 +48,7 @@ const printer = () => _printer || (_printer = require("./syntax-printer"));
4748
* ```
4849
* @experimental exposed as `webpack.css.syntax.SourceProcessor`; unstable API
4950
* @template [TState=unknown] what `process` hands every visitor as `path.state`
50-
* @extends {SourceProcessor<CssPath, Node, CssProcessOptions, EXPECTED_OBJECT, CssParts, TState, CssVisitorMap<TState>>}
51+
* @extends {SourceProcessor<CssPath, Node, CssProcessOptions, CssPrintOptions, CssParts, TState, CssVisitorMap<TState>>}
5152
*/
5253
class CssSourceProcessor extends SourceProcessor {
5354
constructor() {

‎lib/debug/ProfilingPlugin.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class Profiler {
118118
/**
119119
* Returns promise for the result.
120120
* @param {string} method method name
121-
* @param {EXPECTED_OBJECT=} params params
121+
* @param {import("inspector").Profiler.SetSamplingIntervalParameterType=} params params
122122
* @returns {Promise<EXPECTED_ANY | void>} Promise for the result
123123
*/
124124
sendCommand(method, params) {

‎lib/externals/ExternalModuleFactoryPlugin.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const ExternalModule = require("./ExternalModule");
1818
* ResolveOptions,
1919
* ExternalsType,
2020
* ExternalItem,
21+
* ExternalItemFunction,
2122
* ExternalItemValue,
2223
* ExternalItemObjectKnown,
2324
* ExternalItemObjectUnknown,
@@ -75,14 +76,13 @@ const getAlternateCoreModuleRequest = (request) => {
7576
const callDeprecatedExternals = util.deprecate(
7677
/**
7778
* Handles the callback logic for this hook.
78-
* @param {EXPECTED_FUNCTION} externalsFunction externals function
79+
* @param {ExternalItemFunction} externalsFunction externals function, written in the deprecated `(context, request, callback)` form
7980
* @param {string} context context
8081
* @param {string} request request
8182
* @param {(err: Error | null | undefined, value: ExternalValue | undefined, ty: ExternalsType | undefined) => void} cb cb
8283
*/
8384
(externalsFunction, context, request, cb) => {
84-
// eslint-disable-next-line no-useless-call
85-
externalsFunction.call(null, context, request, cb);
85+
Reflect.apply(externalsFunction, null, [context, request, cb]);
8686
},
8787
"The externals-function should be defined like ({context, request}, cb) => { ... }",
8888
"DEP_WEBPACK_EXTERNALS_FUNCTION_PARAMETERS"

‎lib/hmr/HotModuleReplacement.runtime.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@
5656
* @property {Record<ModuleId, AcceptCallback>} _acceptedDependencies accept callbacks by dependency id
5757
* @property {Record<ModuleId, AcceptErrorHandler | undefined>} _acceptedErrorHandlers error handlers by dependency id
5858
* @property {Record<ModuleId, boolean>} _declinedDependencies declined dependency ids
59-
* @property {boolean | EXPECTED_FUNCTION} _selfAccepted true or the self-accept error handler
59+
* @property {boolean | AcceptErrorHandler} _selfAccepted true or the self-accept error handler
6060
* @property {boolean} _selfDeclined module declined itself
6161
* @property {boolean} _selfInvalidated module invalidated itself
6262
* @property {DisposeHandler[]} _disposeHandlers registered dispose handlers
6363
* @property {boolean} _main module is the entry of the update
6464
* @property {() => void} _requireSelf re-require the module on apply
6565
* @property {boolean} active module is not disposed
66-
* @property {(dep?: ModuleId | ModuleId[] | AcceptCallback, callback?: AcceptCallback, errorHandler?: AcceptErrorHandler) => void} accept accept updates
66+
* @property {(dep?: ModuleId | ModuleId[] | AcceptErrorHandler, callback?: AcceptCallback, errorHandler?: AcceptErrorHandler) => void} accept accept updates
6767
* @property {(dep?: ModuleId | ModuleId[]) => void} decline decline updates
6868
* @property {(callback: DisposeHandler) => void} dispose add a dispose handler
6969
* @property {(callback: DisposeHandler) => void} addDisposeHandler add a dispose handler

0 commit comments

Comments
 (0)