Skip to content

Add SortableSet types#7513

Merged
sokra merged 9 commits intowebpack:masterfrom
mohsen1:SortableSet-types
Jun 20, 2018
Merged

Add SortableSet types#7513
sokra merged 9 commits intowebpack:masterfrom
mohsen1:SortableSet-types

Conversation

@mohsen1
Copy link
Copy Markdown
Contributor

@mohsen1 mohsen1 commented Jun 9, 2018

What kind of change does this PR introduce?
Types

Did you add tests for your changes?
N/A

Does this PR introduce a breaking change?
N/A

What needs to be documented once your changes are merged?
N/A

@webpack-bot
Copy link
Copy Markdown
Contributor

For maintainers only:

  • This need to be documented (issue in webpack/webpack.js.org will be filed when merged)

Comment thread lib/Chunk.js Outdated
/** @private @type {SortableSet<Module>} */
this._modules = new SortableSet(undefined, sortByIdentifier);
/** @private */
/** @private @type {SortableSet<ChunkGroup | WithId>} */
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is | WithId needed? This should be a Set with only ChunkGroups

Comment thread lib/util/SortableSet.js Outdated
* Create a new sortable set
* @param {Array<T>=} initialIterable The initial iterable value
* @param {SortFunction=} defaultSort Default sorting function
* @typedef {(a: T, b: T) => number} SortFunction
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put the typedef in a separate comment.

Actually => is not valid jsdoc. You should use @callback or {function(T, T): number}

Comment thread lib/util/SortableSet.js Outdated
this.sortWith(this._sortFn);
}

// eslint-disable-next-line valid-jsdoc
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same. Use @callback or function(SortableSet<T>): R

Comment thread lib/util/SortableSet.js
* @param {(instance: SortableSet<T>) => TODO} fn function to calculate value
* @returns {TODO} returns result of fn(this), cached until set changes
*/
getFromCache(fn) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can use @template R for the return type.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think fn should actually be function(SortableSet<T>): T, and the return type of getFromCache should be T. That's because the type of data is T because the type of _cache is Map<Function, T>.

Comment thread lib/util/SortableSet.js
* @param {Function} fn - function to calculate value
* @returns {TODO} - returns result of fn(this), cached until set changes
* @param {Function} fn function to calculate value
* @returns {any} returns result of fn(this), cached until set changes
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same. @template R

@webpack-bot
Copy link
Copy Markdown
Contributor

@mohsen1 Thanks for your update.

I labeled the Pull Request so reviewers will review it again.

@sokra Please review the new changes.

@mohsen1
Copy link
Copy Markdown
Contributor Author

mohsen1 commented Jun 19, 2018

@sokra According to the types, we might be using sortById function to compare ChunkGroups but ChunkGroup has no id member. Is that an actual bug?

Comment thread lib/util/SortableSet.js Outdated
* @returns {TODO} - returns result of fn(this), cached until set changes
* Get data from cache
* @param {function(SortableSet<T>): T[]} fn function to calculate value
* @returns {TODO} returns result of fn(this), cached until set changes
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can return T from here and getFromUnorderCache, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I think T[] is more accurate. It blow up in some other place. Fun to see code inconsistencies when you add types!

@sokra
Copy link
Copy Markdown
Member

sokra commented Jun 20, 2018

ChunkGroup has no id member. Is that an actual bug?

ChunkGroup has an id. But only a getter.

webpack/lib/ChunkGroup.js

Lines 121 to 127 in a1e79ab

/**
* get a unique id for ChunkGroup, made up of its member Chunk id's
* @returns {string} a unique concatenation of chunk ids
*/
get id() {
return Array.from(this.chunks, x => x.id).join("+");
}

Is it possible to change WithId to only expect a id getter?

Comment thread lib/Template.js Outdated
source.add("[]");
return source;
}
/** @type {Module[]} */
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not correct. The type is {id,source}[].

I think you don't even need a annotation here, typescript should infer the type automatically.

Comment thread lib/util/SortableSet.js Outdated
* @template T item type in set
*/
class SortableSet extends Set {
// eslint-disable-next-line valid-jsdoc
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the eslint-disable comment

@sokra
Copy link
Copy Markdown
Member

sokra commented Jun 20, 2018

hmm

@webpack-bot
Copy link
Copy Markdown
Contributor

Thank you for your pull request! The most important CI builds succeeded, we’ll review the pull request soon.

@sokra sokra merged commit 5c014a2 into webpack:master Jun 20, 2018
@sokra
Copy link
Copy Markdown
Member

sokra commented Jun 20, 2018

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants