Skip to content

Commit fa20888

Browse files
refactor: fix types in hot
1 parent 284e71b commit fa20888

10 files changed

+67
-3
lines changed

hot/dev-server.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
*/
55
/* globals __webpack_hash__ */
66
if (module.hot) {
7+
/** @type {undefined|string} */
78
var lastHash;
89
var upToDate = function upToDate() {
9-
return lastHash.indexOf(__webpack_hash__) >= 0;
10+
return /** @type {string} */ (lastHash).indexOf(__webpack_hash__) >= 0;
1011
};
1112
var log = require("./log");
1213
var check = function check() {

hot/lazy-compilation-node.js

+10
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33
"use strict";
44

55
var urlBase = decodeURIComponent(__resourceQuery.slice(1));
6+
7+
/**
8+
* @param {{ data: string, onError: (err: Error) => void, active: boolean, module: module }} options options
9+
* @returns {() => void} function to destroy response
10+
*/
611
exports.keepAlive = function (options) {
712
var data = options.data;
813
var onError = options.onError;
914
var active = options.active;
1015
var module = options.module;
16+
/** @type {import("http").IncomingMessage} */
1117
var response;
1218
var request = (
1319
urlBase.startsWith("https") ? require("https") : require("http")
@@ -27,6 +33,10 @@ exports.keepAlive = function (options) {
2733
}
2834
}
2935
);
36+
37+
/**
38+
* @param {Error} err error
39+
*/
3040
function errorHandler(err) {
3141
err.message =
3242
"Problem communicating active modules to the server: " + err.message;

hot/lazy-compilation-web.js

+9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ if (typeof EventSource !== "function") {
99
}
1010

1111
var urlBase = decodeURIComponent(__resourceQuery.slice(1));
12+
/** @type {EventSource | undefined} */
1213
var activeEventSource;
1314
var activeKeys = new Map();
1415
var errorHandlers = new Set();
@@ -19,6 +20,10 @@ var updateEventSource = function updateEventSource() {
1920
activeEventSource = new EventSource(
2021
urlBase + Array.from(activeKeys.keys()).join("@")
2122
);
23+
/**
24+
* @this {EventSource}
25+
* @param {Event & { message?: string, filename?: string, lineno?: number, colno?: number, error?: Error }} event event
26+
*/
2227
activeEventSource.onerror = function (event) {
2328
errorHandlers.forEach(function (onError) {
2429
onError(
@@ -42,6 +47,10 @@ var updateEventSource = function updateEventSource() {
4247
}
4348
};
4449

50+
/**
51+
* @param {{ data: string, onError: (err: Error) => void, active: boolean, module: module }} options options
52+
* @returns {() => void} function to destroy response
53+
*/
4554
exports.keepAlive = function (options) {
4655
var data = options.data;
4756
var onError = options.onError;

hot/log-apply-result.js

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5+
6+
/**
7+
* @param {(string | number)[]} updatedModules updated modules
8+
* @param {(string | number)[] | null} renewedModules renewed modules
9+
*/
510
module.exports = function (updatedModules, renewedModules) {
611
var unacceptedModules = updatedModules.filter(function (moduleId) {
712
return renewedModules && renewedModules.indexOf(moduleId) < 0;

hot/log.js

+22
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
/** @typedef {"info" | "warning" | "error"} LogLevel */
2+
3+
/** @type {LogLevel} */
14
var logLevel = "info";
25

36
function dummy() {}
47

8+
/**
9+
* @param {LogLevel} level log level
10+
* @returns {boolean} true, if should log
11+
*/
512
function shouldLog(level) {
613
var shouldLog =
714
(logLevel === "info" && level === "info") ||
@@ -10,6 +17,10 @@ function shouldLog(level) {
1017
return shouldLog;
1118
}
1219

20+
/**
21+
* @param {(msg?: string) => void} logFn log function
22+
* @returns {(level: LogLevel, msg?: string) => void} function that logs when log level is sufficient
23+
*/
1324
function logGroup(logFn) {
1425
return function (level, msg) {
1526
if (shouldLog(level)) {
@@ -18,6 +29,10 @@ function logGroup(logFn) {
1829
};
1930
}
2031

32+
/**
33+
* @param {LogLevel} level log level
34+
* @param {string|Error} msg message
35+
*/
2136
module.exports = function (level, msg) {
2237
if (shouldLog(level)) {
2338
if (level === "info") {
@@ -42,10 +57,17 @@ module.exports.groupCollapsed = logGroup(groupCollapsed);
4257

4358
module.exports.groupEnd = logGroup(groupEnd);
4459

60+
/**
61+
* @param {LogLevel} level log level
62+
*/
4563
module.exports.setLogLevel = function (level) {
4664
logLevel = level;
4765
};
4866

67+
/**
68+
* @param {Error} err error
69+
* @returns {string} formatted error
70+
*/
4971
module.exports.formatError = function (err) {
5072
var message = err.message;
5173
var stack = err.stack;

hot/only-dev-server.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
*/
55
/*globals __webpack_hash__ */
66
if (module.hot) {
7+
/** @type {undefined|string} */
78
var lastHash;
89
var upToDate = function upToDate() {
9-
return lastHash.indexOf(__webpack_hash__) >= 0;
10+
return /** @type {string} */ (lastHash).indexOf(__webpack_hash__) >= 0;
1011
};
1112
var log = require("./log");
1213
var check = function check() {

hot/poll.js

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ if (module.hot) {
77
var hotPollInterval = +__resourceQuery.slice(1) || 10 * 60 * 1000;
88
var log = require("./log");
99

10+
/**
11+
* @param {boolean=} fromUpdate true when called from update
12+
*/
1013
var checkForUpdate = function checkForUpdate(fromUpdate) {
1114
if (module.hot.status() === "idle") {
1215
module.hot

hot/signal.js

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
/*globals __resourceQuery */
66
if (module.hot) {
77
var log = require("./log");
8+
9+
/**
10+
* @param {boolean=} fromUpdate true when called from update
11+
*/
812
var checkForUpdate = function checkForUpdate(fromUpdate) {
913
module.hot
1014
.check()

test/typesCases/hot/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,10 @@ module.hot.apply({
1616
ignoreDeclined: true,
1717
ignoreErrored: true,
1818
}).then(() => {});
19+
module.hot.apply({
20+
ignoreUnaccepted: true,
21+
ignoreDeclined: true,
22+
ignoreErrored: true,
23+
onDeclined: (event) => { console.log(event.moduleId) },
24+
}).then(() => {});
1925
module.hot.apply().then(() => {});

tsconfig.module.test.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@
1010
"types": ["node", "./module"],
1111
"esModuleInterop": true
1212
},
13-
"include": ["test/typesCases/**/*"]
13+
"include": [
14+
"hot/**/*",
15+
"test/typesCases/**/*"
16+
]
1417
}

0 commit comments

Comments
 (0)