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

util: escape symbol and non-enumerable keys #22300

Closed
wants to merge 1 commit into from
Closed
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
util: escape symbol and non-enumerable keys
These keys require escaping as they might also contain line breaks
and other special characters.
  • Loading branch information
BridgeAR committed Aug 15, 2018
commit c3f2bdc3e69a6886d0fa9dd8ec9c4534b91fe14d
5 changes: 3 additions & 2 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -1256,9 +1256,10 @@ function formatProperty(ctx, value, recurseTimes, key, array) {
return str;
}
if (typeof key === 'symbol') {
name = `[${ctx.stylize(key.toString(), 'symbol')}]`;
const tmp = key.toString().replace(strEscapeSequencesReplacer, escapeFn);
name = `[${ctx.stylize(tmp, 'symbol')}]`;
} else if (desc.enumerable === false) {
name = `[${key}]`;
name = `[${key.replace(strEscapeSequencesReplacer, escapeFn)}]`;
} else if (keyStrRegExp.test(key)) {
name = ctx.stylize(key, 'name');
} else {
Expand Down
20 changes: 15 additions & 5 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -838,22 +838,22 @@ if (typeof Symbol !== 'undefined') {
const options = { showHidden: true };
let subject = {};

subject[Symbol('symbol')] = 42;
subject[Symbol('sym\nbol')] = 42;

assert.strictEqual(util.inspect(subject), '{ [Symbol(symbol)]: 42 }');
assert.strictEqual(util.inspect(subject), '{ [Symbol(sym\\nbol)]: 42 }');
assert.strictEqual(
util.inspect(subject, options),
'{ [Symbol(symbol)]: 42 }'
'{ [Symbol(sym\\nbol)]: 42 }'
);

Object.defineProperty(
subject,
Symbol(),
{ enumerable: false, value: 'non-enum' });
assert.strictEqual(util.inspect(subject), '{ [Symbol(symbol)]: 42 }');
assert.strictEqual(util.inspect(subject), '{ [Symbol(sym\\nbol)]: 42 }');
assert.strictEqual(
util.inspect(subject, options),
"{ [Symbol(symbol)]: 42, [Symbol()]: 'non-enum' }"
"{ [Symbol(sym\\nbol)]: 42, [Symbol()]: 'non-enum' }"
);

subject = [1, 2, 3];
Expand Down Expand Up @@ -1637,3 +1637,13 @@ assert.strictEqual(inspect(Object(-1n)), '[BigInt: -1n]');
assert.strictEqual(inspect(Object(13n)), '[BigInt: 13n]');
assert.strictEqual(inspect(new BigInt64Array([0n])), 'BigInt64Array [ 0n ]');
assert.strictEqual(inspect(new BigUint64Array([0n])), 'BigUint64Array [ 0n ]');

// Verify non-enumerable keys get escaped.
{
const obj = {};
Object.defineProperty(obj, 'Non\nenumerable\tkey', { value: true });
assert.strictEqual(
util.inspect(obj, { showHidden: true }),
'{ [Non\\nenumerable\\tkey]: true }'
);
}