-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
feat(chunk): add chunk typing support #7031
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
lib/Chunk.js
Outdated
@@ -388,7 +542,10 @@ class Chunk { | |||
}; | |||
} | |||
|
|||
hasModuleInGraph(filterFn, filterChunkFn) { | |||
hasModuleInGraph( | |||
/** @type {(m: Module) => boolean} */ filterFn, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be more consistent for these to be @param
annotations on the method, instead of @type
on the param?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I agree
@@ -1,5 +1,7 @@ | |||
"use strict"; | |||
|
|||
//TODO: Make this a generic type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I just wait on this to land. Thats my thought.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll work on it tomorrow
lib/Chunk.js
Outdated
const getArray = set => Array.from(set); | ||
|
||
/** | ||
* @param {Set} set the Set to get the count of | ||
* @return {number} the count from the items in the set |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
count -> size
maby also change the variable name to reflect this.
lib/Chunk.js
Outdated
//Blocked by https://github.com/Microsoft/TypeScript/issues/23375 | ||
/** @typedef {typeof import("./Module.js")} Module */ | ||
/** @typedef {typeof import("./ChunkGroup")} ChunkGroup */ | ||
/** @typedef {typeof import("./ModuleReason.js")} ModuleReason */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move to top
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
lib/Chunk.js
Outdated
@@ -312,7 +466,7 @@ class Chunk { | |||
return this.addMultiplierAndOverhead(integratedModulesSize, options); | |||
} | |||
|
|||
sortModules(sortByFn) { | |||
sortModules(/** @type {(a: Module, b: Module) => -1|0|1} */ sortByFn) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use normal jsdoc.
Functions need to be declared as typedef to work with jsdoc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
lib/Chunk.js
Outdated
@@ -388,7 +542,10 @@ class Chunk { | |||
}; | |||
} | |||
|
|||
hasModuleInGraph(filterFn, filterChunkFn) { | |||
hasModuleInGraph( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
lib/Chunk.js
Outdated
constructor(name) { | ||
this.id = null; | ||
this.ids = null; | ||
this.debugId = debugId++; | ||
this.name = name; | ||
this.entryModule = undefined; | ||
//TODO make these typed generics for Module[] and ChunkGroup[] and their sort being (T, T): => 1,-1,0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see #7046
lib/Chunk.js
Outdated
class Chunk { | ||
/** | ||
* @param {string=} name of chunk being created, is optional (for subclasses) | ||
*/ | ||
constructor(name) { | ||
this.id = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
every member should have a @type
comment (and optionally @private
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
@TheLarkInn Thanks for your update. I labeled the Pull Request so reviewers will review it again. @sokra Please review the new changes. |
lib/Chunk.js
Outdated
@@ -12,18 +12,61 @@ const ERR_CHUNK_ENTRY = "Chunk.entry was removed. Use hasRuntime()"; | |||
const ERR_CHUNK_INITIAL = | |||
"Chunk.initial was removed. Use canBeInitial/isOnlyInitial()"; | |||
|
|||
//TODO: Use actual Module type instead of "faked" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be changed now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're waiting on microsoft/TypeScript#23570 to be reviewed, iterated, merged, and shipped. I think the associated issue's just marked fixed because we have a candidate fix ready.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's merged now and will be in typescript@next tomorrow.
I have corrected |
lib/Chunk.js
Outdated
addMultiplierAndOverhead(size, options) { | ||
const overhead = | ||
typeof options.chunkOverhead === "number" ? options.chunkOverhead : 10000; | ||
options.chunkOverhead === "number" ? options.chunkOverhead : 10000; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This removal of typeof looks incorrect. Is it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh yup this is the cause, thanks for seeing that! I hadn't gotten back to this yet. Thanks! Correcting.
This looks ready now. |
d3df941
to
62d7737
Compare
Rebased to remove noise, tests rerunning, and removed two more stray comments. Should be good now. |
Thank you for your pull request! The most important CI builds succeeded, we’ll review the pull request soon. |
lib/Chunk.js
Outdated
*/ | ||
|
||
/** @typedef {Object} WithId an object who has an id property | ||
* @property {string} id - the id of the object |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think -
prefix in description is extra
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely
lib/Chunk.js
Outdated
}; | ||
|
||
/** | ||
* @class |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets remove extra @class
, @name
and @description
directives for consistency.
get entry() { | ||
throw new Error(ERR_CHUNK_ENTRY); | ||
} | ||
|
||
/** | ||
* @deprecated .entry has been deprecated. Please use .hasRuntime() instead | ||
* @param {never} data The data that was attempting to be set |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is type any
or *
in JSDoc syntax
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually never mind! I see what you're trying to do here! This is clever! Type system will complain if this setter is ever called.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup exactly!
lib/Chunk.js
Outdated
this.extraAsync = false; | ||
} | ||
|
||
/** | ||
* @deprecated Chunk.entry has been deprecated. Please use .hasRuntime() instead | ||
* @return {never} Throws an error trying to access this property |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@returns
lib/Chunk.js
Outdated
@@ -121,42 +233,74 @@ class Chunk { | |||
return false; | |||
} | |||
|
|||
/** | |||
* @return {void} set new modules to this chunk and return nothing | |||
* @param {Module[]} modules the new modules to be set |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move @param
to the top
Alright had to rebase, going to correct these now |
@ooflorent @mohsen1 can I get re-reviews. |
lib/Chunk.js
Outdated
*/ | ||
|
||
/** | ||
* @typedef {(a: Module, b: Module) => -1|0|1} ModuleSortPredicate |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: this can be single line
lib/Chunk.js
Outdated
@@ -34,51 +75,103 @@ const getModulesIdent = set => { | |||
return str; | |||
}; | |||
|
|||
/** | |||
* @param {Set} set the set to convert to array | |||
* @returns {Array} the array returned from Array.from(set) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this have to be a generic?
fd9b32e
to
ef3279e
Compare
Rebased master again, addressed @mohsen1's nits. Will wait on CI since we rebased just in case. |
ef3279e
to
f1618ae
Compare
All checks pass going to merge. |
const sortByIdentifier = (a, b) => { | ||
if (a.identifier() > b.identifier()) return 1; | ||
if (a.identifier() < b.identifier()) return -1; | ||
return 0; | ||
}; | ||
|
||
/** | ||
* @returns {string} a concatenation of module identifiers sorted | ||
* @param {SortableSet} set to pull module identifiers from |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mohsen1 Cannot find the typedef
for SortableSet
. Is it not needed here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's imported on top of the file
What kind of change does this PR introduce?
Refactoring, Type Annotations
Did you add tests for your changes?
N/A
If relevant, link to documentation update:
N/A
N/A
Summary
This adds more (but not complete) typing to Chunk.js.
Chunk is one of many base Types that could value from being completely typed and referenced in other modules, etc. It is also heavily used across plugins in our ecosystem, and having these annotations will assist in authoring of plugins and other integrations.
Does this PR introduce a breaking change?
No
Other information
Also helped discover the following TypeScript bugs: