Skip to content

Commit 1ae1c7e

Browse files
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.
1 parent dad8876 commit 1ae1c7e

5 files changed

Lines changed: 17 additions & 5 deletions

File tree

‎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/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/javascript/StackedMap.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ class StackedMap {
206206
layers.push(current);
207207
current = current.parent;
208208
} while (current !== undefined);
209-
/** @type {Map<K, V>} */
209+
/** @type {Map<K, InternalCell<V>>} */
210210
const map = new Map();
211211
for (let i = layers.length - 1; i >= 0; i--) {
212212
const layerMap = layers[i].map;
@@ -215,7 +215,7 @@ class StackedMap {
215215
if (pair[1] === TOMBSTONE) {
216216
map.delete(pair[0]);
217217
} else {
218-
map.set(pair[0], /** @type {V} */ (pair[1]));
218+
map.set(pair[0], pair[1]);
219219
}
220220
}
221221
}

‎lib/util/source.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const isSourceEqual = (a, b) => {
4747
if (cache1 !== undefined) {
4848
cache1.set(b, result);
4949
} else {
50+
/** @type {WeakMap<Source, boolean>} */
5051
const map = new WeakMap();
5152
map.set(b, result);
5253
equalityCache.set(a, map);
@@ -55,6 +56,7 @@ const isSourceEqual = (a, b) => {
5556
if (cache2 !== undefined) {
5657
cache2.set(a, result);
5758
} else {
59+
/** @type {WeakMap<Source, boolean>} */
5860
const map = new WeakMap();
5961
map.set(a, result);
6062
equalityCache.set(b, map);

‎tooling/type-coverage.js‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,17 +291,25 @@ for (const [sourceFile, typeChecker] of sourceFiles) {
291291

292292
/**
293293
* Tells whether a type names one of a signature's own type parameters,
294-
* other than one that defaults to `any` by its declaration.
294+
* other than one this project's source defaults to `any` on purpose;
295+
* lib's `new <T = any>()` of `Set` still counts.
295296
* @param {Set<ts.Type>} parameters the signature's type parameters
296297
* @param {ts.Type} type the declared type
297298
* @returns {boolean} true when it does
298299
*/
299300
const namesTypeParameter = (parameters, type) => {
300301
if (type.flags & ts.TypeFlags.TypeParameter) {
302+
if (!parameters.has(type)) return false;
301303
const defaultType = type.getDefault();
304+
if (!defaultType || !(defaultType.flags & ts.TypeFlags.Any)) {
305+
return true;
306+
}
307+
const declarations = type.symbol && type.symbol.getDeclarations();
302308
return (
303-
parameters.has(type) &&
304-
!(defaultType && defaultType.flags & ts.TypeFlags.Any)
309+
!declarations ||
310+
declarations.every(
311+
(declaration) => declaration.getSourceFile().isDeclarationFile
312+
)
305313
);
306314
}
307315
return (

0 commit comments

Comments
 (0)