Skip to content

Commit ad21917

Browse files
author
vvo
committed
feat(deep): handle deeply set functions
like `<div a={{b: function hello() {}}} />` BREAKING CHANGE: functions are now stringified to `function noRefCheck() {}` instead of `function () {code;}`. For various reasons AND to be specific about the fact that we do not represent the function in a realistic way.
1 parent 403e404 commit ad21917

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ Turn a ReactElement into the corresponding JSX string.
1616
Useful for unit testing and any other need you may think of.
1717

1818
Features:
19-
- supports nesting
20-
- props: supports string, number, function (inlined as `prop={function () {code;}}`), object, ReactElement (inlined), regex..
19+
- supports nesting and deep nesting like `<div a={{b: {c: {d: <div />}}}} />`
20+
- props: supports string, number, function (inlined as `prop={function noRefCheck() {}}`), object, ReactElement (inlined), regex..
2121
- order props alphabetically
2222
- sort object keys in a deterministic order (`o={{a: 1, b:2}} === o={{b:2, a:1}}`)
2323
- React's documentation indent style for JSX

index-test.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ describe(`reactElementToJSXString(ReactElement)`, () => {
2828
it(`reactElementToJSXString(<div fn={() => {}}/>)`, () => {
2929
expect(
3030
reactElementToJSXString(<div fn={() => {}}/>)
31-
).toEqual(`<div fn={function () {code;}} />`);
31+
).toEqual(`<div fn={function noRefCheck() {}} />`);
3232
});
3333

3434
it(`reactElementToJSXString(<div fn={function hello(){}}/>)`, () => {
3535
expect(
3636
reactElementToJSXString(<div fn={function hello() {}}/>)
37-
).toEqual(`<div fn={function () {code;}} />`);
37+
).toEqual(`<div fn={function noRefCheck() {}} />`);
3838
});
3939

4040
it(`reactElementToJSXString(<div co={<div a="1" />} />)`, () => {
@@ -175,4 +175,11 @@ describe(`reactElementToJSXString(ReactElement)`, () => {
175175
reactElementToJSXString(<div a={undefined} />)
176176
);
177177
});
178+
179+
it(`reactElementToJSXString(<div a={{b: function hello() {}}} />`, () => {
180+
expect(
181+
reactElementToJSXString(<div a={{b: function hello() {}}} />)
182+
).toEqual(`<div a={{b: function noRefCheck() {}}} />`);
183+
});
184+
178185
});

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ function formatPropValue(propValue) {
9797

9898
function formatValue(value) {
9999
if (typeof value === 'function') {
100-
return `function () {code;}`;
100+
return function noRefCheck() {};
101101
} else if (isElement(value)) {
102102
return toJSXString({ReactElement: value, inline: true});
103103
} else if (value && Object.keys(value).length > 0) {

0 commit comments

Comments
 (0)