Skip to content

Commit

Permalink
fix: fixed crashing on circular React elements (#619)
Browse files Browse the repository at this point in the history
* fix: fixed crashing on circular React elements

* chore: adjust a test implementation to be more meaningful
  • Loading branch information
Andarist committed Sep 30, 2021
1 parent 2c5132e commit ea73118
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/formatter/sortObject.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
/* @flow */
import * as React from 'react';

export default function sortObject(value: any): any {
// return non-object value as is
if (value === null || typeof value !== 'object') {
return value;
}

// return date and regexp values as is
if (value instanceof Date || value instanceof RegExp) {
// return date, regexp and react element values as is
if (
value instanceof Date ||
value instanceof RegExp ||
React.isValidElement(value)
) {
return value;
}

Expand Down
21 changes: 21 additions & 0 deletions src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1334,4 +1334,25 @@ describe('reactElementToJSXString(ReactElement)', () => {

expect(reactElementToJSXString(<Lazy />)).toEqual(`<Lazy />`);
});

it('should stringify `forwardRef` element with a circular property', () => {
function TagList({ tags }) {
return tags;
}

const Tag = React.forwardRef(function Tag({ text }, ref) {
return <span ref={ref}>{text}</span>;
});
Tag.emotionReal = Tag;

expect(
reactElementToJSXString(
<TagList tags={[<Tag key="oops" text="oops, circular" />]} />
)
).toEqual(`<TagList
tags={[
<Tag key="oops" text="oops, circular"/>
]}
/>`);
});
});

0 comments on commit ea73118

Please sign in to comment.