Skip to content

Commit 3e778b8

Browse files
committed
fix minor type issues and linting
1 parent 04d2af0 commit 3e778b8

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

lib/Chunk.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,24 @@ const ERR_CHUNK_INITIAL =
2424
*/
2525

2626
/**
27-
* Compare two objects based on their ids for sorting
28-
* @param {WithId} a object that contains an ID property
29-
* @param {WithId} b object that contains an ID property
27+
* Compare two Modules based on their ids for sorting
28+
* @param {Module} a module
29+
* @param {Module} b module
3030
* @returns {-1|0|1} sort value
3131
*/
32-
const sortById = (a, b) => {
32+
const sortModuleById = (a, b) => {
33+
if (a.id < b.id) return -1;
34+
if (b.id < a.id) return 1;
35+
return 0;
36+
};
37+
38+
/**
39+
* Compare two ChunkGroups based on their ids for sorting
40+
* @param {ChunkGroup} a chunk group
41+
* @param {ChunkGroup} b chunk group
42+
* @returns {-1|0|1} sort value
43+
*/
44+
const sortChunkGroupById = (a, b) => {
3345
if (a.id < b.id) return -1;
3446
if (b.id < a.id) return 1;
3547
return 0;
@@ -103,7 +115,7 @@ class Chunk {
103115
/** @private @type {SortableSet<Module>} */
104116
this._modules = new SortableSet(undefined, sortByIdentifier);
105117
/** @private @type {SortableSet<ChunkGroup>} */
106-
this._groups = new SortableSet(undefined, sortById);
118+
this._groups = new SortableSet(undefined, sortChunkGroupById);
107119
/** @type {Source[]} */
108120
this.files = [];
109121
/** @type {boolean} */
@@ -492,7 +504,7 @@ class Chunk {
492504
* @returns {void}
493505
*/
494506
sortModules(sortByFn) {
495-
this._modules.sortWith(sortByFn || sortById);
507+
this._modules.sortWith(sortByFn || sortModuleById);
496508
}
497509

498510
sortItems() {

lib/ModuleTemplate.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
const { Tapable, SyncWaterfallHook, SyncHook } = require("tapable");
88

9+
/** @typedef {import("webpack-sources").Source} Source */
10+
/** @typedef {import("./Module")} Module */
11+
912
module.exports = class ModuleTemplate extends Tapable {
1013
constructor(runtimeTemplate, type) {
1114
super();
@@ -40,6 +43,12 @@ module.exports = class ModuleTemplate extends Tapable {
4043
};
4144
}
4245

46+
/**
47+
* @param {Module} module the module
48+
* @param {TODO} dependencyTemplates templates for dependencies
49+
* @param {TODO} options render options
50+
* @returns {Source} the source
51+
*/
4352
render(module, dependencyTemplates, options) {
4453
try {
4554
const moduleSource = module.source(

lib/Template.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,6 @@ const stringifyIdSortPredicate = (a, b) => {
4444
return 0;
4545
};
4646

47-
/**
48-
* @param {Module} module the module to compare against
49-
* @returns {boolean} return true if module.id is equal to type "number"
50-
*/
51-
const moduleIdIsNumber = module => {
52-
return typeof module.id === "number";
53-
};
54-
5547
class Template {
5648
/**
5749
*
@@ -177,18 +169,20 @@ class Template {
177169
}
178170

179171
/**
180-
*
181-
* @param {Module[]} modules a collection of modules to get array bounds for
172+
* @typedef {Object} WithId
173+
* @property {string|number} id
174+
*/
175+
176+
/**
177+
* @param {WithId[]} modules a collection of modules to get array bounds for
182178
* @returns {[number, number] | false} returns the upper and lower array bounds
183179
* or false if not every module has a number based id
184180
*/
185181
static getModulesArrayBounds(modules) {
186-
// Typeguards don't work for .every() with predicate functions
187-
// https://github.com/Microsoft/TypeScript/issues/23799
188-
if (!modules.every(moduleIdIsNumber)) return false;
189182
var maxId = -Infinity;
190183
var minId = Infinity;
191184
for (const module of modules) {
185+
if (typeof module.id !== "number") return false;
192186
if (maxId < module.id) maxId = /** @type {number} */ (module.id);
193187
if (minId > module.id) minId = /** @type {number} */ (module.id);
194188
}
@@ -236,7 +230,6 @@ class Template {
236230
source.add("[]");
237231
return source;
238232
}
239-
/** @type {Module[]} */
240233
var allModules = modules.map(module => {
241234
return {
242235
id: module.id,

0 commit comments

Comments
 (0)