Skip to content

Commit 1d39cce

Browse files
committed
Type SortableSet
1 parent e2fe200 commit 1d39cce

File tree

7 files changed

+66
-35
lines changed

7 files changed

+66
-35
lines changed

.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ module.exports = {
4141
"preferType": {
4242
"*": "any"
4343
},
44-
"requireReturnType": true
44+
"requireReturnType": true,
45+
requireReturnDescription: false
4546
}],
4647
"node/no-unsupported-features": "error",
4748
"node/no-deprecated-api": "error",

lib/Chunk.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,22 @@ const ERR_CHUNK_INITIAL =
1818
/** @typedef {import("./ModuleReason.js")} ModuleReason */
1919
/** @typedef {import("webpack-sources").Source} Source */
2020

21+
/** @typedef {(a: Module, b: Module) => -1|0|1} ModuleSortPredicate */
22+
/** @typedef {(m: Module) => boolean} ModuleFilterPredicate */
23+
/** @typedef {(c: Chunk) => boolean} ChunkFilterPredicate */
24+
2125
/**
2226
* @typedef {Object} Identifiable an object who contains an identifier function property
2327
* @property {() => string} identifier the resource or unique identifier of something
2428
*/
2529

2630
/**
2731
* @typedef {Object} WithId an object who has an id property
28-
* @property {string} id the id of the object
32+
* @property {string | number} id the id of the object
2933
*/
3034

31-
/** @typedef {(a: Module, b: Module) => -1|0|1} ModuleSortPredicate */
32-
/** @typedef {(m: Module) => boolean} ModuleFilterPredicate */
33-
/** @typedef {(c: Chunk) => boolean} ChunkFilterPredicate */
34-
3535
/**
36+
* Compare two objects based on their ids for sorting
3637
* @param {WithId} a object that contains an ID property
3738
* @param {WithId} b object that contains an ID property
3839
* @returns {-1|0|1} sort value
@@ -44,9 +45,9 @@ const sortById = (a, b) => {
4445
};
4546

4647
/**
47-
*
48-
* @param {Identifiable} a first object with ident fn
49-
* @param {Identifiable} b second object with ident fn
48+
* Compare two Identifiables , based on their ids for sorting
49+
* @param {Module} a first object with ident fn
50+
* @param {Module} b second object with ident fn
5051
* @returns {-1|0|1} The order number of the sort
5152
*/
5253
const sortByIdentifier = (a, b) => {
@@ -108,11 +109,9 @@ class Chunk {
108109
this.preventIntegration = false;
109110
/** @type {Module=} */
110111
this.entryModule = undefined;
111-
//TODO make these typed generics for Module[] and ChunkGroup[] and their sort being (T, T): => 1,-1,0
112-
//See https://github.com/webpack/webpack/pull/7046
113-
/** @private */
112+
/** @private @type {SortableSet<Module>} */
114113
this._modules = new SortableSet(undefined, sortByIdentifier);
115-
/** @private */
114+
/** @private @type {SortableSet<ChunkGroup | WithId>} */
116115
this._groups = new SortableSet(undefined, sortById);
117116
/** @type {Source[]} */
118117
this.files = [];

lib/ChunkGroup.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ const getArray = set => Array.from(set);
2626

2727
/**
2828
* A convenience method used to sort chunks based on their id's
29-
* @param {HasId} a first sorting comparitor
30-
* @param {HasId} b second sorting comparitor
29+
* @param {HasId} a first sorting comparator
30+
* @param {HasId} b second sorting comparator
3131
* @returns {1|0|-1} a sorting index to determine order
3232
*/
3333
const sortById = (a, b) => {
@@ -37,8 +37,8 @@ const sortById = (a, b) => {
3737
};
3838

3939
/**
40-
* @param {OriginRecord} a the first comparitor in sort
41-
* @param {OriginRecord} b the second comparitor in sort
40+
* @param {OriginRecord} a the first comparator in sort
41+
* @param {OriginRecord} b the second comparator in sort
4242
* @returns {1|-1|0} returns sorting order as index
4343
*/
4444
const sortOrigin = (a, b) => {

lib/Module.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class Module extends DependenciesBlock {
6565
// Graph (per Compilation)
6666
/** @type {ModuleReason[]} */
6767
this.reasons = [];
68-
/** @type {SortableSet} */
68+
/** @type {SortableSet<Chunk>} */
6969
this._chunks = new SortableSet(undefined, sortById);
7070

7171
// Info from Compilation (per Compilation)

lib/util/SortableSet.js

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
11
"use strict";
2-
//TODO: Make this a generic type
3-
//https://github.com/Microsoft/TypeScript/issues/23385
4-
//https://github.com/Microsoft/TypeScript/issues/23384
2+
3+
/**
4+
* A subset of Set that offers sorting functionality
5+
* @template T item type in set
6+
*/
57
class SortableSet extends Set {
8+
// eslint-disable-next-line valid-jsdoc
9+
/**
10+
* Create a new sortable set
11+
* @param {Array<T>=} initialIterable The initial iterable value
12+
* @param {SortFunction=} defaultSort Default sorting function
13+
* @typedef {(a: T, b: T) => number} SortFunction
14+
*/
615
constructor(initialIterable, defaultSort) {
716
super(initialIterable);
17+
/** @private @type {(a: T, b: T) => number}} */
818
this._sortFn = defaultSort;
19+
/** @private @type {(a: T, b: T) => number} | null} */
920
this._lastActiveSortFn = null;
21+
/** @private @type {Map<Function, T> | undefined} */
1022
this._cache = undefined;
23+
/** @private @type {Map<Function, T> | undefined} */
1124
this._cacheOrderIndependent = undefined;
1225
}
1326

1427
/**
15-
* @param {TODO} value - value to add to set
16-
* @returns {this} - returns itself
28+
* @param {T} value value to add to set
29+
* @returns {this} returns itself
1730
*/
1831
add(value) {
1932
this._lastActiveSortFn = null;
@@ -23,19 +36,31 @@ class SortableSet extends Set {
2336
return this;
2437
}
2538

39+
/**
40+
* @param {T} value value to delete
41+
* @returns {boolean} true if value existed in set, false otherwise
42+
*/
2643
delete(value) {
2744
this._invalidateCache();
2845
this._invalidateOrderedCache();
2946
return super.delete(value);
3047
}
3148

49+
/**
50+
* @returns {void}
51+
*/
3252
clear() {
3353
this._invalidateCache();
3454
this._invalidateOrderedCache();
3555
return super.clear();
3656
}
3757

38-
sortWith(/** @type {(a: TODO, b: TODO) => number} */ sortFn) {
58+
/**
59+
* Sort with a comparer function
60+
* @param {SortFunction} sortFn Sorting comparer function
61+
* @returns {void}
62+
*/
63+
sortWith(sortFn) {
3964
if (this.size <= 1 || sortFn === this._lastActiveSortFn) {
4065
// already sorted - nothing to do
4166
return;
@@ -50,16 +75,14 @@ class SortableSet extends Set {
5075
this._invalidateCache();
5176
}
5277

53-
/**
54-
* @returns {void}
55-
*/
5678
sort() {
5779
this.sortWith(this._sortFn);
5880
}
5981

82+
// eslint-disable-next-line valid-jsdoc
6083
/**
61-
* @param {Function} fn - function to calculate value
62-
* @returns {TODO} - returns result of fn(this), cached until set changes
84+
* @param {(instance: SortableSet<T>) => TODO} fn function to calculate value
85+
* @returns {TODO} returns result of fn(this), cached until set changes
6386
*/
6487
getFromCache(fn) {
6588
if (this._cache === undefined) {
@@ -76,8 +99,8 @@ class SortableSet extends Set {
7699
}
77100

78101
/**
79-
* @param {Function} fn - function to calculate value
80-
* @returns {TODO} - returns result of fn(this), cached until set changes
102+
* @param {Function} fn function to calculate value
103+
* @returns {any} returns result of fn(this), cached until set changes
81104
*/
82105
getFromUnorderedCache(fn) {
83106
if (this._cacheOrderIndependent === undefined) {
@@ -93,12 +116,20 @@ class SortableSet extends Set {
93116
return newData;
94117
}
95118

119+
/**
120+
* @private
121+
* @returns {void}
122+
*/
96123
_invalidateCache() {
97124
if (this._cache !== undefined) {
98125
this._cache.clear();
99126
}
100127
}
101128

129+
/**
130+
* @private
131+
* @returns {void}
132+
*/
102133
_invalidateOrderedCache() {
103134
if (this._cacheOrderIndependent !== undefined) {
104135
this._cacheOrderIndependent.clear();

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"script-loader": "~0.7.0",
7171
"simple-git": "^1.65.0",
7272
"style-loader": "^0.19.1",
73-
"typescript": "^2.9.0-dev.20180518",
73+
"typescript": "^2.9.1",
7474
"url-loader": "^0.6.2",
7575
"val-loader": "^1.0.2",
7676
"vm-browserify": "~0.0.0",

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5984,9 +5984,9 @@ typedarray@^0.0.6:
59845984
version "0.0.6"
59855985
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
59865986

5987-
typescript@^2.9.0-dev.20180518:
5988-
version "2.9.0-dev.20180518"
5989-
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.9.0-dev.20180518.tgz#7c709cb5d59e60a5e346cc5856277fd7b8ced68c"
5987+
typescript@^2.9.1:
5988+
version "2.9.1"
5989+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.9.1.tgz#fdb19d2c67a15d11995fd15640e373e09ab09961"
59905990

59915991
ua-parser-js@^0.7.9:
59925992
version "0.7.17"

0 commit comments

Comments
 (0)