Skip to content

Commit

Permalink
fix(formatting): symbol description are now quoted
Browse files Browse the repository at this point in the history
Closes #134

BREAKING CHANGE: Symbol description are now correctly quoted. This change the output if you use Symbol in your code
  • Loading branch information
armandabric committed Oct 8, 2017
1 parent 12a9343 commit 2747f1b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
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

0 comments on commit 2747f1b

Please sign in to comment.