From 2747f1b34501a5fcc83f344a46f72b31e805c3d3 Mon Sep 17 00:00:00 2001 From: Armand Abric Date: Sun, 8 Oct 2017 17:32:11 +0200 Subject: [PATCH] fix(formatting): symbol description are now quoted Closes #134 BREAKING CHANGE: Symbol description are now correctly quoted. This change the output if you use Symbol in your code --- src/formatter/formatPropValue.js | 11 ++++++++++- src/formatter/formatPropValue.spec.js | 7 ++++++- src/index.spec.js | 4 ++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/formatter/formatPropValue.js b/src/formatter/formatPropValue.js index 9fade670a..93879ba07 100644 --- a/src/formatter/formatPropValue.js +++ b/src/formatter/formatPropValue.js @@ -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') { diff --git a/src/formatter/formatPropValue.spec.js b/src/formatter/formatPropValue.spec.js index 8f719a86a..62dfb8b37 100644 --- a/src/formatter/formatPropValue.spec.js +++ b/src/formatter/formatPropValue.spec.js @@ -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', () => { diff --git a/src/index.spec.js b/src/index.spec.js index 62944985b..695d8851b 100644 --- a/src/index.spec.js +++ b/src/index.spec.js @@ -131,7 +131,7 @@ describe('reactElementToJSXString(ReactElement)', () => { reactElementToJSXString( React.createElement('div', { title: Symbol('hello "you"') }) ) - ).toEqual('
'); + ).toEqual('
'); }); it('reactElementToJSXString(
)', () => { @@ -660,7 +660,7 @@ describe('reactElementToJSXString(ReactElement)', () => { it('reactElementToJSXString(
)', () => { expect(reactElementToJSXString(
)).toEqual( - '
' + "
" ); });