-
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 remaining _.get()
from core-data
#48310
Conversation
Size Change: +321 B (0%) Total Size: 1.33 MB
ℹ️ View Unchanged
|
@@ -66,7 +66,10 @@ function getQueriedItemsUncached( state, query ) { | |||
|
|||
for ( let f = 0; f < fields.length; f++ ) { | |||
const field = fields[ f ].split( '.' ); | |||
const value = get( item, field ); | |||
let value = { ...item }; |
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 don't think we need to clone the object 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.
My rationale was that if we directly assign item
to value
by reference, when we change value
on L71, we'll also change item
, which may eventually lead to unexpected results if we end up using item
somewhere.
Or are you talking about the fact that item
is not used after this potential mutation in this branch of the code, so we might as well mutate 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.
my rationale here is that "field" contains at least one element, so there's no way value is going to be item in the end, or am I wrong?
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 you have a point. Removed in f00713e
packages/core-data/src/selectors.ts
Outdated
@@ -323,7 +332,10 @@ export const getEntityRecord = createSelector( | |||
const fields = getNormalizedCommaSeparable( query._fields ) ?? []; | |||
for ( let f = 0; f < fields.length; f++ ) { | |||
const field = fields[ f ].split( '.' ); | |||
const value = get( item, field ); | |||
let value = { ...item }; |
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 cloning doesn't seem 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.
See my rationale above. Also, item
is used later here so we definitely don't want to inadvertently mutate 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.
even if it's used later, value is never going to be equal to "item" after the loop
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.
👍 Removed in f00713e
Flaky tests detected in 4116122. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4242801768
|
What?
This PR removes Lodash's
_.get()
from the@wordpress/core-data
package. It also adds some missing types whose lack was previously concealed by the Lodash's broad types.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 using direct access with optional chaining and nullish coalescing as an alternative. We're also adding some additional types where necessary.
Testing Instructions