Skip to content

Commit b474a77

Browse files
committed
Cleanup
1 parent 1802836 commit b474a77

File tree

2 files changed

+64
-11
lines changed

2 files changed

+64
-11
lines changed

index.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,24 @@ export default function stringifyObject(input, options, pad) {
6161
}
6262

6363
if (typeof input === 'symbol') {
64-
const t = input.description;
65-
if (t === undefined) return 'Symbol()';
66-
if (t.slice(0,7) === 'Symbol.'
67-
&& Symbol[t.slice(7)] === input
64+
const {description} = input;
65+
if (description === undefined) {
66+
return 'Symbol()';
67+
}
68+
69+
if (
70+
description?.startsWith('Symbol.')
71+
&& Symbol[description.slice(7)] === input
6872
) {
69-
return t;
73+
return description;
7074
}
71-
const q = stringify(t, options);
75+
76+
const quotedDescription = stringify(description, options);
7277
if (Symbol.keyFor(input) !== undefined) {
73-
return `Symbol.for(${q})`;
78+
return `Symbol.for(${quotedDescription})`;
7479
}
75-
return `Symbol(${q})`;
80+
81+
return `Symbol(${quotedDescription})`;
7682
}
7783

7884
if (input instanceof Date) {
@@ -120,9 +126,15 @@ export default function stringifyObject(input, options, pad) {
120126
const eol = objectKeys.length - 1 === index ? tokens.newline : ',' + tokens.newlineOrSpace;
121127
const isSymbol = typeof element === 'symbol';
122128
const isClassic = !isSymbol && /^[a-z$_][$\w]*$/i.test(element);
129+
123130
let key = element;
124-
if (!isClassic) key = stringify(element, options);
125-
if (isSymbol) key = '[' + key + ']';
131+
if (!isClassic) {
132+
key = stringify(element, options);
133+
}
134+
135+
if (isSymbol) {
136+
key = `[${key}]`;
137+
}
126138

127139
let value = stringify(input[element], options, pad + indent);
128140
if (options.transform) {

test/index.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,53 @@ test('don\'t stringify non-enumerable symbols', t => {
201201

202202
t.is(stringifyObject(object), '{\n\t[Symbol(\'for enumerable key\')]: undefined\n}');
203203
});
204+
204205
test('handle symbols', t => {
205206
const object = {
206207
[Symbol('unique')]: Symbol('unique'),
207208
[Symbol.for('registry')]: [Symbol.for('registry'), 2],
208209
[Symbol.iterator]: {k: Symbol.iterator},
209-
[Symbol()]: 'undef'
210+
[Symbol()]: 'undef', // eslint-disable-line symbol-description
210211
};
211212
t.is(stringifyObject(object), '{\n\t[Symbol(\'unique\')]: Symbol(\'unique\'),\n\t[Symbol.for(\'registry\')]: [\n\t\tSymbol.for(\'registry\'),\n\t\t2\n\t],\n\t[Symbol.iterator]: {\n\t\tk: Symbol.iterator\n\t},\n\t[Symbol()]: \'undef\'\n}');
213+
214+
// Anonymous symbol (no description)
215+
t.is(stringifyObject(Symbol()), 'Symbol()'); // eslint-disable-line symbol-description
216+
217+
// Symbol with empty string description
218+
t.is(stringifyObject(Symbol('')), 'Symbol(\'\')');
219+
220+
// Symbol.for with empty string
221+
t.is(stringifyObject(Symbol.for('')), 'Symbol.for(\'\')');
222+
223+
// Test as object keys
224+
const emptySymbolKeys = {
225+
[Symbol()]: 'anonymous', // eslint-disable-line symbol-description
226+
[Symbol('')]: 'empty string',
227+
[Symbol.for('')]: 'empty for',
228+
};
229+
t.regex(stringifyObject(emptySymbolKeys), /\[Symbol\(\)]/);
230+
t.regex(stringifyObject(emptySymbolKeys), /\[Symbol\(''\)]/);
231+
t.regex(stringifyObject(emptySymbolKeys), /\[Symbol\.for\(''\)]/);
232+
233+
// Symbol escaping with special characters
234+
const symbolWithSpecialChars = Symbol('a"b\\c\n');
235+
t.is(stringifyObject(symbolWithSpecialChars), 'Symbol(\'a"b\\\\c\\n\')');
236+
t.is(stringifyObject(symbolWithSpecialChars, {singleQuotes: false}), 'Symbol("a\\"b\\\\c\\n")');
237+
238+
const specialCharKey = {
239+
[Symbol('a"b\\c\n')]: 'value',
240+
};
241+
t.regex(stringifyObject(specialCharKey), /\[Symbol\('a"b\\\\c\\n'\)]/);
242+
243+
// Well-known symbols
244+
t.is(stringifyObject(Symbol.iterator), 'Symbol.iterator');
245+
t.is(stringifyObject(Symbol.hasInstance), 'Symbol.hasInstance');
246+
t.is(stringifyObject(Symbol.toStringTag), 'Symbol.toStringTag');
247+
248+
// Look-alike symbols (not real well-known symbols)
249+
t.is(stringifyObject(Symbol('Symbol.iterator')), 'Symbol(\'Symbol.iterator\')');
250+
t.is(stringifyObject(Symbol('Symbol.hasInstance')), 'Symbol(\'Symbol.hasInstance\')');
251+
t.is(stringifyObject(Symbol('Symbol.toStringTag')), 'Symbol(\'Symbol.toStringTag\')');
252+
});
212253
});

0 commit comments

Comments
 (0)