Skip to content

Commit e8f9334

Browse files
refactor: annotate constructor fields with @type to improve type coverage (#21265)
Add explicit JSDoc @type annotations to class field assignments across lib/. At the first assignment site TypeScript widens the property to any (circular inference), so these identifiers counted as uncovered even though the field's resolved type is concrete. Annotating them with their already-inferred type lifts type coverage from 97.94% to 98.19% without changing the generated types.d.ts.
1 parent 99671f3 commit e8f9334

182 files changed

Lines changed: 502 additions & 4 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

lib/CacheFacade.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class MultiItemCache {
3939
* @param {ItemCacheFacade[]} items item caches
4040
*/
4141
constructor(items) {
42+
/** @type {ItemCacheFacade[]} */
4243
this._items = items;
4344
// @ts-expect-error expected - returns the single ItemCacheFacade when passed an array of length 1
4445
// eslint-disable-next-line no-constructor-return
@@ -110,8 +111,11 @@ class ItemCacheFacade {
110111
* @param {Etag | null} etag the etag
111112
*/
112113
constructor(cache, name, etag) {
114+
/** @type {Cache} */
113115
this._cache = cache;
116+
/** @type {string} */
114117
this._name = name;
118+
/** @type {Etag | null} */
115119
this._etag = etag;
116120
}
117121

@@ -215,8 +219,11 @@ class CacheFacade {
215219
* @param {HashFunction=} hashFunction the hash function to use
216220
*/
217221
constructor(cache, name, hashFunction) {
222+
/** @type {Cache} */
218223
this._cache = cache;
224+
/** @type {string} */
219225
this._name = name;
226+
/** @type {HashFunction | undefined} */
220227
this._hashFunction = hashFunction;
221228
}
222229

lib/ChunkGraph.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ class ChunkGraph {
315315
/** @type {ModuleGraph} */
316316
this.moduleGraph = moduleGraph;
317317

318+
/** @type {HashFunction} */
318319
this._hashFunction = hashFunction;
319320

320321
this._getGraphRoots = this._getGraphRoots.bind(this);

lib/Compilation.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,7 @@ class Compilation {
594594
* @param {CompilationParams} params the compilation parameters
595595
*/
596596
constructor(compiler, params) {
597+
/** @type {boolean} */
597598
this._backCompat = compiler._backCompat;
598599

599600
const getNormalModuleLoader = () => deprecatedNormalModuleLoaderHook(this);
@@ -1154,6 +1155,7 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
11541155
this.inputFileSystem =
11551156
/** @type {InputFileSystem} */
11561157
(compiler.inputFileSystem);
1158+
/** @type {FileSystemInfo} */
11571159
this.fileSystemInfo = new FileSystemInfo(this.inputFileSystem, {
11581160
unmanagedPaths: compiler.unmanagedPaths,
11591161
managedPaths: compiler.managedPaths,
@@ -1172,7 +1174,9 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
11721174
}
11731175
/** @type {ValueCacheVersions} */
11741176
this.valueCacheVersions = new Map();
1177+
/** @type {RequestShortener} */
11751178
this.requestShortener = compiler.requestShortener;
1179+
/** @type {string} */
11761180
this.compilerPath = compiler.compilerPath;
11771181

11781182
this.logger = this.getLogger("webpack.Compilation");
@@ -1187,9 +1191,13 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
11871191
/** @type {boolean} */
11881192
this.profile = (options && options.profile) || false;
11891193

1194+
/** @type {CompilationParams} */
11901195
this.params = params;
1196+
/** @type {MainTemplate} */
11911197
this.mainTemplate = new MainTemplate(this.outputOptions, this);
1198+
/** @type {ChunkTemplate} */
11921199
this.chunkTemplate = new ChunkTemplate(this.outputOptions, this);
1200+
/** @type {RuntimeTemplate} */
11931201
this.runtimeTemplate = new RuntimeTemplate(
11941202
this,
11951203
this.outputOptions,
@@ -1365,8 +1373,11 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
13651373
)
13661374
};
13671375

1376+
/** @type {CacheFacade} */
13681377
this._modulesCache = this.getCache("Compilation/modules");
1378+
/** @type {CacheFacade} */
13691379
this._assetsCache = this.getCache("Compilation/assets");
1380+
/** @type {CacheFacade} */
13701381
this._codeGenerationCache = this.getCache("Compilation/codeGeneration");
13711382

13721383
const unsafeCache = options.module.unsafeCache;

lib/Compiler.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,15 +324,19 @@ class Compiler {
324324

325325
this.options = options;
326326

327+
/** @type {string} */
327328
this.context = context;
328329

330+
/** @type {RequestShortener} */
329331
this.requestShortener = new RequestShortener(context, this.root);
330332

333+
/** @type {Cache} */
331334
this.cache = new Cache();
332335

333336
/** @type {Map<Module, ModuleMemCachesItem> | undefined} */
334337
this.moduleMemCaches = undefined;
335338

339+
/** @type {string} */
336340
this.compilerPath = "";
337341

338342
/** @type {boolean} */
@@ -344,6 +348,7 @@ class Compiler {
344348
/** @type {boolean} */
345349
this.watchMode = false;
346350

351+
/** @type {boolean} */
347352
this._backCompat = this.options.experiments.backCompat !== false;
348353

349354
/** @type {Compilation | undefined} */

lib/ConcatenationScope.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class ConcatenationScope {
4343
* @param {Set<string>} usedNames all used names
4444
*/
4545
constructor(modulesMap, currentModule, usedNames) {
46+
/** @type {ConcatenatedModuleInfo} */
4647
this._currentModule = currentModule;
4748
if (Array.isArray(modulesMap)) {
4849
/** @type {Map<Module, ConcatenatedModuleInfo>} */
@@ -52,7 +53,9 @@ class ConcatenationScope {
5253
}
5354
modulesMap = map;
5455
}
56+
/** @type {Set<string>} */
5557
this.usedNames = usedNames;
58+
/** @type {Map<Module, ModuleInfo>} */
5659
this._modulesMap = modulesMap;
5760
}
5861

lib/ContextExclusionPlugin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class ContextExclusionPlugin {
1414
* @param {RegExp} negativeMatcher Matcher regular expression
1515
*/
1616
constructor(negativeMatcher) {
17+
/** @type {RegExp} */
1718
this.negativeMatcher = negativeMatcher;
1819
}
1920

lib/ContextModule.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,17 @@ class ContextModule extends Module {
173173
/** @type {ResolveDependencies | undefined} */
174174
this.resolveDependencies = resolveDependencies;
175175
if (options && options.resolveOptions !== undefined) {
176+
/** @type {ResolveOptions | undefined} */
176177
this.resolveOptions = options.resolveOptions;
177178
}
178179

179180
if (options && typeof options.mode !== "string") {
180181
throw new Error("options.mode is a required option");
181182
}
182183

184+
/** @type {string} */
183185
this._identifier = this._createIdentifier();
186+
/** @type {boolean} */
184187
this._forceBuild = true;
185188
}
186189

lib/ContextReplacementPlugin.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class ContextReplacementPlugin {
3232
newContentRecursive,
3333
newContentRegExp
3434
) {
35+
/** @type {RegExp} */
3536
this.resourceRegExp = resourceRegExp;
3637

3738
// new webpack.ContextReplacementPlugin(/selector/, (context) => { /* Logic */ });
@@ -43,6 +44,7 @@ class ContextReplacementPlugin {
4344
typeof newContentResource === "string" &&
4445
typeof newContentRecursive === "object"
4546
) {
47+
/** @type {string | undefined} */
4648
this.newContentResource = newContentResource;
4749
/**
4850
* Stores new content create context map.
@@ -79,9 +81,11 @@ class ContextReplacementPlugin {
7981
this.newContentResource =
8082
/** @type {string | undefined} */
8183
(newContentResource);
84+
/** @type {boolean | undefined} */
8285
this.newContentRecursive =
8386
/** @type {boolean | undefined} */
8487
(newContentRecursive);
88+
/** @type {RegExp | undefined} */
8589
this.newContentRegExp =
8690
/** @type {RegExp | undefined} */
8791
(newContentRegExp);

lib/Dependency.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,13 @@ class Dependency {
133133
// stays on base: also set on ContextDependency, which is not a ModuleDependency
134134
/** @type {boolean | undefined} */
135135
this.optional = false;
136+
/** @type {number} */
136137
this._locSL = 0;
138+
/** @type {number} */
137139
this._locSC = 0;
140+
/** @type {number} */
138141
this._locEL = 0;
142+
/** @type {number} */
139143
this._locEC = 0;
140144
/** @type {undefined | number} */
141145
this._locI = undefined;

lib/EntryPlugin.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ class EntryPlugin {
2020
* @param {EntryOptions | string=} options entry options (passing a string is deprecated)
2121
*/
2222
constructor(context, entry, options) {
23+
/** @type {string} */
2324
this.context = context;
25+
/** @type {string} */
2426
this.entry = entry;
2527
this.options = options || "";
2628
}

0 commit comments

Comments
 (0)