From 33a8f2caa04151b481c4295c9c72aac83514d815 Mon Sep 17 00:00:00 2001 From: arthur791004 Date: Thu, 18 Apr 2024 02:19:07 +0800 Subject: [PATCH] Fix the removePropertyFromObject function throws an error if the object is null (#60831) Co-authored-by: arthur791004 Co-authored-by: fabiankaegy --- .../test/use-theme-style-variations-by-property.js | 4 ++++ .../use-theme-style-variations-by-property.js | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/edit-site/src/hooks/use-theme-style-variations/test/use-theme-style-variations-by-property.js b/packages/edit-site/src/hooks/use-theme-style-variations/test/use-theme-style-variations-by-property.js index 23db164b24895d..89a58166257f66 100644 --- a/packages/edit-site/src/hooks/use-theme-style-variations/test/use-theme-style-variations-by-property.js +++ b/packages/edit-site/src/hooks/use-theme-style-variations/test/use-theme-style-variations-by-property.js @@ -1079,6 +1079,10 @@ describe( 'removePropertyFromObject', () => { expect( removePropertyFromObject( object, 'color' ) ).toEqual( object ); } ); + it( 'should return with null', () => { + expect( removePropertyFromObject( null, 'color' ) ).toEqual( null ); + } ); + it( 'should remove the specified property from the object', () => { expect( removePropertyFromObject( diff --git a/packages/edit-site/src/hooks/use-theme-style-variations/use-theme-style-variations-by-property.js b/packages/edit-site/src/hooks/use-theme-style-variations/use-theme-style-variations-by-property.js index f2acef0026916c..96d1522b07b2a6 100644 --- a/packages/edit-site/src/hooks/use-theme-style-variations/use-theme-style-variations-by-property.js +++ b/packages/edit-site/src/hooks/use-theme-style-variations/use-theme-style-variations-by-property.js @@ -28,7 +28,11 @@ export function removePropertyFromObject( object, property ) { return object; } - if ( typeof object !== 'object' || ! Object.keys( object ).length ) { + if ( + typeof object !== 'object' || + ! object || + ! Object.keys( object ).length + ) { return object; }