Skip to content

Commit 57a57d7

Browse files
committed
refactor: replace EXPECTED_ANY with specific types in hot module definitions
1 parent 177b18a commit 57a57d7

2 files changed

Lines changed: 48 additions & 42 deletions

File tree

src/hot.js

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22
/** @typedef {import("webpack").MultiCompiler} MultiCompiler */
33
/** @typedef {import("webpack").Stats} Stats */
44
/** @typedef {import("webpack").MultiStats} MultiStats */
5+
/** @typedef {import("webpack").StatsCompilation} StatsCompilation */
6+
/** @typedef {import("webpack").StatsError} StatsError */
7+
/** @typedef {import("webpack").StatsModule} StatsModule */
58
/** @typedef {import("./index.js").IncomingMessage} IncomingMessage */
69
/** @typedef {import("./index.js").ServerResponse} ServerResponse */
710

8-
// eslint-disable-next-line jsdoc/reject-any-type
9-
/** @typedef {any} EXPECTED_ANY */
11+
/** @typedef {NonNullable<import("webpack").Configuration["stats"]>} StatsOptions */
1012

1113
/**
1214
* @typedef {object} HotOptions
1315
* @property {string=} path the path the SSE endpoint is served at
1416
* @property {number=} heartbeat heartbeat interval in milliseconds
1517
* @property {((message: string) => void) | false=} log logger
16-
* @property {EXPECTED_ANY=} statsOptions webpack stats options used when serializing compilation results
18+
* @property {StatsOptions=} statsOptions webpack stats options used when serializing compilation results
1719
*/
1820

1921
/**
@@ -78,16 +80,15 @@ function createEventStream(heartbeat) {
7880
}, heartbeat);
7981

8082
// Don't block process exit on the heartbeat timer.
81-
if (typeof (/** @type {EXPECTED_ANY} */ (interval).unref) === "function") {
82-
/** @type {EXPECTED_ANY} */
83-
(interval).unref();
83+
if (typeof interval.unref === "function") {
84+
interval.unref();
8485
}
8586

8687
return {
8788
close() {
8889
clearInterval(interval);
8990
everyClient((client) => {
90-
if (!(/** @type {EXPECTED_ANY} */ (client).writableEnded)) {
91+
if (!client.writableEnded) {
9192
client.end();
9293
}
9394
});
@@ -104,7 +105,7 @@ function createEventStream(heartbeat) {
104105
"X-Accel-Buffering": "no",
105106
};
106107

107-
const { httpVersion, socket } = /** @type {EXPECTED_ANY} */ (req);
108+
const { httpVersion, socket } = req;
108109
const isHttp1 = !(Number.parseInt(httpVersion, 10) >= 2);
109110

110111
if (isHttp1) {
@@ -121,7 +122,7 @@ function createEventStream(heartbeat) {
121122
clients.set(id, res);
122123

123124
req.on("close", () => {
124-
if (!(/** @type {EXPECTED_ANY} */ (res).writableEnded)) {
125+
if (!res.writableEnded) {
125126
res.end();
126127
}
127128
clients.delete(id);
@@ -136,7 +137,7 @@ function createEventStream(heartbeat) {
136137
}
137138

138139
/**
139-
* @param {EXPECTED_ANY[]} errors errors or warnings
140+
* @param {(string | StatsError)[]} errors errors or warnings
140141
* @returns {string[]} flat strings
141142
*/
142143
function formatErrors(errors) {
@@ -148,7 +149,7 @@ function formatErrors(errors) {
148149
return /** @type {string[]} */ (errors);
149150
}
150151

151-
return errors.map((error) => {
152+
return /** @type {StatsError[]} */ (errors).map((error) => {
152153
const moduleName = error.moduleName || "";
153154
const loc = error.loc || "";
154155

@@ -157,9 +158,9 @@ function formatErrors(errors) {
157158
}
158159

159160
/**
160-
* @param {EXPECTED_ANY} stats stats
161-
* @param {EXPECTED_ANY} statsOptions stats options
162-
* @returns {EXPECTED_ANY} json stats with compilation reference attached
161+
* @param {Stats} stats stats
162+
* @param {StatsOptions} statsOptions stats options
163+
* @returns {StatsCompilation} json stats with compilation reference attached
163164
*/
164165
function normalizeStats(stats, statsOptions) {
165166
const statsJson = stats.toJson(statsOptions);
@@ -172,8 +173,8 @@ function normalizeStats(stats, statsOptions) {
172173
}
173174

174175
/**
175-
* @param {EXPECTED_ANY} stats normalized stats
176-
* @returns {EXPECTED_ANY[]} extracted bundles
176+
* @param {StatsCompilation} stats normalized stats
177+
* @returns {StatsCompilation[]} extracted bundles
177178
*/
178179
function extractBundles(stats) {
179180
if (stats.modules) {
@@ -188,15 +189,17 @@ function extractBundles(stats) {
188189
}
189190

190191
/**
191-
* @param {EXPECTED_ANY[]} modules modules
192+
* @param {StatsModule[]} modules modules
192193
* @returns {Record<string, string>} module id to name map
193194
*/
194195
function buildModuleMap(modules) {
195196
/** @type {Record<string, string>} */
196197
const map = {};
197198

198199
for (const item of modules) {
199-
map[item.id] = item.name;
200+
map[/** @type {string | number} */ (item.id)] = /** @type {string} */ (
201+
item.name
202+
);
200203
}
201204

202205
return map;
@@ -207,7 +210,7 @@ function buildModuleMap(modules) {
207210
* @param {Stats | MultiStats} statsResult stats result
208211
* @param {EventStream} eventStream event stream
209212
* @param {((message: string) => void) | false} log logger or false to disable
210-
* @param {EXPECTED_ANY} statsOptions stats options
213+
* @param {StatsOptions | undefined} statsOptions stats options
211214
*/
212215
function publishStats(action, statsResult, eventStream, log, statsOptions) {
213216
const resultStatsOptions = {
@@ -222,17 +225,13 @@ function publishStats(action, statsResult, eventStream, log, statsOptions) {
222225
...(statsOptions && typeof statsOptions === "object" ? statsOptions : {}),
223226
};
224227

225-
/** @type {EXPECTED_ANY[]} */
226-
let bundles = [];
228+
/** @type {StatsCompilation[]} */
229+
let bundles;
227230

228231
// Multi-compiler stats have stats for each child compiler.
229-
if (/** @type {EXPECTED_ANY} */ (statsResult).stats) {
230-
bundles = /** @type {EXPECTED_ANY} */ (statsResult).stats.flatMap(
231-
/**
232-
* @param {EXPECTED_ANY} stats stats
233-
* @returns {EXPECTED_ANY[]} extracted bundles
234-
*/
235-
(stats) => extractBundles(normalizeStats(stats, resultStatsOptions)),
232+
if ("stats" in statsResult) {
233+
bundles = statsResult.stats.flatMap((stats) =>
234+
extractBundles(normalizeStats(stats, resultStatsOptions)),
236235
);
237236
} else {
238237
bundles = extractBundles(normalizeStats(statsResult, resultStatsOptions));
@@ -259,7 +258,7 @@ function publishStats(action, statsResult, eventStream, log, statsOptions) {
259258
hash: stats.hash,
260259
warnings: formatErrors(stats.warnings || []),
261260
errors: formatErrors(stats.errors || []),
262-
modules: buildModuleMap(stats.modules),
261+
modules: buildModuleMap(stats.modules || []),
263262
});
264263
}
265264
}

types/hot.d.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@ declare namespace createHot {
3030
MultiCompiler,
3131
Stats,
3232
MultiStats,
33+
StatsCompilation,
34+
StatsError,
35+
StatsModule,
3336
IncomingMessage,
3437
ServerResponse,
35-
EXPECTED_ANY,
38+
StatsOptions,
3639
HotOptions,
3740
Payload,
3841
EventStream,
@@ -43,15 +46,18 @@ declare const HOT_DEFAULT_HEARTBEAT: number;
4346
/** @typedef {import("webpack").MultiCompiler} MultiCompiler */
4447
/** @typedef {import("webpack").Stats} Stats */
4548
/** @typedef {import("webpack").MultiStats} MultiStats */
49+
/** @typedef {import("webpack").StatsCompilation} StatsCompilation */
50+
/** @typedef {import("webpack").StatsError} StatsError */
51+
/** @typedef {import("webpack").StatsModule} StatsModule */
4652
/** @typedef {import("./index.js").IncomingMessage} IncomingMessage */
4753
/** @typedef {import("./index.js").ServerResponse} ServerResponse */
48-
/** @typedef {any} EXPECTED_ANY */
54+
/** @typedef {NonNullable<import("webpack").Configuration["stats"]>} StatsOptions */
4955
/**
5056
* @typedef {object} HotOptions
5157
* @property {string=} path the path the SSE endpoint is served at
5258
* @property {number=} heartbeat heartbeat interval in milliseconds
5359
* @property {((message: string) => void) | false=} log logger
54-
* @property {EXPECTED_ANY=} statsOptions webpack stats options used when serializing compilation results
60+
* @property {StatsOptions=} statsOptions webpack stats options used when serializing compilation results
5561
*/
5662
/**
5763
* @typedef {object} Payload
@@ -71,22 +77,20 @@ declare const HOT_DEFAULT_HEARTBEAT: number;
7177
*/
7278
declare const HOT_DEFAULT_PATH: "/__webpack_hmr";
7379
/**
74-
* @param {EXPECTED_ANY[]} modules modules
80+
* @param {StatsModule[]} modules modules
7581
* @returns {Record<string, string>} module id to name map
7682
*/
77-
declare function buildModuleMap(
78-
modules: EXPECTED_ANY[],
79-
): Record<string, string>;
83+
declare function buildModuleMap(modules: StatsModule[]): Record<string, string>;
8084
/**
8185
* @param {number} heartbeat heartbeat interval in milliseconds
8286
* @returns {EventStream} event stream
8387
*/
8488
declare function createEventStream(heartbeat: number): EventStream;
8589
/**
86-
* @param {EXPECTED_ANY[]} errors errors or warnings
90+
* @param {(string | StatsError)[]} errors errors or warnings
8791
* @returns {string[]} flat strings
8892
*/
89-
declare function formatErrors(errors: EXPECTED_ANY[]): string[];
93+
declare function formatErrors(errors: (string | StatsError)[]): string[];
9094
/**
9195
* @param {string | undefined} url url
9296
* @param {string} expected expected pathname
@@ -98,14 +102,14 @@ declare function pathMatch(url: string | undefined, expected: string): boolean;
98102
* @param {Stats | MultiStats} statsResult stats result
99103
* @param {EventStream} eventStream event stream
100104
* @param {((message: string) => void) | false} log logger or false to disable
101-
* @param {EXPECTED_ANY} statsOptions stats options
105+
* @param {StatsOptions | undefined} statsOptions stats options
102106
*/
103107
declare function publishStats(
104108
action: string,
105109
statsResult: Stats | MultiStats,
106110
eventStream: EventStream,
107111
log: ((message: string) => void) | false,
108-
statsOptions: EXPECTED_ANY,
112+
statsOptions: StatsOptions | undefined,
109113
): void;
110114
type HotInstance = {
111115
/**
@@ -135,9 +139,12 @@ type Compiler = import("webpack").Compiler;
135139
type MultiCompiler = import("webpack").MultiCompiler;
136140
type Stats = import("webpack").Stats;
137141
type MultiStats = import("webpack").MultiStats;
142+
type StatsCompilation = import("webpack").StatsCompilation;
143+
type StatsError = import("webpack").StatsError;
144+
type StatsModule = import("webpack").StatsModule;
138145
type IncomingMessage = import("./index.js").IncomingMessage;
139146
type ServerResponse = import("./index.js").ServerResponse;
140-
type EXPECTED_ANY = any;
147+
type StatsOptions = NonNullable<import("webpack").Configuration["stats"]>;
141148
type HotOptions = {
142149
/**
143150
* the path the SSE endpoint is served at
@@ -154,7 +161,7 @@ type HotOptions = {
154161
/**
155162
* webpack stats options used when serializing compilation results
156163
*/
157-
statsOptions?: EXPECTED_ANY | undefined;
164+
statsOptions?: StatsOptions | undefined;
158165
};
159166
type Payload = {
160167
/**

0 commit comments

Comments
 (0)