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

fix(formatting): symbol description are now quoted #204

Merged
merged 1 commit into from
Oct 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/formatter/formatPropValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@ const formatPropValue = (
// @see: https://flow.org/en/docs/types/primitives/
// $FlowFixMe: Flow does not support Symbol
if (typeof propValue === 'symbol') {
return `{${String(propValue)}}`;
const symbolDescription = propValue
.valueOf()
.toString()
.replace(/Symbol\((.*)\)/, '$1');

if (!symbolDescription) {
return `{Symbol()}`;
}

return `{Symbol('${symbolDescription}')}`;
}

if (typeof propValue === 'function') {
Expand Down
7 changes: 6 additions & 1 deletion src/formatter/formatPropValue.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ describe('formatPropValue', () => {
});

it('should format a symbol prop value', () => {
expect(formatPropValue(Symbol('Foo'), false, 0, {})).toBe('{Symbol(Foo)}');
expect(formatPropValue(Symbol('Foo'), false, 0, {})).toBe(
"{Symbol('Foo')}"
);

// eslint-disable-next-line symbol-description
expect(formatPropValue(Symbol(), false, 0, {})).toBe('{Symbol()}');
});

it('should replace a function prop value by a an empty generic function by default', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ describe('reactElementToJSXString(ReactElement)', () => {
reactElementToJSXString(
React.createElement('div', { title: Symbol('hello "you"') })
)
).toEqual('<div title={Symbol(hello "you")} />');
).toEqual('<div title={Symbol(\'hello "you"\')} />');
});

it('reactElementToJSXString(<div/>)', () => {
Expand Down Expand Up @@ -660,7 +660,7 @@ describe('reactElementToJSXString(ReactElement)', () => {

it('reactElementToJSXString(<div type={Symbol("test")}/>)', () => {
expect(reactElementToJSXString(<div type={Symbol('test')} />)).toEqual(
'<div type={Symbol(test)} />'
"<div type={Symbol('test')} />"
);
});

Expand Down