Skip to content

Commit

Permalink
Lodash: Replace _.mergeWith() with deepmerge in blocks (#50637)
Browse files Browse the repository at this point in the history
  • Loading branch information
tyxla authored Jun 14, 2023
1 parent fd7c943 commit 4d0f303
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 27 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/blocks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@wordpress/shortcode": "file:../shortcode",
"change-case": "^4.1.2",
"colord": "^2.7.0",
"deepmerge": "^4.3.0",
"fast-deep-equal": "^3.1.3",
"hpq": "^1.3.0",
"is-plain-object": "^5.0.0",
Expand Down
66 changes: 39 additions & 27 deletions packages/blocks/src/api/raw-handling/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { mergeWith } from 'lodash';
import deepmerge from 'deepmerge';

/**
* WordPress dependencies
Expand All @@ -14,6 +14,41 @@ import { isPhrasingContent, getPhrasingContentSchema } from '@wordpress/dom';
import { hasBlockSupport } from '..';
import { getRawTransforms } from './get-raw-transforms';

const customMerge = ( key ) => {
return ( srcValue, objValue ) => {
switch ( key ) {
case 'children': {
if ( objValue === '*' || srcValue === '*' ) {
return '*';
}

return { ...objValue, ...srcValue };
}
case 'attributes':
case 'require': {
return [ ...( objValue || [] ), ...( srcValue || [] ) ];
}
case 'isMatch': {
// If one of the values being merge is undefined (matches everything),
// the result of the merge will be undefined.
if ( ! objValue || ! srcValue ) {
return undefined;
}
// When merging two isMatch functions, the result is a new function
// that returns if one of the source functions returns true.
return ( ...args ) => {
return objValue( ...args ) || srcValue( ...args );
};
}
}

return deepmerge( objValue, srcValue, {
customMerge,
clone: false,
} );
};
};

export function getBlockContentSchemaFromTransforms( transforms, context ) {
const phrasingContentSchema = getPhrasingContentSchema( context );
const schemaArgs = { phrasingContentSchema, isPaste: context === 'paste' };
Expand Down Expand Up @@ -51,32 +86,9 @@ export function getBlockContentSchemaFromTransforms( transforms, context ) {
);
} );

return mergeWith( {}, ...schemas, ( objValue, srcValue, key ) => {
switch ( key ) {
case 'children': {
if ( objValue === '*' || srcValue === '*' ) {
return '*';
}

return { ...objValue, ...srcValue };
}
case 'attributes':
case 'require': {
return [ ...( objValue || [] ), ...( srcValue || [] ) ];
}
case 'isMatch': {
// If one of the values being merge is undefined (matches everything),
// the result of the merge will be undefined.
if ( ! objValue || ! srcValue ) {
return undefined;
}
// When merging two isMatch functions, the result is a new function
// that returns if one of the source functions returns true.
return ( ...args ) => {
return objValue( ...args ) || srcValue( ...args );
};
}
}
return deepmerge.all( schemas, {
customMerge,
clone: false,
} );
}

Expand Down

1 comment on commit 4d0f303

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 4d0f303.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5265701997
📝 Reported issues:

Please sign in to comment.