Skip to content

Commit 98e638a

Browse files
refactor(types): type TupleSet keys as a tuple and reuse MagicCommentOptions
1 parent 33f3194 commit 98e638a

10 files changed

Lines changed: 53 additions & 49 deletions

‎lib/Compilation.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6748,7 +6748,7 @@ Object.defineProperty(compilationPrototype, "cache", {
67486748
set: util.deprecate(
67496749
/**
67506750
* Handles the value callback for this hook.
6751-
* @param {EXPECTED_ANY} _v value
6751+
* @param {Cache} _v value
67526752
*/
67536753
(_v) => {},
67546754
"Compilation.cache was removed in favor of Compilation.getCache()",

‎lib/css/CssParser.js‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ const getCommentCompilationWarning = memoize(() =>
9191
* CssAutoOrModuleParserOptions
9292
* } from "../../declarations/WebpackOptions"
9393
*/
94+
/** @import { MagicCommentOptions } from "../util/magicComment" */
9495

9596
/** @typedef {[number, number]} Range */
9697
/** @typedef {{ line: number, column: number }} Position */
@@ -1019,7 +1020,7 @@ class CssParser extends Parser {
10191020
* `CssUrlDependency`. Magic comments win over the project-wide default.
10201021
* @param {CssUrlDependency} dep dep
10211022
* @param {string} request asset request
1022-
* @param {Record<string, EXPECTED_ANY> | null | undefined} options parsed comment options
1023+
* @param {MagicCommentOptions | null | undefined} options parsed comment options
10231024
* @param {import("../graph/Dependency").DependencyLocation} loc location for warnings
10241025
* @returns {void}
10251026
*/
@@ -1252,10 +1253,10 @@ class CssParser extends Parser {
12521253
* @param {Range} range byte range to scan for magic comments
12531254
* @param {number} warnStart start offset of the loc for an invalid-`webpackIgnore` warning (computed lazily)
12541255
* @param {number} warnEnd end offset of that loc
1255-
* @returns {{ ignored: boolean, options: Record<string, EXPECTED_ANY> | null }} parsed options and `webpackIgnore` flag
1256+
* @returns {{ ignored: boolean, options: MagicCommentOptions | null }} parsed options and `webpackIgnore` flag
12561257
*/
12571258
const magicCommentsIn = (range, warnStart, warnEnd) => {
1258-
/** @type {{ options: Record<string, EXPECTED_ANY> | null, errors: (Error & { comment: Comment })[] | null }} */
1259+
/** @type {{ options: MagicCommentOptions | null, errors: (Error & { comment: Comment })[] | null }} */
12591260
const { options, errors } = parseCommentOptionsInRange(
12601261
/** @type {(Comment & { range: [number, number], value: string })[]} */ (
12611262
comments

‎lib/hmr/HotModuleReplacementPlugin.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -410,11 +410,11 @@ class HotModuleReplacementPlugin {
410410
records.chunkModuleIds[chunkId] = moduleIds;
411411
}
412412
});
413-
/** @type {TupleSet<Module, Chunk>} */
413+
/** @type {TupleSet<[Module], Chunk>} */
414414
const updatedModules = new TupleSet();
415-
/** @type {TupleSet<Module, Chunk>} */
415+
/** @type {TupleSet<[Module], Chunk>} */
416416
const fullHashModules = new TupleSet();
417-
/** @type {TupleSet<Module, RuntimeSpec>} */
417+
/** @type {TupleSet<[Module], RuntimeSpec>} */
418418
const nonCodeGeneratedModules = new TupleSet();
419419
compilation.hooks.fullHash.tap(PLUGIN_NAME, (hash) => {
420420
const chunkGraph = compilation.chunkGraph;

‎lib/html/HtmlParser.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ const getCommentCompilationWarning = memoize(() =>
8181
/** @typedef {(source: string, context: HtmlTemplateContext) => string} HtmlTemplateFunction */
8282

8383
/** @import { ParsedSource } from "./syntax-parser" */
84+
/** @import { MagicCommentOptions } from "../util/magicComment" */
8485

8586
// Cheap pre-filter for a `style="..."` attribute: only route it through the
8687
// CSS pipeline when it can hold a URL-bearing function (`url()` / `src()` /
@@ -1253,7 +1254,7 @@ class HtmlParser extends Parser {
12531254
pendingHints = undefined;
12541255
return;
12551256
}
1256-
/** @type {Record<string, EXPECTED_ANY>} */
1257+
/** @type {MagicCommentOptions} */
12571258
let options;
12581259
try {
12591260
options = parseMagicComment(value, magicCommentContext);

‎lib/index.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ const lazyFunction = (factory) => {
133133
const f = /** @type {unknown} */ (
134134
/**
135135
* Handles the callback logic for this hook.
136-
* @param {...EXPECTED_ANY} args args
137-
* @returns {T} result
136+
* @param {Parameters<T>} args args
137+
* @returns {ReturnType<T>} result
138138
*/
139139
(...args) => fac()(...args)
140140
);

‎lib/optimize/FlagDependencyUsagePlugin.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ class FlagDependencyUsagePlugin {
7777
/** @type {Map<ExportsInfo, Module>} */
7878
const exportInfoToModuleMap = new Map();
7979

80-
/** @type {TupleQueue<Module, RuntimeSpec>} */
80+
/** @type {TupleQueue<[Module], RuntimeSpec>} */
8181
const queue = new TupleQueue();
8282

83-
/** @type {TupleSet<Module, RuntimeSpec> | undefined} */
83+
/** @type {TupleSet<[Module], RuntimeSpec> | undefined} */
8484
let walkedSideEffectFreeModules;
8585

8686
/**

‎lib/optimize/TupleQueue.js‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@ const TupleSet = require("../util/TupleSet");
1010
/**
1111
* FIFO queue for tuples that preserves uniqueness by delegating membership
1212
* tracking to `TupleSet`.
13-
* @template T
13+
* @template {EXPECTED_ANY[]} K
1414
* @template V
1515
*/
1616
class TupleQueue {
1717
/**
1818
* Seeds the queue with an optional iterable of tuples to visit.
19-
* @param {Iterable<[T, V, ...EXPECTED_ANY]>=} items The initial elements.
19+
* @param {Iterable<[...K, V]>=} items The initial elements.
2020
*/
2121
constructor(items) {
2222
/**
2323
* @private
24-
* @type {TupleSet<T, V>}
24+
* @type {TupleSet<K, V>}
2525
*/
2626
this._set = new TupleSet(items);
2727
/**
2828
* @private
29-
* @type {Iterator<[T, V, ...EXPECTED_ANY]>}
29+
* @type {Iterator<[...K, V]>}
3030
*/
3131
this._iterator = this._set[Symbol.iterator]();
3232
}
@@ -41,7 +41,7 @@ class TupleQueue {
4141

4242
/**
4343
* Enqueues a tuple if it is not already present in the underlying set.
44-
* @param {[T, V, ...EXPECTED_ANY]} item The element to add.
44+
* @param {[...K, V]} item The element to add.
4545
* @returns {void}
4646
*/
4747
enqueue(...item) {
@@ -51,22 +51,22 @@ class TupleQueue {
5151
/**
5252
* Removes and returns the next queued tuple, rebuilding the iterator when
5353
* the underlying tuple set has changed since the last full pass.
54-
* @returns {[T, V, ...EXPECTED_ANY] | undefined} The head of the queue of `undefined` if this queue is empty.
54+
* @returns {[...K, V] | undefined} The head of the queue of `undefined` if this queue is empty.
5555
*/
5656
dequeue() {
5757
const result = this._iterator.next();
5858
if (result.done) {
5959
if (this._set.size > 0) {
6060
this._iterator = this._set[Symbol.iterator]();
6161
const value =
62-
/** @type {[T, V, ...EXPECTED_ANY]} */
62+
/** @type {[...K, V]} */
6363
(this._iterator.next().value);
6464
this._set.delete(...value);
6565
return value;
6666
}
6767
return;
6868
}
69-
this._set.delete(.../** @type {[T, V, ...EXPECTED_ANY]} */ (result.value));
69+
this._set.delete(.../** @type {[...K, V]} */ (result.value));
7070
return result.value;
7171
}
7272
}

‎lib/prefetch/ResourceHintPlugin.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const parseResourceHintOptions = require("./parseResourceHintOptions");
6060
*/
6161
/** @import { HtmlResourceHint } from "../dependencies/html/HtmlEntryDependency" */
6262
/** @import { ResourceHintOptions } from "./parseResourceHintOptions" */
63+
/** @import { MagicCommentOptions } from "../util/magicComment" */
6364

6465
/**
6566
* @typedef {object} ResolvedResourceHints
@@ -735,7 +736,7 @@ class ResourceHintPlugin {
735736
* @param {ResourceHintDep} dep dep to mutate in place
736737
* @param {UrlHintRule[] | undefined} rules parser-scoped `urlHints` rules
737738
* @param {string} request the asset request (for rule matching)
738-
* @param {Record<string, EXPECTED_ANY> | null | undefined} commentOptions parsed magic-comment options (`null` / `undefined` skips override phase)
739+
* @param {MagicCommentOptions | null | undefined} commentOptions parsed magic-comment options (`null` / `undefined` skips override phase)
739740
* @param {import("../module/NormalModule")} module module for emitting warnings on invalid comments
740741
* @param {import("../graph/Dependency").DependencyLocation} loc loc for warnings
741742
* @returns {void}

‎lib/prefetch/parseResourceHintOptions.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const getUnsupportedFeatureWarning = memoize(() =>
1212

1313
/** @import { DependencyLocation } from "../graph/Dependency" */
1414
/** @import NormalModule from "../module/NormalModule" */
15+
/** @import { MagicCommentOptions } from "../util/magicComment" */
1516

1617
/**
1718
* @typedef {object} ResourceHintOptions
@@ -27,7 +28,7 @@ const getUnsupportedFeatureWarning = memoize(() =>
2728
* Reads `webpackPrefetch` / `webpackPreload` / `webpackFetchPriority` /
2829
* `webpackAs` / `webpackType` / `webpackMedia` from a parsed magic-comment
2930
* options object. Invalid values emit warnings to the given module.
30-
* @param {Record<string, EXPECTED_ANY> | null | undefined} importOptions parsed comment options
31+
* @param {MagicCommentOptions | null | undefined} importOptions parsed comment options
3132
* @param {NormalModule} module the module to attach warnings to
3233
* @param {DependencyLocation} loc location for warnings
3334
* @returns {ResourceHintOptions} parsed hints (empty when no comments matched)

‎lib/util/TupleSet.js‎

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@
1616
/**
1717
* Stores tuples of arbitrary length while preserving efficient prefix lookups
1818
* through a tree of maps that ends in a set of final values.
19-
* @template T
19+
* @template {EXPECTED_ANY[]} K
2020
* @template V
2121
*/
2222
class TupleSet {
2323
/**
2424
* Seeds the tuple set with an optional iterable of tuples.
25-
* @param {Iterable<[T, V, ...EXPECTED_ANY]>=} init init
25+
* @param {Iterable<[...K, V]>=} init init
2626
*/
2727
constructor(init) {
28-
/** @type {InnerMap<T, V>} */
28+
/** @type {InnerMap<K[number], V>} */
2929
this._map = new Map();
3030
/** @type {number} */
3131
this.size = 0;
@@ -38,80 +38,80 @@ class TupleSet {
3838

3939
/**
4040
* Adds a tuple to the set, creating any missing prefix maps along the way.
41-
* @param {[T, V, ...EXPECTED_ANY]} args tuple
41+
* @param {[...K, V]} args tuple
4242
* @returns {void}
4343
*/
4444
add(...args) {
4545
let map = this._map;
4646
for (let i = 0; i < args.length - 2; i++) {
47-
const arg = args[i];
47+
const arg = /** @type {K[number]} */ (args[i]);
4848
const innerMap = map.get(arg);
4949
if (innerMap === undefined) {
5050
map.set(arg, (map = new Map()));
5151
} else {
52-
map = /** @type {InnerMap<T, V>} */ (innerMap);
52+
map = /** @type {InnerMap<K[number], V>} */ (innerMap);
5353
}
5454
}
5555

56-
const beforeLast = args[args.length - 2];
56+
const beforeLast = /** @type {K[number]} */ (args[args.length - 2]);
5757
let set = /** @type {Set<V>} */ (map.get(beforeLast));
5858
if (set === undefined) {
5959
map.set(beforeLast, (set = new Set()));
6060
}
6161

62-
const last = args[args.length - 1];
62+
const last = /** @type {V} */ (args[args.length - 1]);
6363
this.size -= set.size;
6464
set.add(last);
6565
this.size += set.size;
6666
}
6767

6868
/**
6969
* Checks whether the exact tuple is already present in the set.
70-
* @param {[T, V, ...EXPECTED_ANY]} args tuple
70+
* @param {[...K, V]} args tuple
7171
* @returns {boolean} true, if the tuple is in the Set
7272
*/
7373
has(...args) {
7474
let map = this._map;
7575
for (let i = 0; i < args.length - 2; i++) {
76-
const arg = args[i];
77-
map = /** @type {InnerMap<T, V>} */ (map.get(arg));
76+
const arg = /** @type {K[number]} */ (args[i]);
77+
map = /** @type {InnerMap<K[number], V>} */ (map.get(arg));
7878
if (map === undefined) {
7979
return false;
8080
}
8181
}
8282

83-
const beforeLast = args[args.length - 2];
84-
const set = map.get(beforeLast);
83+
const beforeLast = /** @type {K[number]} */ (args[args.length - 2]);
84+
const set = /** @type {Set<V> | undefined} */ (map.get(beforeLast));
8585
if (set === undefined) {
8686
return false;
8787
}
8888

89-
const last = args[args.length - 1];
89+
const last = /** @type {V} */ (args[args.length - 1]);
9090
return set.has(last);
9191
}
9292

9393
/**
9494
* Removes a tuple from the set when it is present.
95-
* @param {[T, V, ...EXPECTED_ANY]} args tuple
95+
* @param {[...K, V]} args tuple
9696
* @returns {void}
9797
*/
9898
delete(...args) {
9999
let map = this._map;
100100
for (let i = 0; i < args.length - 2; i++) {
101-
const arg = args[i];
102-
map = /** @type {InnerMap<T, V>} */ (map.get(arg));
101+
const arg = /** @type {K[number]} */ (args[i]);
102+
map = /** @type {InnerMap<K[number], V>} */ (map.get(arg));
103103
if (map === undefined) {
104104
return;
105105
}
106106
}
107107

108-
const beforeLast = args[args.length - 2];
109-
const set = map.get(beforeLast);
108+
const beforeLast = /** @type {K[number]} */ (args[args.length - 2]);
109+
const set = /** @type {Set<V> | undefined} */ (map.get(beforeLast));
110110
if (set === undefined) {
111111
return;
112112
}
113113

114-
const last = args[args.length - 1];
114+
const last = /** @type {V} */ (args[args.length - 1]);
115115
this.size -= set.size;
116116
set.delete(last);
117117
this.size += set.size;
@@ -120,7 +120,7 @@ class TupleSet {
120120
/**
121121
* Iterates over every stored tuple by walking the nested map structure and
122122
* yielding each complete prefix plus its terminal set value.
123-
* @returns {Iterator<[T, V, ...EXPECTED_ANY]>} iterator
123+
* @returns {Iterator<[...K, V]>} iterator
124124
*/
125125
[Symbol.iterator]() {
126126
/**
@@ -131,17 +131,17 @@ class TupleSet {
131131

132132
// This is difficult to type because we can have a map inside a map inside a map, etc. where the end is a set (each key is an argument)
133133
// But in basic use we only have 2 arguments in our methods, so we have `Map<K, Set<V>>`
134-
/** @type {IteratorStack<T, V>[]} */
134+
/** @type {IteratorStack<K[number], V>[]} */
135135
const iteratorStack = [];
136-
/** @type {[T?, V?, ...EXPECTED_ANY]} */
136+
/** @type {(K[number] | V)[]} */
137137
const tuple = [];
138138
/** @type {SetIterator<V> | undefined} */
139139
let currentSetIterator;
140140

141141
/**
142142
* Advances through nested maps until a terminal value set is reached or
143143
* every remaining branch has been exhausted.
144-
* @param {IteratorStack<T, V>} it iterator
144+
* @param {IteratorStack<K[number], V>} it iterator
145145
* @returns {boolean} result
146146
*/
147147
const next = (it) => {
@@ -150,7 +150,7 @@ class TupleSet {
150150
if (iteratorStack.length === 0) return false;
151151
tuple.pop();
152152
return next(
153-
/** @type {IteratorStack<T, V>} */
153+
/** @type {IteratorStack<K[number], V>} */
154154
(iteratorStack.pop())
155155
);
156156
}
@@ -174,7 +174,7 @@ class TupleSet {
174174
tuple.pop();
175175
if (
176176
!next(
177-
/** @type {IteratorStack<T, V>} */
177+
/** @type {IteratorStack<K[number], V>} */
178178
(iteratorStack.pop())
179179
)
180180
) {
@@ -185,7 +185,7 @@ class TupleSet {
185185
done: false,
186186
value:
187187
/* eslint-disable unicorn/prefer-spread */
188-
/** @type {[T, V, ...EXPECTED_ANY]} */
188+
/** @type {[...K, V]} */
189189
(tuple.concat(result.value))
190190
};
191191
}

0 commit comments

Comments
 (0)