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 _.isPlainObject() #42508

Merged
merged 4 commits into from Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Expand Up @@ -106,6 +106,7 @@ module.exports = {
'isNumber',
'isObject',
'isObjectLike',
'isPlainObject',
'isString',
'isUndefined',
'keyBy',
Expand Down
61 changes: 55 additions & 6 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
Expand Up @@ -43,6 +43,7 @@
"@wordpress/shortcode": "file:../shortcode",
"colord": "^2.7.0",
"hpq": "^1.3.0",
"is-plain-obj": "^4.1.0",
"lodash": "^4.17.21",
"memize": "^1.1.0",
"rememo": "^4.0.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/blocks/src/store/actions.js
@@ -1,7 +1,8 @@
/**
* External dependencies
*/
import { castArray, isPlainObject, omit, pick, some } from 'lodash';
import isPlainObject from 'is-plain-obj';
import { castArray, omit, pick, some } from 'lodash';

/**
* WordPress dependencies
Expand Down
8 changes: 5 additions & 3 deletions packages/components/src/text/hook.js
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import { css } from '@emotion/react';
import { isPlainObject } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -167,8 +166,11 @@ export default function useText( props ) {
*/
if ( ! truncate && Array.isArray( children ) ) {
content = Children.map( children, ( child ) => {
// @ts-ignore
if ( ! isPlainObject( child ) || ! ( 'props' in child ) ) {
if (
typeof child !== 'object' ||
child === null ||
! ( 'props' in child )
) {
return child;
}

Expand Down
1 change: 1 addition & 0 deletions packages/data/package.json
Expand Up @@ -35,6 +35,7 @@
"@wordpress/priority-queue": "file:../priority-queue",
"@wordpress/redux-routine": "file:../redux-routine",
"equivalent-key-map": "^0.2.2",
"is-plain-obj": "^4.1.0",
"is-promise": "^4.0.0",
"lodash": "^4.17.21",
"redux": "^4.1.2",
Expand Down
3 changes: 2 additions & 1 deletion packages/data/src/plugins/persistence/index.js
@@ -1,7 +1,8 @@
/**
* External dependencies
*/
import { merge, isPlainObject } from 'lodash';
import isPlainObject from 'is-plain-obj';
import { merge } from 'lodash';

/**
* Internal dependencies
Expand Down
1 change: 1 addition & 0 deletions packages/element/package.json
Expand Up @@ -32,6 +32,7 @@
"@types/react": "^17.0.37",
"@types/react-dom": "^17.0.11",
"@wordpress/escape-html": "file:../escape-html",
"is-plain-obj": "^4.1.0",
"lodash": "^4.17.21",
"react": "^17.0.2",
"react-dom": "^17.0.2"
Expand Down
3 changes: 2 additions & 1 deletion packages/element/src/serialize.js
Expand Up @@ -28,7 +28,8 @@
/**
* External dependencies
*/
import { kebabCase, isPlainObject } from 'lodash';
import isPlainObject from 'is-plain-obj';
import { kebabCase } from 'lodash';

/**
* WordPress dependencies
Expand Down
3 changes: 3 additions & 0 deletions packages/npm-package-json-lint-config/package.json
Expand Up @@ -26,6 +26,9 @@
"index.js"
],
"main": "index.js",
"dependencies": {
gziolo marked this conversation as resolved.
Show resolved Hide resolved
"is-plain-obj": "^4.1.0"
},
"peerDependencies": {
"npm-package-json-lint": ">=3.6.0"
},
Expand Down
14 changes: 5 additions & 9 deletions packages/npm-package-json-lint-config/test/index.test.js
@@ -1,17 +1,13 @@
/**
* External dependencies
*/
import isPlainObject from 'is-plain-obj';

/**
* Internal dependencies
*/
import config from '../';

const isPlainObject = ( obj ) => {
return (
typeof obj === 'object' &&
obj !== null &&
obj.constructor === Object &&
Object.prototype.toString.call( obj ) === '[object Object]'
);
};

describe( 'npm-package-json-lint config tests', () => {
it( 'should be an object', () => {
expect( isPlainObject( config ) ).toBeTruthy();
Expand Down
3 changes: 3 additions & 0 deletions packages/prettier-config/package.json
Expand Up @@ -27,6 +27,9 @@
],
"main": "lib/index.js",
"types": "build-types",
"dependencies": {
gziolo marked this conversation as resolved.
Show resolved Hide resolved
"is-plain-obj": "^4.1.0"
},
"peerDependencies": {
"prettier": ">=2"
},
Expand Down
14 changes: 5 additions & 9 deletions packages/prettier-config/test/index.js
@@ -1,17 +1,13 @@
/**
* External dependencies
*/
import isPlainObject from 'is-plain-obj';

/**
* Internal dependencies
*/
import config from '../lib/';

const isPlainObject = ( obj ) => {
return (
typeof obj === 'object' &&
obj !== null &&
obj.constructor === Object &&
Object.prototype.toString.call( obj ) === '[object Object]'
);
};

describe( 'prettier config tests', () => {
it( 'should be an object', () => {
expect( isPlainObject( config ) ).toBeTruthy();
Expand Down
1 change: 1 addition & 0 deletions packages/redux-routine/package.json
Expand Up @@ -30,6 +30,7 @@
"sideEffects": false,
"dependencies": {
"@babel/runtime": "^7.16.0",
"is-plain-obj": "^4.1.0",
"is-promise": "^4.0.0",
"lodash": "^4.17.21",
"rungen": "^0.3.2"
Expand Down
2 changes: 1 addition & 1 deletion packages/redux-routine/src/is-action.js
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { isPlainObject } from 'lodash';
import isPlainObject from 'is-plain-obj';

/* eslint-disable jsdoc/valid-types */
/**
Expand Down
2 changes: 1 addition & 1 deletion test/native/jest.config.js
Expand Up @@ -65,7 +65,7 @@ module.exports = {
// See: https://github.com/wordpress-mobile/gutenberg-mobile/pull/257#discussion_r234978268
// There is no overloading in jest so we need to rewrite the config from react-native-jest-preset:
// https://github.com/facebook/react-native/blob/HEAD/jest-preset.json#L20
'node_modules/(?!(simple-html-tokenizer|(jest-)?react-native|@react-native|react-clone-referenced-element|@react-navigation))',
'node_modules/(?!(simple-html-tokenizer|is-plain-obj|(jest-)?react-native|@react-native|react-clone-referenced-element|@react-navigation))',
],
snapshotSerializers: [ '@emotion/jest/serializer' ],
reporters: [ 'default', 'jest-junit' ],
Expand Down
1 change: 1 addition & 0 deletions test/unit/jest.config.js
Expand Up @@ -36,6 +36,7 @@ module.exports = {
transform: {
'^.+\\.[jt]sx?$': '<rootDir>/test/unit/scripts/babel-transformer.js',
},
transformIgnorePatterns: [ 'node_modules/(?!(is-plain-obj))' ],
Copy link
Member

@gziolo gziolo Jul 19, 2022

Choose a reason for hiding this comment

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

Interesting, is Jest still having issues with ES modules?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes! If we ditch that, a bunch of unit tests where this module is directly or transitively used will fail.

snapshotSerializers: [
'enzyme-to-json/serializer',
'@emotion/jest/serializer',
Expand Down