Skip to content

Commit

Permalink
util: inspect: enumerable Symbols no longer have square brackets
Browse files Browse the repository at this point in the history
Implements https://github.com/orgs/nodejs/discussions/41283#discussioncomment-11188239

PR-URL: #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>
  • Loading branch information
ljharb authored and nodejs-github-bot committed Nov 17, 2024
1 parent 23275cc commit e577618
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 56 deletions.
4 changes: 2 additions & 2 deletions doc/api/assert.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions doc/api/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand Down
17 changes: 6 additions & 11 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/test-runner/output/describe_it.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -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'
...
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/test-runner/output/dot_reporter.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
8 changes: 4 additions & 4 deletions test/fixtures/test-runner/output/junit_reporter.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/test-runner/output/output.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -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'
...
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/test-runner/output/output_cli.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -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'
...
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/test-runner/output/spec_reporter.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
});
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-console.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
42 changes: 22 additions & 20 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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}`
);
}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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' }"
);
}

Expand All @@ -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',
Expand Down Expand Up @@ -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');
Expand All @@ -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 }'
);
}

Expand Down Expand Up @@ -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 ]'
);
}

Expand Down Expand Up @@ -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 } }"
);
}

Expand Down Expand Up @@ -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,
Expand All @@ -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' +
'}',
);
}
10 changes: 5 additions & 5 deletions test/parallel/test-whatwg-encoding-custom-textdecoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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' +
'}'
);
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-whatwg-url-custom-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit e577618

Please sign in to comment.