Skip to content

Commit 2747f1b

Browse files
committed
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
1 parent 12a9343 commit 2747f1b

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

src/formatter/formatPropValue.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,16 @@ const formatPropValue = (
3030
// @see: https://flow.org/en/docs/types/primitives/
3131
// $FlowFixMe: Flow does not support Symbol
3232
if (typeof propValue === 'symbol') {
33-
return `{${String(propValue)}}`;
33+
const symbolDescription = propValue
34+
.valueOf()
35+
.toString()
36+
.replace(/Symbol\((.*)\)/, '$1');
37+
38+
if (!symbolDescription) {
39+
return `{Symbol()}`;
40+
}
41+
42+
return `{Symbol('${symbolDescription}')}`;
3443
}
3544

3645
if (typeof propValue === 'function') {

src/formatter/formatPropValue.spec.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ describe('formatPropValue', () => {
3030
});
3131

3232
it('should format a symbol prop value', () => {
33-
expect(formatPropValue(Symbol('Foo'), false, 0, {})).toBe('{Symbol(Foo)}');
33+
expect(formatPropValue(Symbol('Foo'), false, 0, {})).toBe(
34+
"{Symbol('Foo')}"
35+
);
36+
37+
// eslint-disable-next-line symbol-description
38+
expect(formatPropValue(Symbol(), false, 0, {})).toBe('{Symbol()}');
3439
});
3540

3641
it('should replace a function prop value by a an empty generic function by default', () => {

src/index.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ describe('reactElementToJSXString(ReactElement)', () => {
131131
reactElementToJSXString(
132132
React.createElement('div', { title: Symbol('hello "you"') })
133133
)
134-
).toEqual('<div title={Symbol(hello "you")} />');
134+
).toEqual('<div title={Symbol(\'hello "you"\')} />');
135135
});
136136

137137
it('reactElementToJSXString(<div/>)', () => {
@@ -660,7 +660,7 @@ describe('reactElementToJSXString(ReactElement)', () => {
660660

661661
it('reactElementToJSXString(<div type={Symbol("test")}/>)', () => {
662662
expect(reactElementToJSXString(<div type={Symbol('test')} />)).toEqual(
663-
'<div type={Symbol(test)} />'
663+
"<div type={Symbol('test')} />"
664664
);
665665
});
666666

0 commit comments

Comments
 (0)