util: inspect: enumerable Symbols no longer have square brackets

Implements https://github.com/orgs/nodejs/discussions/41283#discussioncomment-11188239

PR-URL: https://github.com/nodejs/node/pull/55778
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
This commit is contained in:
Jordan Harband 2024-11-08 12:18:04 +00:00 committed by Node.js GitHub Bot
parent 23275cc7bc
commit e577618227
14 changed files with 53 additions and 56 deletions

View File

@ -878,7 +878,7 @@ assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 });
// AssertionError [ERR_ASSERTION]: Inputs identical but not reference equal:
//
// {
// [Symbol()]: 1
// Symbol(): 1
// }
const weakMap1 = new WeakMap();
@ -970,7 +970,7 @@ assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 });
// AssertionError [ERR_ASSERTION]: Inputs identical but not reference equal:
//
// {
// [Symbol()]: 1
// Symbol(): 1
// }
const weakMap1 = new WeakMap();

View File

@ -73,8 +73,8 @@ myEmitter.on('event', function(a, b) {
// _events: [Object: null prototype] { event: [Function (anonymous)] },
// _eventsCount: 1,
// _maxListeners: undefined,
// [Symbol(shapeMode)]: false,
// [Symbol(kCapture)]: false
// Symbol(shapeMode): false,
// Symbol(kCapture): false
// } true
});
myEmitter.emit('event', 'a', 'b');
@ -91,8 +91,8 @@ myEmitter.on('event', function(a, b) {
// _events: [Object: null prototype] { event: [Function (anonymous)] },
// _eventsCount: 1,
// _maxListeners: undefined,
// [Symbol(shapeMode)]: false,
// [Symbol(kCapture)]: false
// Symbol(shapeMode): false,
// Symbol(kCapture): false
// } true
});
myEmitter.emit('event', 'a', 'b');

View File

@ -1999,21 +1999,16 @@ function formatProperty(ctx, value, recurseTimes, key, type, desc,
SymbolPrototypeToString(key),
escapeFn,
);
name = `[${ctx.stylize(tmp, 'symbol')}]`;
} else if (key === '__proto__') {
name = "['__proto__']";
} else if (desc.enumerable === false) {
const tmp = RegExpPrototypeSymbolReplace(
strEscapeSequencesReplacer,
key,
escapeFn,
);
name = `[${tmp}]`;
name = ctx.stylize(tmp, 'symbol');
} else if (RegExpPrototypeExec(keyStrRegExp, key) !== null) {
name = ctx.stylize(key, 'name');
name = key === '__proto__' ? "['__proto__']" : ctx.stylize(key, 'name');
} else {
name = ctx.stylize(strEscape(key), 'string');
}
if (desc.enumerable === false) {
name = `[${name}]`;
}
return `${name}:${extra}${str}`;
}

View File

@ -454,7 +454,7 @@ not ok 50 - custom inspect symbol that throws fail
error: |-
{
foo: 1,
[Symbol(nodejs.util.inspect.custom)]: [Function: [nodejs.util.inspect.custom]]
Symbol(nodejs.util.inspect.custom): [Function: [nodejs.util.inspect.custom]]
}
code: 'ERR_TEST_FAILURE'
...

View File

@ -137,7 +137,7 @@ Failed tests:
✖ custom inspect symbol fail (*ms)
customized
✖ custom inspect symbol that throws fail (*ms)
{ foo: 1, [Symbol(nodejs.util.inspect.custom)]: [Function: [nodejs.util.inspect.custom]] }
{ foo: 1, Symbol(nodejs.util.inspect.custom): [Function: [nodejs.util.inspect.custom]] }
✖ sync throw fails at first (*ms)
Error: thrown from subtest sync throw fails at first
*

View File

@ -319,15 +319,15 @@ Error [ERR_TEST_FAILURE]: thrown from callback async throw
[Error [ERR_TEST_FAILURE]: customized] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: customized }
</failure>
</testcase>
<testcase name="custom inspect symbol that throws fail" time="*" classname="test" failure="{ foo: 1, [Symbol(nodejs.util.inspect.custom)]: [Function: [nodejs.util.inspect.custom]]}">
<failure type="testCodeFailure" message="{ foo: 1, [Symbol(nodejs.util.inspect.custom)]: [Function: [nodejs.util.inspect.custom]]}">
<testcase name="custom inspect symbol that throws fail" time="*" classname="test" failure="{ foo: 1, Symbol(nodejs.util.inspect.custom): [Function: [nodejs.util.inspect.custom]]}">
<failure type="testCodeFailure" message="{ foo: 1, Symbol(nodejs.util.inspect.custom): [Function: [nodejs.util.inspect.custom]]}">
[Error [ERR_TEST_FAILURE]: {
foo: 1,
[Symbol(nodejs.util.inspect.custom)]: [Function: [nodejs.util.inspect.custom]]
Symbol(nodejs.util.inspect.custom): [Function: [nodejs.util.inspect.custom]]
}] {
code: 'ERR_TEST_FAILURE',
failureType: 'testCodeFailure',
cause: { foo: 1, [Symbol(nodejs.util.inspect.custom)]: [Function: [nodejs.util.inspect.custom]] }
cause: { foo: 1, Symbol(nodejs.util.inspect.custom): [Function: [nodejs.util.inspect.custom]] }
}
</failure>
</testcase>

View File

@ -502,7 +502,7 @@ not ok 51 - custom inspect symbol that throws fail
error: |-
{
foo: 1,
[Symbol(nodejs.util.inspect.custom)]: [Function: [nodejs.util.inspect.custom]]
Symbol(nodejs.util.inspect.custom): [Function: [nodejs.util.inspect.custom]]
}
code: 'ERR_TEST_FAILURE'
...

View File

@ -509,7 +509,7 @@ not ok 51 - custom inspect symbol that throws fail
error: |-
{
foo: 1,
[Symbol(nodejs.util.inspect.custom)]: [Function: [nodejs.util.inspect.custom]]
Symbol(nodejs.util.inspect.custom): [Function: [nodejs.util.inspect.custom]]
}
code: 'ERR_TEST_FAILURE'
...

View File

@ -194,7 +194,7 @@
customized
custom inspect symbol that throws fail (*ms)
{ foo: 1, [Symbol(nodejs.util.inspect.custom)]: [Function: [nodejs.util.inspect.custom]] }
{ foo: 1, Symbol(nodejs.util.inspect.custom): [Function: [nodejs.util.inspect.custom]] }
subtest sync throw fails
sync throw fails at first (*ms)
@ -476,7 +476,7 @@
*
custom inspect symbol that throws fail (*ms)
{ foo: 1, [Symbol(nodejs.util.inspect.custom)]: [Function: [nodejs.util.inspect.custom]] }
{ foo: 1, Symbol(nodejs.util.inspect.custom): [Function: [nodejs.util.inspect.custom]] }
*
sync throw fails at first (*ms)

View File

@ -801,7 +801,7 @@ test('Test strict assert', () => {
'\n' +
'+ {}\n' +
'- {\n' +
'- [Symbol(nodejs.util.inspect.custom)]: [Function (anonymous)],\n' +
'- Symbol(nodejs.util.inspect.custom): [Function (anonymous)],\n' +
"- loop: 'forever'\n" +
'- }\n'
});

View File

@ -234,10 +234,10 @@ for (const expected of expectedStrings) {
}
assert.strictEqual(strings.shift(),
"{\n foo: 'bar',\n [Symbol(nodejs.util.inspect.custom)]:" +
"{\n foo: 'bar',\n Symbol(nodejs.util.inspect.custom):" +
' [Function: [nodejs.util.inspect.custom]]\n}\n');
assert.strictEqual(strings.shift(),
"{\n foo: 'bar',\n [Symbol(nodejs.util.inspect.custom)]:" +
"{\n foo: 'bar',\n Symbol(nodejs.util.inspect.custom):" +
' [Function: [nodejs.util.inspect.custom]]\n}\n');
assert.ok(strings.shift().includes('foo: [Object]'));
assert.strictEqual(strings.shift().includes('baz'), false);

View File

@ -145,7 +145,7 @@ assert.strictEqual(
Object.assign(new String('hello'), { [Symbol('foo')]: 123 }),
{ showHidden: true }
),
"[String: 'hello'] { [length]: 5, [Symbol(foo)]: 123 }"
"[String: 'hello'] { [length]: 5, Symbol(foo): 123 }"
);
assert.match(util.inspect((new JSStream())._externalStream),
@ -823,7 +823,7 @@ assert.strictEqual(util.inspect({ __proto__: Date.prototype }), 'Date {}');
{
const x = { [util.inspect.custom]: util.inspect };
assert(util.inspect(x).includes(
'[Symbol(nodejs.util.inspect.custom)]: [Function: inspect] {\n'));
'Symbol(nodejs.util.inspect.custom): [Function: inspect] {\n'));
}
// `util.inspect` should display the escaped value of a key.
@ -1045,7 +1045,7 @@ util.inspect({ hasOwnProperty: null });
const UIC = 'nodejs.util.inspect.custom';
assert.strictEqual(
util.inspect(subject),
`{\n a: 123,\n [Symbol(${UIC})]: [Function: [${UIC}]]\n}`
`{\n a: 123,\n Symbol(${UIC}): [Function: [${UIC}]]\n}`
);
}
@ -1145,27 +1145,29 @@ if (typeof Symbol !== 'undefined') {
subject[Symbol('sym\nbol')] = 42;
assert.strictEqual(util.inspect(subject), '{ [Symbol(sym\\nbol)]: 42 }');
assert.strictEqual(util.inspect(subject), '{ Symbol(sym\\nbol): 42 }');
assert.strictEqual(
util.inspect(subject, options),
'{ [Symbol(sym\\nbol)]: 42 }'
'{ Symbol(sym\\nbol): 42 }'
);
Object.defineProperty(
subject,
Symbol(),
{ enumerable: false, value: 'non-enum' });
assert.strictEqual(util.inspect(subject), '{ [Symbol(sym\\nbol)]: 42 }');
assert.strictEqual(util.inspect(subject), '{ Symbol(sym\\nbol): 42 }');
assert.strictEqual(
util.inspect(subject, options),
"{ [Symbol(sym\\nbol)]: 42, [Symbol()]: 'non-enum' }"
"{ Symbol(sym\\nbol): 42, [Symbol()]: 'non-enum' }"
);
subject = [1, 2, 3];
subject[Symbol('symbol')] = 42;
assert.strictEqual(util.inspect(subject),
'[ 1, 2, 3, [Symbol(symbol)]: 42 ]');
assert.strictEqual(
util.inspect(subject),
'[ 1, 2, 3, Symbol(symbol): 42 ]'
);
}
// Test Set.
@ -1589,7 +1591,7 @@ util.inspect(process);
const obj = { [util.inspect.custom]: 'fhqwhgads' };
assert.strictEqual(
util.inspect(obj),
"{ [Symbol(nodejs.util.inspect.custom)]: 'fhqwhgads' }"
"{ Symbol(nodejs.util.inspect.custom): 'fhqwhgads' }"
);
}
@ -1598,7 +1600,7 @@ util.inspect(process);
const obj = { [Symbol.toStringTag]: 'a' };
assert.strictEqual(
util.inspect(obj),
"{ [Symbol(Symbol.toStringTag)]: 'a' }"
"{ Symbol(Symbol.toStringTag): 'a' }"
);
Object.defineProperty(obj, Symbol.toStringTag, {
value: 'a',
@ -2268,7 +2270,7 @@ assert.strictEqual(util.inspect('"\'${a}'), "'\"\\'${a}'");
value[Symbol('foo')] = 'yeah';
res = util.inspect(value);
assert.notStrictEqual(res, expectedWithoutProto);
assert.match(res, /\[Symbol\(foo\)]: 'yeah'/);
assert.match(res, /Symbol\(foo\): 'yeah'/);
});
assert.strictEqual(inspect(1n), '1n');
@ -2284,7 +2286,7 @@ assert.strictEqual(
Object.defineProperty(obj, 'Non\nenumerable\tkey', { value: true });
assert.strictEqual(
util.inspect(obj, { showHidden: true }),
'{ [Non\\nenumerable\\tkey]: true }'
'{ [\'Non\\nenumerable\\tkey\']: true }'
);
}
@ -2375,7 +2377,7 @@ assert.strictEqual(
arr[Symbol('a')] = false;
assert.strictEqual(
inspect(arr, { sorted: true }),
'[ 3, 2, 1, [Symbol(a)]: false, [Symbol(b)]: true, a: 1, b: 2, c: 3 ]'
'[ 3, 2, 1, Symbol(a): false, Symbol(b): true, a: 1, b: 2, c: 3 ]'
);
}
@ -3263,7 +3265,7 @@ assert.strictEqual(
Object.defineProperty(o, '__proto__', { enumerable: false });
assert.strictEqual(
util.inspect(o, { showHidden: true }),
"{ ['__proto__']: { a: 1 } }"
"{ [['__proto__']]: { a: 1 } }"
);
}
@ -3328,11 +3330,11 @@ assert.strictEqual(
get [Symbol.iterator]() {
throw new Error();
}
}), '{ [Symbol(Symbol.iterator)]: [Getter] }');
}), '{ Symbol(Symbol.iterator): [Getter] }');
}
{
const sym = Symbol('bar');
const sym = Symbol('bar()');
const o = {
'foo': 0,
'Symbol(foo)': 0,
@ -3347,9 +3349,9 @@ assert.strictEqual(
'{\n' +
' foo: 0,\n' +
" 'Symbol(foo)': 0,\n" +
' [Symbol(foo)]: 0,\n' +
' [Symbol(foo())]: 0,\n' +
' [Symbol(bar)]: 0\n' +
' Symbol(foo): 0,\n' +
' Symbol(foo()): 0,\n' +
' [Symbol(bar())]: 0\n' +
'}',
);
}

View File

@ -127,8 +127,8 @@ if (common.hasIntl) {
' encoding: \'utf-8\',\n' +
' fatal: false,\n' +
' ignoreBOM: true,\n' +
' [Symbol(flags)]: 4,\n' +
' [Symbol(handle)]: undefined\n' +
' Symbol(flags): 4,\n' +
' Symbol(handle): undefined\n' +
'}'
);
} else {
@ -138,10 +138,10 @@ if (common.hasIntl) {
" encoding: 'utf-8',\n" +
' fatal: false,\n' +
' ignoreBOM: true,\n' +
' [Symbol(flags)]: 4,\n' +
' [Symbol(handle)]: StringDecoder {\n' +
' Symbol(flags): 4,\n' +
' Symbol(handle): StringDecoder {\n' +
" encoding: 'utf8',\n" +
' [Symbol(kNativeDecoder)]: <Buffer 00 00 00 00 00 00 01>\n' +
' Symbol(kNativeDecoder): <Buffer 00 00 00 00 00 00 01>\n' +
' }\n' +
'}'
);

View File

@ -45,7 +45,7 @@ assert.strictEqual(
search: '?que=ry',
searchParams: URLSearchParams { 'que' => 'ry' },
hash: '#hash',
[Symbol(context)]: URLContext {
Symbol(context): URLContext {
href: 'https://username:password@host.name:8080/path/name/?que=ry#hash',
protocol_end: 6,
username_end: 16,