Skip to content
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: Refactor away from _.forEach() #43807

Merged
merged 1 commit into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ module.exports = {
'flatMap',
'flatten',
'flattenDeep',
'forEach',
'fromPairs',
'has',
'identity',
Expand Down
66 changes: 30 additions & 36 deletions packages/babel-plugin-makepot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*/

const { po } = require( 'gettext-parser' );
const { pick, reduce, forEach, isEqual, merge, isEmpty } = require( 'lodash' );
const { pick, reduce, isEqual, merge, isEmpty } = require( 'lodash' );
const { relative, sep } = require( 'path' );
const { writeFileSync } = require( 'fs' );

Expand Down Expand Up @@ -122,7 +122,7 @@ function getExtractedComment( path, _originalNodeLine ) {
}

let comment;
forEach( node.leadingComments, ( commentNode ) => {
Object.values( node.leadingComments ?? {} ).forEach( ( commentNode ) => {
let line = 0;
if ( commentNode && commentNode.loc && commentNode.loc.end ) {
line = commentNode.loc.end.line;
Expand Down Expand Up @@ -333,41 +333,35 @@ module.exports = () => {
strings[ file ][ context ]
);

forEach(
sortedTranslations,
( translation ) => {
const { msgctxt = '', msgid } =
translation;
if (
! memo.hasOwnProperty( msgctxt )
) {
memo[ msgctxt ] = {};
}

// Merge references if translation already exists.
if (
isSameTranslation(
translation,
memo[ msgctxt ][ msgid ]
)
) {
translation.comments.reference = [
...new Set(
[
memo[ msgctxt ][ msgid ]
.comments.reference,
translation.comments
.reference,
]
.join( '\n' )
.split( '\n' )
),
].join( '\n' );
}

memo[ msgctxt ][ msgid ] = translation;
sortedTranslations.forEach( ( translation ) => {
const { msgctxt = '', msgid } = translation;
if ( ! memo.hasOwnProperty( msgctxt ) ) {
memo[ msgctxt ] = {};
}
);

// Merge references if translation already exists.
if (
isSameTranslation(
translation,
memo[ msgctxt ][ msgid ]
)
) {
translation.comments.reference = [
...new Set(
[
memo[ msgctxt ][ msgid ]
.comments.reference,
translation.comments
.reference,
]
.join( '\n' )
.split( '\n' )
),
].join( '\n' );
}

memo[ msgctxt ][ msgid ] = translation;
} );
}

return memo;
Expand Down
13 changes: 2 additions & 11 deletions packages/block-editor/src/hooks/utils.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
/**
* External dependencies
*/
import {
pickBy,
isEmpty,
mapValues,
forEach,
get,
setWith,
clone,
every,
} from 'lodash';
import { pickBy, isEmpty, mapValues, get, setWith, clone, every } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -77,7 +68,7 @@ export function transformStyles(
}
}
let returnBlock = result;
forEach( activeSupports, ( isActive, support ) => {
Object.entries( activeSupports ).forEach( ( [ support, isActive ] ) => {
if ( isActive ) {
migrationPaths[ support ].forEach( ( path ) => {
const styleValue = get( referenceBlockAttributes, path );
Expand Down
14 changes: 2 additions & 12 deletions packages/block-library/src/gallery/v1/edit.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
/**
* External dependencies
*/
import {
every,
filter,
find,
forEach,
get,
isEmpty,
map,
reduce,
some,
} from 'lodash';
import { every, filter, find, get, isEmpty, map, reduce, some } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -346,7 +336,7 @@ function GalleryEdit( props ) {
every( images, ( { url } ) => isBlobURL( url ) )
) {
const filesList = map( images, ( { url } ) => getBlobByURL( url ) );
forEach( images, ( { url } ) => revokeBlobURL( url ) );
images.forEach( ( { url } ) => revokeBlobURL( url ) );
mediaUpload( {
filesList,
onFileChange: onSelectImages,
Expand Down
3 changes: 1 addition & 2 deletions packages/block-library/src/post-author/edit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* External dependencies
*/
import { forEach } from 'lodash';
import classnames from 'classnames';

/**
Expand Down Expand Up @@ -51,7 +50,7 @@ function PostAuthorEdit( {

const avatarSizes = [];
if ( authorDetails ) {
forEach( authorDetails.avatar_urls, ( url, size ) => {
Object.keys( authorDetails.avatar_urls ).forEach( ( size ) => {
avatarSizes.push( {
value: size,
label: `${ size } x ${ size }`,
Expand Down
11 changes: 3 additions & 8 deletions packages/components/src/modal/aria-helper.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
// @ts-nocheck

/**
* External dependencies
*/
import { forEach } from 'lodash';

const LIVE_REGION_ARIA_ROLES = new Set( [
'alert',
'status',
Expand Down Expand Up @@ -32,8 +27,8 @@ export function hideApp( unhiddenElement ) {
if ( isHidden ) {
return;
}
const elements = document.body.children;
forEach( elements, ( element ) => {
const elements = Array.from( document.body.children );
Copy link
Contributor

@sgomes sgomes Sep 2, 2022

Choose a reason for hiding this comment

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

TIL the collections returned by Element.prototype.children() don't have a forEach method...

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, HTMLCollection is limited to having .length, but doesn't reliably implement the iterator protocol in all browsers. Some browser support some means of iteration (like for/of and for/in for example) but not reliably, so Array.from() ensures we'll be able to iterate the collection of elements.

elements.forEach( ( element ) => {
if ( element === unhiddenElement ) {
return;
}
Expand Down Expand Up @@ -70,7 +65,7 @@ export function showApp() {
if ( ! isHidden ) {
return;
}
forEach( hiddenElements, ( element ) => {
hiddenElements.forEach( ( element ) => {
element.removeAttribute( 'aria-hidden' );
} );
hiddenElements = [];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { forEach } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -57,13 +52,13 @@ export default function withGlobalEvents( eventTypesToHandlers ) {
}

componentDidMount() {
forEach( eventTypesToHandlers, ( _, eventType ) => {
Object.keys( eventTypesToHandlers ).forEach( ( eventType ) => {
listener.add( eventType, this );
} );
}

componentWillUnmount() {
forEach( eventTypesToHandlers, ( _, eventType ) => {
Object.keys( eventTypesToHandlers ).forEach( ( eventType ) => {
listener.remove( eventType, this );
} );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { forEach, without } from 'lodash';
import { without } from 'lodash';

/**
* Class responsible for orchestrating event handling on the global window,
Expand Down Expand Up @@ -40,9 +40,11 @@ class Listener {
}

handleEvent( /** @type {any} */ event ) {
forEach( this.listeners[ event.type ], ( instance ) => {
instance.handleEvent( event );
} );
this.listeners[ event.type ]?.forEach(
( /** @type {any} */ instance ) => {
instance.handleEvent( event );
}
);
}
}

Expand Down
6 changes: 3 additions & 3 deletions packages/data/src/registry.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { mapValues, forEach } from 'lodash';
import { mapValues } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -293,10 +293,10 @@ export function createRegistry( storeConfigs = {}, parent = null ) {

function batch( callback ) {
emitter.pause();
forEach( stores, ( store ) => store.emitter.pause() );
Object.values( stores ).forEach( ( store ) => store.emitter.pause() );
callback();
emitter.resume();
forEach( stores, ( store ) => store.emitter.resume() );
Object.values( stores ).forEach( ( store ) => store.emitter.resume() );
}

let registry = {
Expand Down
4 changes: 2 additions & 2 deletions packages/edit-post/src/editor.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { forEach, map, without } from 'lodash';
import { map, without } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -162,7 +162,7 @@ function Editor( {
const styles = useMemo( () => {
const themeStyles = [];
const presetStyles = [];
forEach( settings.styles, ( style ) => {
settings.styles?.forEach( ( style ) => {
if ( ! style.__unstableType || style.__unstableType === 'theme' ) {
themeStyles.push( style );
} else {
Expand Down
Loading