From 1b7d5b81bf234d17f8896751ebcdb3caa77712ba Mon Sep 17 00:00:00 2001 From: Valentin Semirulnik Date: Tue, 8 Apr 2025 23:27:51 +0400 Subject: [PATCH 1/3] Remove check for React.isValidElement --- src/formatter/sortObject.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/formatter/sortObject.js b/src/formatter/sortObject.js index c070ef612..780226035 100644 --- a/src/formatter/sortObject.js +++ b/src/formatter/sortObject.js @@ -7,12 +7,8 @@ function safeSortObject(value: any, seen: WeakSet): any { return value; } - // return date, regexp and react element values as is - if ( - value instanceof Date || - value instanceof RegExp || - React.isValidElement(value) - ) { + // return date and regexp values as is + if (value instanceof Date || value instanceof RegExp) { return value; } From 4ee3b94cb5800df80b27dcb180940c5a5cf2ce68 Mon Sep 17 00:00:00 2001 From: Valentin Semirulnik Date: Sun, 13 Apr 2025 21:38:36 +0400 Subject: [PATCH 2/3] Remove _owner key from react elements --- src/formatter/sortObject.js | 10 +++++++--- src/formatter/sortObject.spec.js | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/formatter/sortObject.js b/src/formatter/sortObject.js index 780226035..5e6b8a46a 100644 --- a/src/formatter/sortObject.js +++ b/src/formatter/sortObject.js @@ -12,6 +12,13 @@ function safeSortObject(value: any, seen: WeakSet): any { return value; } + // return react element as is but remove _owner key because it can lead to recursion + if (React.isValidElement(value)) { + const copyObj = { ...value }; + delete copyObj._owner; + return copyObj; + } + seen.add(value); // make a copy of array with each item passed through the sorting algorithm @@ -23,9 +30,6 @@ function safeSortObject(value: any, seen: WeakSet): any { return Object.keys(value) .sort() .reduce((result, key) => { - if (key === '_owner') { - return result; - } if (key === 'current' || seen.has(value[key])) { // eslint-disable-next-line no-param-reassign result[key] = '[Circular]'; diff --git a/src/formatter/sortObject.spec.js b/src/formatter/sortObject.spec.js index 1b6ab3d45..0b609dc8a 100644 --- a/src/formatter/sortObject.spec.js +++ b/src/formatter/sortObject.spec.js @@ -1,5 +1,6 @@ /* @flow */ +import React from 'react'; import sortObject from './sortObject'; describe('sortObject', () => { @@ -42,4 +43,24 @@ describe('sortObject', () => { c: date, }); }); + + it('should remove _owner key from react elements', () => { + const fixture = { + _owner: "_owner that doesn't belong to react element", + component:
, + }; + + expect(JSON.stringify(sortObject(fixture))).toEqual( + JSON.stringify({ + _owner: "_owner that doesn't belong to react element", + component: { + $$typeof: Symbol('react.transitional.element'), + type: 'div', + key: null, + props: {}, + _store: {}, + }, + }) + ); + }); }); From 74b094184c804340ecaa10af36e55bc2c69d453f Mon Sep 17 00:00:00 2001 From: Valentin Semirulnik Date: Fri, 25 Apr 2025 01:01:28 +0300 Subject: [PATCH 3/3] split test in to two --- src/formatter/sortObject.spec.js | 59 +++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 17 deletions(-) diff --git a/src/formatter/sortObject.spec.js b/src/formatter/sortObject.spec.js index 0b609dc8a..182118e19 100644 --- a/src/formatter/sortObject.spec.js +++ b/src/formatter/sortObject.spec.js @@ -44,23 +44,48 @@ describe('sortObject', () => { }); }); - it('should remove _owner key from react elements', () => { - const fixture = { - _owner: "_owner that doesn't belong to react element", - component:
, - }; - - expect(JSON.stringify(sortObject(fixture))).toEqual( - JSON.stringify({ + describe('_owner key', () => { + it('should preserve the _owner key for objects that are not react elements', () => { + const fixture = { _owner: "_owner that doesn't belong to react element", - component: { - $$typeof: Symbol('react.transitional.element'), - type: 'div', - key: null, - props: {}, - _store: {}, - }, - }) - ); + foo: 'bar', + }; + + expect(JSON.stringify(sortObject(fixture))).toEqual( + JSON.stringify({ + _owner: "_owner that doesn't belong to react element", + foo: 'bar', + }) + ); + }); + + it('should remove the _owner key from top level react element', () => { + const fixture = { + reactElement: ( +
+ +
+ ), + }; + + expect(JSON.stringify(sortObject(fixture))).toEqual( + JSON.stringify({ + reactElement: { + type: 'div', + key: null, + props: { + children: { + type: 'span', + key: null, + props: {}, + _owner: null, + _store: {}, + }, + }, + _store: {}, + }, + }) + ); + }); }); });