-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Lodash: Remove from block factory API #43560
Conversation
Size Change: +3 B (0%) Total Size: 1.24 MB
ℹ️ View Unchanged
|
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.
LGTM, great work @tyxla! 👍
@@ -163,14 +153,14 @@ export function cloneBlock( block, mergeAttributes = {}, newInnerBlocks ) { | |||
* @return {boolean} Is the transform possible? | |||
*/ | |||
const isPossibleTransformForSource = ( transform, direction, blocks ) => { | |||
if ( isEmpty( blocks ) ) { | |||
if ( ! blocks.length ) { |
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.
Adding a note that blocks could in theory be undefined
(in which case blocks?.length
would be preferred), but the rest of the code on this file already appears to assume that it will always be an array 👍
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.
Tests pass so it’s probably working correctly but extra safety won’t hurt. I believe that isEmpty
like most lodash methods was covering a few things including checking if we deal with an array in the first place.
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, this is a common problem with migrating away from Lodash, especially in untyped codebases 😕 It's hard to separate which checks we want to keep, and which ones are not needed. Implicit conversions are even worse.
Sometimes you get it wrong and create some bugs that need to be fixed, but the end result after all that is much better: not only are you not relying on a large lib anymore, but you're now explicit about your checks, which improves readability and maintainability. It's painful, but it's worth 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.
TBH cases like isEmpty
in combination with the untyped codebase makes it the ultimate hell for following through code and ensuring no bugs occur. By removing the extra ambiguity that isEmpty()
added, we solve one of the problems, and then when one day we type the entire codebase, things will look great!
What?
This PR removes Lodash usage from the block factory API.
Why?
Lodash is known to unnecessarily inflate the bundle size of packages, and in most cases, it can be replaced with native language functionality. See these for more information and rationale:
@wordpress/api-fetch
package haslodash
as a dependency #39495How?
We're replacing all Lodash usage with native functionality.
Testing Instructions
npm run test:unit packages/blocks