util: escape symbol and non-enumerable keys
These keys require escaping as they might also contain line breaks and other special characters. PR-URL: https://github.com/nodejs/node/pull/22300 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
parent
c535bde5da
commit
a04f2f7df6
@ -1256,9 +1256,10 @@ function formatProperty(ctx, value, recurseTimes, key, array) {
|
|||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
if (typeof key === 'symbol') {
|
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) {
|
} else if (desc.enumerable === false) {
|
||||||
name = `[${key}]`;
|
name = `[${key.replace(strEscapeSequencesReplacer, escapeFn)}]`;
|
||||||
} else if (keyStrRegExp.test(key)) {
|
} else if (keyStrRegExp.test(key)) {
|
||||||
name = ctx.stylize(key, 'name');
|
name = ctx.stylize(key, 'name');
|
||||||
} else {
|
} else {
|
||||||
|
@ -838,22 +838,22 @@ if (typeof Symbol !== 'undefined') {
|
|||||||
const options = { showHidden: true };
|
const options = { showHidden: true };
|
||||||
let subject = {};
|
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(
|
assert.strictEqual(
|
||||||
util.inspect(subject, options),
|
util.inspect(subject, options),
|
||||||
'{ [Symbol(symbol)]: 42 }'
|
'{ [Symbol(sym\\nbol)]: 42 }'
|
||||||
);
|
);
|
||||||
|
|
||||||
Object.defineProperty(
|
Object.defineProperty(
|
||||||
subject,
|
subject,
|
||||||
Symbol(),
|
Symbol(),
|
||||||
{ enumerable: false, value: 'non-enum' });
|
{ enumerable: false, value: 'non-enum' });
|
||||||
assert.strictEqual(util.inspect(subject), '{ [Symbol(symbol)]: 42 }');
|
assert.strictEqual(util.inspect(subject), '{ [Symbol(sym\\nbol)]: 42 }');
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
util.inspect(subject, options),
|
util.inspect(subject, options),
|
||||||
"{ [Symbol(symbol)]: 42, [Symbol()]: 'non-enum' }"
|
"{ [Symbol(sym\\nbol)]: 42, [Symbol()]: 'non-enum' }"
|
||||||
);
|
);
|
||||||
|
|
||||||
subject = [1, 2, 3];
|
subject = [1, 2, 3];
|
||||||
@ -1637,3 +1637,13 @@ assert.strictEqual(inspect(Object(-1n)), '[BigInt: -1n]');
|
|||||||
assert.strictEqual(inspect(Object(13n)), '[BigInt: 13n]');
|
assert.strictEqual(inspect(Object(13n)), '[BigInt: 13n]');
|
||||||
assert.strictEqual(inspect(new BigInt64Array([0n])), 'BigInt64Array [ 0n ]');
|
assert.strictEqual(inspect(new BigInt64Array([0n])), 'BigInt64Array [ 0n ]');
|
||||||
assert.strictEqual(inspect(new BigUint64Array([0n])), 'BigUint64Array [ 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 }'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user