Skip to content

Commit 12d477d

Browse files
authored
Merge pull request webpack#17107 from webpack/some-types
refactor(type): small improve
2 parents 75b9e76 + 2cb6f6a commit 12d477d

14 files changed

+134
-2
lines changed

cspell.json

+1
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@
188188
"prewalking",
189189
"prioritise",
190190
"promisify",
191+
"proxied",
191192
"quasis",
192193
"queryloader",
193194
"querystrings",

lib/library/ModuleLibraryPlugin.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
8989
)}`;
9090
result.add(
9191
`var ${varName} = __webpack_exports__${propertyAccess([
92-
exportInfo.getUsedName(exportInfo.name, chunk.runtime)
92+
/** @type {string} */
93+
(exportInfo.getUsedName(exportInfo.name, chunk.runtime))
9394
])};\n`
9495
);
9596
exports.push(`${varName} as ${exportInfo.name}`);

lib/runtime/GetChunkFilenameRuntimeModule.js

+4
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ class GetChunkFilenameRuntimeModule extends RuntimeModule {
132132
const s = JSON.stringify(str);
133133
return s.slice(1, s.length - 1);
134134
};
135+
/**
136+
* @param {string} value string
137+
* @returns {function(number): string} string to put in quotes with length
138+
*/
135139
const unquotedStringifyWithLength = value => length =>
136140
unquotedStringify(`${value}`.slice(0, length));
137141
const chunkFilenameValue =

lib/schemes/DataUriPlugin.js

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ const NormalModule = require("../NormalModule");
1313
// http://www.ietf.org/rfc/rfc2397.txt
1414
const URIRegEx = /^data:([^;,]+)?((?:;[^;,]+)*?)(?:;(base64))?,(.*)$/i;
1515

16+
/**
17+
* @param {string} uri data URI
18+
* @returns {Buffer|null} decoded data
19+
*/
1620
const decodeDataURI = uri => {
1721
const match = URIRegEx.exec(uri);
1822
if (!match) return null;

lib/schemes/HttpUriPlugin.js

+38
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,31 @@ const validate = createSchemaValidation(
7171
}
7272
);
7373

74+
/**
75+
* @param {string} str path
76+
* @returns {string} safe path
77+
*/
7478
const toSafePath = str =>
7579
str
7680
.replace(/^[^a-zA-Z0-9]+|[^a-zA-Z0-9]+$/g, "")
7781
.replace(/[^a-zA-Z0-9._-]+/g, "_");
7882

83+
/**
84+
* @param {Buffer} content content
85+
* @returns {string} integrity
86+
*/
7987
const computeIntegrity = content => {
8088
const hash = createHash("sha512");
8189
hash.update(content);
8290
const integrity = "sha512-" + hash.digest("base64");
8391
return integrity;
8492
};
8593

94+
/**
95+
* @param {Buffer} content content
96+
* @param {string} integrity integrity
97+
* @returns {boolean} true, if integrity matches
98+
*/
8699
const verifyIntegrity = (content, integrity) => {
87100
if (integrity === "ignore") return true;
88101
return computeIntegrity(content) === integrity;
@@ -110,6 +123,11 @@ const parseKeyValuePairs = str => {
110123
return result;
111124
};
112125

126+
/**
127+
* @param {string | undefined} cacheControl Cache-Control header
128+
* @param {number} requestTime timestamp of request
129+
* @returns {{storeCache: boolean, storeLock: boolean, validUntil: number}} Logic for storing in cache and lockfile cache
130+
*/
113131
const parseCacheControl = (cacheControl, requestTime) => {
114132
// When false resource is not stored in cache
115133
let storeCache = true;
@@ -147,6 +165,10 @@ const areLockfileEntriesEqual = (a, b) => {
147165
);
148166
};
149167

168+
/**
169+
* @param {LockfileEntry} entry lockfile entry
170+
* @returns {`resolved: ${string}, integrity: ${string}, contentType: ${*}`} stringified entry
171+
*/
150172
const entryToString = entry => {
151173
return `resolved: ${entry.resolved}, integrity: ${entry.integrity}, contentType: ${entry.contentType}`;
152174
};
@@ -158,6 +180,10 @@ class Lockfile {
158180
this.entries = new Map();
159181
}
160182

183+
/**
184+
* @param {string} content content of the lockfile
185+
* @returns {Lockfile} lockfile
186+
*/
161187
static parse(content) {
162188
// TODO handle merge conflicts
163189
const data = JSON.parse(content);
@@ -180,6 +206,9 @@ class Lockfile {
180206
return lockfile;
181207
}
182208

209+
/**
210+
* @returns {string} stringified lockfile
211+
*/
183212
toString() {
184213
let str = "{\n";
185214
const entries = Array.from(this.entries).sort(([a], [b]) =>
@@ -342,6 +371,7 @@ class HttpUriPlugin {
342371
const fs = compilation.inputFileSystem;
343372
const cache = compilation.getCache("webpack.HttpUriPlugin");
344373
const logger = compilation.getLogger("webpack.HttpUriPlugin");
374+
/** @type {string} */
345375
const lockfileLocation =
346376
this._lockfileLocation ||
347377
join(
@@ -351,6 +381,7 @@ class HttpUriPlugin {
351381
? `${toSafePath(compiler.name)}.webpack.lock`
352382
: "webpack.lock"
353383
);
384+
/** @type {string | false} */
354385
const cacheLocation =
355386
this._cacheLocation !== undefined
356387
? this._cacheLocation
@@ -364,6 +395,7 @@ class HttpUriPlugin {
364395

365396
let warnedAboutEol = false;
366397

398+
/** @type {Map<string, string>} */
367399
const cacheKeyCache = new Map();
368400
/**
369401
* @param {string} url the url
@@ -447,6 +479,12 @@ class HttpUriPlugin {
447479

448480
/** @type {Map<string, LockfileEntry | "ignore" | "no-cache"> | undefined} */
449481
let lockfileUpdates = undefined;
482+
483+
/**
484+
* @param {Lockfile} lockfile lockfile instance
485+
* @param {string} url url to store
486+
* @param {LockfileEntry | "ignore" | "no-cache"} entry lockfile entry
487+
*/
450488
const storeLockEntry = (lockfile, url, entry) => {
451489
const oldEntry = lockfile.entries.get(url);
452490
if (lockfileUpdates === undefined) lockfileUpdates = new Map();

lib/stats/DefaultStatsPrinterPlugin.js

+25
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
const DATA_URI_CONTENT_LENGTH = 16;
1313
const MAX_MODULE_IDENTIFIER_LENGTH = 80;
1414

15+
/**
16+
* @param {number} n a number
17+
* @param {string} singular singular
18+
* @param {string} plural plural
19+
* @returns {string} if n is 1, singular, else plural
20+
*/
1521
const plural = (n, singular, plural) => (n === 1 ? singular : plural);
1622

1723
/**
@@ -29,6 +35,10 @@ const printSizes = (sizes, { formatSize = n => `${n}` }) => {
2935
}
3036
};
3137

38+
/**
39+
* @param {string} resource resource
40+
* @returns {string} resource name for display
41+
*/
3242
const getResourceName = resource => {
3343
const dataUrl = /^data:[^,]+,/.exec(resource);
3444
if (!dataUrl) return resource;
@@ -41,6 +51,10 @@ const getResourceName = resource => {
4151
)}..`;
4252
};
4353

54+
/**
55+
* @param {string} name module name
56+
* @returns {[string,string]} prefix and module name
57+
*/
4458
const getModuleName = name => {
4559
const [, prefix, resource] = /^(.*!)?([^!]*)$/.exec(name);
4660

@@ -59,6 +73,11 @@ const getModuleName = name => {
5973
return [prefix, getResourceName(resource)];
6074
};
6175

76+
/**
77+
* @param {string} str string
78+
* @param {function(string): string} fn function to apply to each line
79+
* @returns {string} joined string
80+
*/
6281
const mapLines = (str, fn) => str.split("\n").map(fn).join("\n");
6382

6483
/**
@@ -71,6 +90,12 @@ const isValidId = id => {
7190
return typeof id === "number" || id;
7291
};
7392

93+
/**
94+
* @template T
95+
* @param {Array<T>} list of items
96+
* @param {number} count number of items to show
97+
* @returns {string} string representation of list
98+
*/
7499
const moreCount = (list, count) => {
75100
return list && list.length > 0 ? `+ ${count}` : `${count}`;
76101
};

lib/util/StackedCacheMap.js

+6
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ class StackedCacheMap {
8383
this.map.clear();
8484
}
8585

86+
/**
87+
* @returns {number} size of the map
88+
*/
8689
get size() {
8790
let size = this.map.size;
8891
for (const map of this.stack) {
@@ -91,6 +94,9 @@ class StackedCacheMap {
9194
return size;
9295
}
9396

97+
/**
98+
* @returns {Iterator<[K, V]>} iterator
99+
*/
94100
[Symbol.iterator]() {
95101
const iterators = this.stack.map(map => map[Symbol.iterator]());
96102
let current = this.map[Symbol.iterator]();

lib/util/compileBooleanMatcher.js

+31
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,18 @@
55

66
"use strict";
77

8+
/**
9+
* @param {string} str string
10+
* @returns {string} quoted meta
11+
*/
812
const quoteMeta = str => {
913
return str.replace(/[-[\]\\/{}()*+?.^$|]/g, "\\$&");
1014
};
1115

16+
/**
17+
* @param {string} str string
18+
* @returns {string} string
19+
*/
1220
const toSimpleString = str => {
1321
if (`${+str}` === str) {
1422
return str;
@@ -49,19 +57,28 @@ const compileBooleanMatcherFromLists = (positiveItems, negativeItems) => {
4957
}
5058
};
5159

60+
/**
61+
* @param {Set<string>} itemsSet items set
62+
* @param {(str: string) => string | false} getKey get key function
63+
* @param {(str: Array<string>) => boolean} condition condition
64+
* @returns {Array<Array<string>>} list of common items
65+
*/
5266
const popCommonItems = (itemsSet, getKey, condition) => {
67+
/** @type {Map<string, Array<string>>} */
5368
const map = new Map();
5469
for (const item of itemsSet) {
5570
const key = getKey(item);
5671
if (key) {
5772
let list = map.get(key);
5873
if (list === undefined) {
74+
/** @type {Array<string>} */
5975
list = [];
6076
map.set(key, list);
6177
}
6278
list.push(item);
6379
}
6480
}
81+
/** @type {Array<Array<string>>} */
6582
const result = [];
6683
for (const list of map.values()) {
6784
if (condition(list)) {
@@ -74,6 +91,10 @@ const popCommonItems = (itemsSet, getKey, condition) => {
7491
return result;
7592
};
7693

94+
/**
95+
* @param {Array<string>} items items
96+
* @returns {string} common prefix
97+
*/
7798
const getCommonPrefix = items => {
7899
let prefix = items[0];
79100
for (let i = 1; i < items.length; i++) {
@@ -88,6 +109,10 @@ const getCommonPrefix = items => {
88109
return prefix;
89110
};
90111

112+
/**
113+
* @param {Array<string>} items items
114+
* @returns {string} common suffix
115+
*/
91116
const getCommonSuffix = items => {
92117
let suffix = items[0];
93118
for (let i = 1; i < items.length; i++) {
@@ -102,10 +127,15 @@ const getCommonSuffix = items => {
102127
return suffix;
103128
};
104129

130+
/**
131+
* @param {Array<string>} itemsArr array of items
132+
* @returns {string} regexp
133+
*/
105134
const itemsToRegexp = itemsArr => {
106135
if (itemsArr.length === 1) {
107136
return quoteMeta(itemsArr[0]);
108137
}
138+
/** @type {Array<string>} */
109139
const finishedItems = [];
110140

111141
// merge single char items: (a|b|c|d|ef) => ([abcd]|ef)
@@ -146,6 +176,7 @@ const itemsToRegexp = itemsArr => {
146176

147177
// special case for 2 items with common suffix
148178
if (finishedItems.length === 0 && items.size === 2) {
179+
/** @type {Iterator<string>} */
149180
const it = items[Symbol.iterator]();
150181
const a = it.next().value;
151182
const b = it.next().value;

lib/util/deprecation.js

+8
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,14 @@ exports.createArrayToSetDeprecationSet = name => {
178178
return SetDeprecatedArray;
179179
};
180180

181+
/**
182+
* @template T
183+
* @param {Object} obj object
184+
* @param {string} name property name
185+
* @param {string} code deprecation code
186+
* @param {string} note additional note
187+
* @returns {Object} frozen object with deprecation when modifying
188+
*/
181189
exports.soonFrozenObjectDeprecation = (obj, name, code, note = "") => {
182190
const message = `${name} will be frozen in future, all modifications are deprecated.${
183191
note && `\n${note}`

lib/util/identifier.js

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ const WINDOWS_PATH_SEPARATOR_REGEXP = /\\/g;
1515
* @property {Map<string, Map<string, string>>=} relativePaths
1616
*/
1717

18+
/**
19+
* @param {string} relativePath relative path
20+
* @returns {string} request
21+
*/
1822
const relativePathToRequest = relativePath => {
1923
if (relativePath === "") return "./.";
2024
if (relativePath === "..") return "../.";

lib/util/propertyAccess.js

+5
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ const RESERVED_IDENTIFIER = new Set([
6060
"false"
6161
]);
6262

63+
/**
64+
* @param {ArrayLike<string>} properties properties
65+
* @param {number} start start index
66+
* @returns {string} chain of property accesses
67+
*/
6368
const propertyAccess = (properties, start = 0) => {
6469
let str = "";
6570
for (let i = start; i < properties.length; i++) {

lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class AsyncWebAssemblyJavascriptGenerator extends Generator {
8585
}
8686
}
8787

88+
/** @type {Array<string>} */
8889
const promises = [];
8990

9091
const importStatements = Array.from(

lib/wasm-async/AsyncWebAssemblyParser.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class WebAssemblyParser extends Parser {
4747
// parse it
4848
const program = decode(source, decoderOpts);
4949
const module = program.body[0];
50-
50+
/** @type {Array<string>} */
5151
const exports = [];
5252
t.traverse(module, {
5353
ModuleExport({ node }) {

lib/wasm/EnableWasmLoadingPlugin.js

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
/** @type {WeakMap<Compiler, Set<WasmLoadingType>>} */
1313
const enabledTypes = new WeakMap();
1414

15+
/**
16+
* @param {Compiler} compiler compiler instance
17+
* @returns {Set<WasmLoadingType>} enabled types
18+
*/
1519
const getEnabledTypes = compiler => {
1620
let set = enabledTypes.get(compiler);
1721
if (set === undefined) {

0 commit comments

Comments
 (0)