Skip to content

Commit f57e0fc

Browse files
fix(tooling): count every escape hatch and inferred type argument (#23041)
* fix(tooling): count every escape hatch and inferred type argument Read escape hatches from the syntax tree instead of a regex over a list of JSDoc tags, which missed `@template` defaults, `@extends` and raw `any`/`object`/`Function`, and counted `unknown` inside string types. Count `declarations/` too. Count `new` and generic calls whose type arguments TypeScript fills with `any`, such as `new Map()`. * refactor(types): type the type arguments inferred as any * fix(tooling): count lib's default any type arguments A type parameter that lib declares with an `any` default, as `Set` and `WeakMap` do, still counts as inferred; only a default this project declares is a choice. Type the four calls it finds, and keep the StackedMap sentinel in its compressed map's type. * fix(watch): drop a running child's missing watching from MultiWatching When a child compiler is already running, its `watch()` reports the error and returns `undefined`. `MultiCompiler.watch` kept that entry, so every later `close`, `suspend`, `resume` or `invalidate` on the returned watching, `compiler.close()` included, threw a TypeError.
1 parent 4fdd148 commit f57e0fc

34 files changed

Lines changed: 348 additions & 72 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+
Keep `MultiCompiler` watching usable when a child compiler is already running.

‎hot/emitter.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
var EventEmitter = require("events");
2+
/** @type {EventEmitter} */
23
module.exports = new EventEmitter();

‎lib/Compilation.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3184,6 +3184,7 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
31843184
const buildInfo = module.buildInfo;
31853185
if (buildInfo) {
31863186
// create a new entry
3187+
/** @type {MemCache} */
31873188
const memCache = new WeakTupleMap();
31883189
moduleMemCacheCache.set(module, {
31893190
buildInfo,

‎lib/MultiCompiler.js‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,7 @@ module.exports = class MultiCompiler {
756756
}
757757
this.running = true;
758758

759+
/** @type {(Watching | undefined)[]} */
759760
const watchings = this._runGraph(
760761
(compiler, idx, callback, isBlocked, setChanged, setInvalid) => {
761762
const watching = compiler.watch(
@@ -769,8 +770,8 @@ module.exports = class MultiCompiler {
769770
}
770771
return watching;
771772
},
772-
(compiler, /** @type {Watching} */ watching, _callback) => {
773-
if (compiler.watching !== watching) return;
773+
(compiler, watching, _callback) => {
774+
if (watching === undefined || compiler.watching !== watching) return;
774775
if (!watching.running) watching.invalidate();
775776
},
776777
(err, stats) => {
@@ -779,7 +780,11 @@ module.exports = class MultiCompiler {
779780
handler(err, stats);
780781
}
781782
);
782-
this.watching = new MultiWatching(watchings, this);
783+
// A child already running has no watching and reported the error to `handler`
784+
this.watching = new MultiWatching(
785+
watchings.filter((watching) => watching !== undefined),
786+
this
787+
);
783788
return this.watching;
784789
}
785790

‎lib/cache/CacheFacade.js‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,12 @@ class MultiItemCache {
7373
* @returns {Promise<T | undefined>} promise with the data
7474
*/
7575
const next = (i) =>
76-
this._items[i].getPromise().then((/** @type {T} */ result) => {
77-
if (result !== undefined) return result;
78-
if (++i < this._items.length) return next(i);
79-
});
76+
/** @type {Promise<T | undefined>} */ (this._items[i].getPromise()).then(
77+
(result) => {
78+
if (result !== undefined) return result;
79+
if (++i < this._items.length) return next(i);
80+
}
81+
);
8082
return /** @type {Promise<T>} */ (next(0));
8183
}
8284

‎lib/config/cli.js‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,10 @@ const getObjectAndProperty = (config, schemaPath, index = 0) => {
555555
if (isArray) {
556556
if (value === undefined) {
557557
value = {};
558-
current[name] = [...Array.from({ length: index }), value];
558+
current[name] = [
559+
.../** @type {undefined[]} */ (Array.from({ length: index })),
560+
value
561+
];
559562
cliAddedItems.set(current[name], index + 1);
560563
} else if (!Array.isArray(value)) {
561564
return {
@@ -613,15 +616,18 @@ const getObjectAndProperty = (config, schemaPath, index = 0) => {
613616
const value = current[name];
614617
if (value === undefined) {
615618
/** @type {ConfigurationValue[]} */
616-
const array = [...Array.from({ length: index }), undefined];
619+
const array = [
620+
.../** @type {undefined[]} */ (Array.from({ length: index })),
621+
undefined
622+
];
617623
current[name] = array;
618624
cliAddedItems.set(array, index + 1);
619625
return { object: array, property: index, value: undefined };
620626
} else if (!Array.isArray(value)) {
621627
/** @type {ConfigurationValue[]} */
622628
const array = [
623629
/** @type {ConfigurationValue} */ (value),
624-
...Array.from({ length: index }),
630+
.../** @type {undefined[]} */ (Array.from({ length: index })),
625631
undefined
626632
];
627633
current[name] = array;

‎lib/css/syntax-parser.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9527,6 +9527,7 @@ const _evaluateCalcProduct = (tokens, cursor) => {
95279527
if (token.type === "/") {
95289528
const divisor = right.get("");
95299529
if (divisor === undefined || right.size !== 1) return null;
9530+
/** @type {Map<string, number>} */
95309531
const out = new Map();
95319532
for (const [key, coefficient] of sum) {
95329533
const quotient = foldDivide(coefficient, divisor);
@@ -9543,6 +9544,7 @@ const _evaluateCalcProduct = (tokens, cursor) => {
95439544
const factor = rightNumber !== undefined ? rightNumber : leftNumber;
95449545
const other = rightNumber !== undefined ? sum : right;
95459546
if (factor === undefined) return null;
9547+
/** @type {Map<string, number>} */
95469548
const out = new Map();
95479549
for (const [key, coefficient] of other) {
95489550
const product = foldMultiply(coefficient, factor);
@@ -10093,6 +10095,7 @@ const _neededPrefixes = (table, name) => {
1009310095
/** @type {Set<string> | null} */
1009410096
let result = null;
1009510097
if (prefixes !== undefined) {
10098+
/** @type {Set<string>} */
1009610099
const needed = new Set();
1009710100
const prefixWindows = getPrefixWindows();
1009810101
for (const [prefix, windows] of prefixes) {
@@ -16444,6 +16447,7 @@ const _propertiesOverlap = (a, b) => {
1644416447
* @returns {Set<string>} the property names, lowercased
1644516448
*/
1644616449
const _blockProperties = (body) => {
16450+
/** @type {Set<string>} */
1644716451
const out = new Set();
1644816452
let depth = 0;
1644916453
let quote = 0;

‎lib/define/DefinePlugin.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,7 @@ const getMergedDefinitions = (compilation) => {
620620
addToMap(finalByNestedKey, nested, final);
621621
addToMap(nestedByFinalKey, final, nested);
622622
}
623+
/** @type {MergedDefinitions} */
623624
const result = {
624625
compilation,
625626
definitions,

‎lib/dependencies/helpers/processExportInfo.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ const processExportInfo = (
4949
return;
5050
}
5151
// Only the recursive path needs the visited set; allocate it lazily
52+
/** @type {Set<InstanceType<ExportInfo>>} */
5253
const visited = alreadyVisited !== undefined ? alreadyVisited : new Set();
5354
visited.add(exportInfo);
5455
const exportsInfo = exportInfo.exportsInfo;

‎lib/fs/FileSystemInfo.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,9 @@ const INVALID = Symbol("invalid");
173173
* @property {boolean=} timestamp should use timestamp to snapshot
174174
*/
175175

176-
const DONE_ITERATOR_RESULT = new Set().keys().next();
176+
const DONE_ITERATOR_RESULT = /** @type {Set<never>} */ (new Set())
177+
.keys()
178+
.next();
177179

178180
// cspell:word tshs
179181
// Tsh = Timestamp + Hash
@@ -1381,20 +1383,23 @@ class FileSystemInfo {
13811383
(s, v) => s.setManagedItemInfo(v),
13821384
false
13831385
);
1386+
/** @type {SnapshotOptimization<string, true>} */
13841387
this._managedFilesOptimization = new SnapshotOptimization(
13851388
(s) => s.hasManagedFiles(),
13861389
(s) => s.managedFiles,
13871390
(s, v) => s.setManagedFiles(v),
13881391
false,
13891392
true
13901393
);
1394+
/** @type {SnapshotOptimization<string, true>} */
13911395
this._managedContextsOptimization = new SnapshotOptimization(
13921396
(s) => s.hasManagedContexts(),
13931397
(s) => s.managedContexts,
13941398
(s, v) => s.setManagedContexts(v),
13951399
false,
13961400
true
13971401
);
1402+
/** @type {SnapshotOptimization<string, true>} */
13981403
this._managedMissingOptimization = new SnapshotOptimization(
13991404
(s) => s.hasManagedMissing(),
14001405
(s) => s.managedMissing,

0 commit comments

Comments
 (0)