Skip to content

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

Merged
merged 1 commit into from
May 4, 2018
Merged

Conversation

TheLarkInn
Copy link
Member

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:

@TheLarkInn TheLarkInn changed the title Feature/chunk type support feat(chunk): add chunk typing support Apr 13, 2018
lib/Chunk.js Outdated
@@ -388,7 +542,10 @@ class Chunk {
};
}

hasModuleInGraph(filterFn, filterChunkFn) {
hasModuleInGraph(
/** @type {(m: Module) => boolean} */ filterFn,
Copy link
Contributor

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?

Copy link
Member Author

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
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member Author

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.

Copy link
Contributor

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
Copy link
Member

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 */
Copy link
Member

Choose a reason for hiding this comment

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

Move to top

Copy link
Member Author

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) {
Copy link
Member

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

Copy link
Member Author

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(
Copy link
Member

Choose a reason for hiding this comment

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

Same

Copy link
Member Author

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
Copy link
Member

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;
Copy link
Member

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)

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@webpack-bot
Copy link
Contributor

@TheLarkInn Thanks for your update.

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

@sokra Please review the new changes.

@sokra sokra closed this Apr 23, 2018
@sokra sokra reopened this Apr 23, 2018
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"
Copy link
Member

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?

Copy link
Contributor

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.

Copy link
Contributor

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.

@TheLarkInn
Copy link
Member Author

I have corrected import() statements with the new xx24 update of TypeScript nightly and the bug @weswigham mentioning being fixed last night.

lib/Chunk.js Outdated
addMultiplierAndOverhead(size, options) {
const overhead =
typeof options.chunkOverhead === "number" ? options.chunkOverhead : 10000;
options.chunkOverhead === "number" ? options.chunkOverhead : 10000;
Copy link
Contributor

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?

Copy link
Member Author

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.

@TheLarkInn
Copy link
Member Author

This looks ready now.

@TheLarkInn TheLarkInn force-pushed the feature/chunk-type-support branch 3 times, most recently from d3df941 to 62d7737 Compare April 28, 2018 17:35
@TheLarkInn
Copy link
Member Author

Rebased to remove noise, tests rerunning, and removed two more stray comments. Should be good now.

@webpack-bot
Copy link
Contributor

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
Copy link
Contributor

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

Copy link
Member Author

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
Copy link
Contributor

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
Copy link
Contributor

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

Copy link
Contributor

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.

Copy link
Member Author

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
Copy link
Contributor

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
Copy link
Contributor

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

@TheLarkInn
Copy link
Member Author

Alright had to rebase, going to correct these now

@TheLarkInn
Copy link
Member Author

@ooflorent @mohsen1 can I get re-reviews.

lib/Chunk.js Outdated
*/

/**
* @typedef {(a: Module, b: Module) => -1|0|1} ModuleSortPredicate
Copy link
Contributor

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)
Copy link
Contributor

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?

@TheLarkInn TheLarkInn force-pushed the feature/chunk-type-support branch 3 times, most recently from fd9b32e to ef3279e Compare May 3, 2018 23:16
@TheLarkInn
Copy link
Member Author

Rebased master again, addressed @mohsen1's nits. Will wait on CI since we rebased just in case.

@TheLarkInn TheLarkInn force-pushed the feature/chunk-type-support branch from ef3279e to f1618ae Compare May 3, 2018 23:18
@TheLarkInn
Copy link
Member Author

All checks pass going to merge.

@TheLarkInn TheLarkInn merged commit e361ba5 into master May 4, 2018
@montogeek montogeek deleted the feature/chunk-type-support branch May 4, 2018 08:11
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
Copy link
Member

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?

Copy link
Contributor

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

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.

7 participants