Skip to content

Commit fa54eac

Browse files
chore: fix types
1 parent 34df7ee commit fa54eac

File tree

8 files changed

+141
-80
lines changed

8 files changed

+141
-80
lines changed

lib/NormalModuleFactory.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,8 +572,11 @@ class NormalModuleFactory extends ModuleFactory {
572572

573573
/** @type {ModuleSettings} */
574574
const settings = {};
575+
/** @type {LoaderItem[]} */
575576
const useLoadersPost = [];
577+
/** @type {LoaderItem[]} */
576578
const useLoaders = [];
579+
/** @type {LoaderItem[]} */
577580
const useLoadersPre = [];
578581

579582
// handle .webpack[] suffix
@@ -632,7 +635,8 @@ class NormalModuleFactory extends ModuleFactory {
632635
typeof r.value === "object" &&
633636
r.value !== null &&
634637
typeof settings[
635-
/** @type {keyof ModuleSettings} */ (r.type)
638+
/** @type {keyof ModuleSettings} */
639+
(r.type)
636640
] === "object" &&
637641
settings[/** @type {keyof ModuleSettings} */ (r.type)] !== null
638642
) {

lib/rules/RuleSetCompiler.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const { SyncHook } = require("tapable");
1010
/** @typedef {import("../../declarations/WebpackOptions").Falsy} Falsy */
1111
/** @typedef {import("../../declarations/WebpackOptions").RuleSetLoaderOptions} RuleSetLoaderOptions */
1212
/** @typedef {import("../../declarations/WebpackOptions").RuleSetRule} RuleSetRule */
13+
/** @typedef {import("../NormalModule").LoaderItem} LoaderItem */
1314

1415
/** @typedef {(Falsy | RuleSetRule)[]} RuleSetRules */
1516

@@ -54,12 +55,22 @@ const { SyncHook } = require("tapable");
5455
* @property {CompiledRule[]=} oneOf
5556
*/
5657

58+
/** @typedef {"use" | "use-pre" | "use-post"} EffectUseType */
59+
5760
/**
58-
* @typedef {object} Effect
61+
* @typedef {object} EffectUse
62+
* @property {EffectUseType} type
63+
* @property {{ loader: string, options?: string | null | Record<string, EXPECTED_ANY>, ident?: string }} value
64+
*/
65+
66+
/**
67+
* @typedef {object} EffectBasic
5968
* @property {string} type
60-
* @property {TODO} value
69+
* @property {EXPECTED_ANY} value
6170
*/
6271

72+
/** @typedef {EffectUse | EffectBasic} Effect */
73+
6374
/** @typedef {Map<string, RuleSetLoaderOptions>} References */
6475

6576
/**

lib/rules/UseEffectRulePlugin.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const util = require("util");
1616
/** @typedef {import("./RuleSetCompiler")} RuleSetCompiler */
1717
/** @typedef {import("./RuleSetCompiler").Effect} Effect */
1818
/** @typedef {import("./RuleSetCompiler").EffectData} EffectData */
19+
/** @typedef {import("./RuleSetCompiler").EffectUseType} EffectUseType */
1920

2021
const PLUGIN_NAME = "UseEffectRulePlugin";
2122

@@ -52,7 +53,9 @@ class UseEffectRulePlugin {
5253
const use = /** @type {RuleSetUse} */ (rule.use);
5354
const enforce = rule.enforce;
5455

55-
const type = enforce ? `use-${enforce}` : "use";
56+
const type =
57+
/** @type {EffectUseType} */
58+
(enforce ? `use-${enforce}` : "use");
5659

5760
/**
5861
* @param {string} path options path
@@ -89,7 +92,7 @@ class UseEffectRulePlugin {
8992
}
9093
};
9194
}
92-
const loader = item.loader;
95+
const loader = /** @type {string} */ (item.loader);
9396
const options = item.options;
9497
let ident = item.ident;
9598
if (options && typeof options === "object") {
@@ -207,12 +210,15 @@ class UseEffectRulePlugin {
207210

208211
const ident =
209212
options && typeof options === "object" ? path : undefined;
210-
references.set(
211-
/** @type {TODO} */
212-
(ident),
213-
/** @type {RuleSetLoaderOptions} */
214-
(options)
215-
);
213+
214+
if (ident) {
215+
references.set(
216+
ident,
217+
/** @type {RuleSetLoaderOptions} */
218+
(options)
219+
);
220+
}
221+
216222
result.effects.push({
217223
type: enforce ? `use-${enforce}` : "use",
218224
value: {

lib/stats/DefaultStatsPresetPlugin.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ const AUTO_FOR_TO_STRING = ({ all }, { forToString }) => {
182182
/** @typedef {keyof NormalizedStatsOptions} DefaultsKeys */
183183
/** @typedef {{ [Key in DefaultsKeys]: (options: Partial<NormalizedStatsOptions>, context: CreateStatsOptionsContext, compilation: Compilation) => NormalizedStatsOptions[Key] | RequestShortener }} Defaults */
184184

185-
/** @type {Partial<Defaults>} */
185+
/** @type {Defaults} */
186186
const DEFAULTS = {
187187
context: (options, context, compilation) => compilation.compiler.context,
188188
requestShortener: (options, context, compilation) =>
@@ -307,9 +307,9 @@ const normalizeFilter = item => {
307307
};
308308

309309
/** @typedef {keyof (KnownNormalizedStatsOptions | StatsOptions)} NormalizerKeys */
310-
/** @typedef {{ [Key in NormalizerKeys]: (value: StatsOptions[Key]) => KnownNormalizedStatsOptions[Key] }} Normalizers */
310+
/** @typedef {{ [Key in NormalizerKeys]?: (value: StatsOptions[Key]) => KnownNormalizedStatsOptions[Key] }} Normalizers */
311311

312-
/** @type {Partial<Normalizers>} */
312+
/** @type {Normalizers} */
313313
const NORMALIZER = {
314314
excludeModules: value => {
315315
if (!Array.isArray(value)) {
@@ -391,18 +391,16 @@ class DefaultStatsPresetPlugin {
391391
compilation.hooks.statsNormalize.tap(PLUGIN_NAME, (options, context) => {
392392
for (const key of Object.keys(DEFAULTS)) {
393393
if (options[key] === undefined) {
394-
options[key] =
395-
/** @type {Defaults[DefaultsKeys]} */
396-
(DEFAULTS[/** @type {DefaultsKeys} */ (key)])(
397-
options,
398-
context,
399-
compilation
400-
);
394+
options[key] = DEFAULTS[/** @type {DefaultsKeys} */ (key)](
395+
options,
396+
context,
397+
compilation
398+
);
401399
}
402400
}
403401
for (const key of Object.keys(NORMALIZER)) {
404402
options[key] =
405-
/** @type {TODO} */
403+
/** @type {NonNullable<Normalizers[keyof Normalizers]>} */
406404
(NORMALIZER[/** @type {NormalizerKeys} */ (key)])(options[key]);
407405
}
408406
});

lib/stats/StatsFactory.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ class StatsFactory {
121121
getItemFactory: new HookMap(() => new SyncBailHook(["item", "context"]))
122122
});
123123
const hooks = this.hooks;
124-
this._caches = /** @type {TODO} */ ({});
124+
this._caches =
125+
/** @type {{ [Key in keyof StatsFactoryHooks]: Map<string, SyncBailHook<EXPECTED_ANY, EXPECTED_ANY>[]> }} */ ({});
125126
for (const key of Object.keys(hooks)) {
126127
this._caches[/** @type {keyof StatsFactoryHooks} */ (key)] = new Map();
127128
}

lib/stats/StatsPrinter.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,7 @@ class StatsPrinter {
102102
print: new HookMap(() => new SyncBailHook(["object", "context"])),
103103
result: new HookMap(() => new SyncWaterfallHook(["result", "context"]))
104104
});
105-
/**
106-
* @type {TODO}
107-
*/
105+
/** @type {Map<StatsPrintHooks[keyof StatsPrintHooks], Map<string, import("tapable").Hook<EXPECTED_ANY, EXPECTED_ANY>[]>>} */
108106
this._levelHookCache = new Map();
109107
this._inPrint = false;
110108
}
@@ -126,7 +124,7 @@ class StatsPrinter {
126124
}
127125
const cacheEntry = cache.get(type);
128126
if (cacheEntry !== undefined) {
129-
return cacheEntry;
127+
return /** @type {H[]} */ (cacheEntry);
130128
}
131129
/** @type {H[]} */
132130
const hooks = [];
@@ -146,7 +144,7 @@ class StatsPrinter {
146144
* @private
147145
* @template {StatsPrintHooks[keyof StatsPrintHooks]} HM
148146
* @template {HM extends HookMap<infer H> ? H : never} H
149-
* @template {H extends import("tapable").Hook<any, infer R> ? R : never} R
147+
* @template {H extends import("tapable").Hook<EXPECTED_ANY, infer R> ? R : never} R
150148
* @param {HM} hookMap hook map
151149
* @param {string} type type
152150
* @param {(hooK: H) => R | undefined | void} fn fn

0 commit comments

Comments
 (0)