Skip to content

Commit e577618

Browse files
ljharbnodejs-github-bot
authored andcommitted
util: inspect: enumerable Symbols no longer have square brackets
Implements https://github.com/orgs/nodejs/discussions/41283#discussioncomment-11188239 PR-URL: #55778 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: Chemi Atlow <[email protected]> Reviewed-By: Juan José Arboleda <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]>
1 parent 23275cc commit e577618

14 files changed

+53
-56
lines changed

doc/api/assert.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 });
878878
// AssertionError [ERR_ASSERTION]: Inputs identical but not reference equal:
879879
//
880880
// {
881-
// [Symbol()]: 1
881+
// Symbol(): 1
882882
// }
883883

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

976976
const weakMap1 = new WeakMap();

doc/api/events.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ myEmitter.on('event', function(a, b) {
7373
// _events: [Object: null prototype] { event: [Function (anonymous)] },
7474
// _eventsCount: 1,
7575
// _maxListeners: undefined,
76-
// [Symbol(shapeMode)]: false,
77-
// [Symbol(kCapture)]: false
76+
// Symbol(shapeMode): false,
77+
// Symbol(kCapture): false
7878
// } true
7979
});
8080
myEmitter.emit('event', 'a', 'b');
@@ -91,8 +91,8 @@ myEmitter.on('event', function(a, b) {
9191
// _events: [Object: null prototype] { event: [Function (anonymous)] },
9292
// _eventsCount: 1,
9393
// _maxListeners: undefined,
94-
// [Symbol(shapeMode)]: false,
95-
// [Symbol(kCapture)]: false
94+
// Symbol(shapeMode): false,
95+
// Symbol(kCapture): false
9696
// } true
9797
});
9898
myEmitter.emit('event', 'a', 'b');

lib/internal/util/inspect.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,21 +1999,16 @@ function formatProperty(ctx, value, recurseTimes, key, type, desc,
19991999
SymbolPrototypeToString(key),
20002000
escapeFn,
20012001
);
2002-
name = `[${ctx.stylize(tmp, 'symbol')}]`;
2003-
} else if (key === '__proto__') {
2004-
name = "['__proto__']";
2005-
} else if (desc.enumerable === false) {
2006-
const tmp = RegExpPrototypeSymbolReplace(
2007-
strEscapeSequencesReplacer,
2008-
key,
2009-
escapeFn,
2010-
);
2011-
name = `[${tmp}]`;
2002+
name = ctx.stylize(tmp, 'symbol');
20122003
} else if (RegExpPrototypeExec(keyStrRegExp, key) !== null) {
2013-
name = ctx.stylize(key, 'name');
2004+
name = key === '__proto__' ? "['__proto__']" : ctx.stylize(key, 'name');
20142005
} else {
20152006
name = ctx.stylize(strEscape(key), 'string');
20162007
}
2008+
2009+
if (desc.enumerable === false) {
2010+
name = `[${name}]`;
2011+
}
20172012
return `${name}:${extra}${str}`;
20182013
}
20192014

test/fixtures/test-runner/output/describe_it.snapshot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ not ok 50 - custom inspect symbol that throws fail
454454
error: |-
455455
{
456456
foo: 1,
457-
[Symbol(nodejs.util.inspect.custom)]: [Function: [nodejs.util.inspect.custom]]
457+
Symbol(nodejs.util.inspect.custom): [Function: [nodejs.util.inspect.custom]]
458458
}
459459
code: 'ERR_TEST_FAILURE'
460460
...

test/fixtures/test-runner/output/dot_reporter.snapshot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ Failed tests:
137137
✖ custom inspect symbol fail (*ms)
138138
customized
139139
✖ custom inspect symbol that throws fail (*ms)
140-
{ foo: 1, [Symbol(nodejs.util.inspect.custom)]: [Function: [nodejs.util.inspect.custom]] }
140+
{ foo: 1, Symbol(nodejs.util.inspect.custom): [Function: [nodejs.util.inspect.custom]] }
141141
✖ sync throw fails at first (*ms)
142142
Error: thrown from subtest sync throw fails at first
143143
*

test/fixtures/test-runner/output/junit_reporter.snapshot

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,15 +319,15 @@ Error [ERR_TEST_FAILURE]: thrown from callback async throw
319319
[Error [ERR_TEST_FAILURE]: customized] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: customized }
320320
</failure>
321321
</testcase>
322-
<testcase name="custom inspect symbol that throws fail" time="*" classname="test" failure="{ foo: 1, [Symbol(nodejs.util.inspect.custom)]: [Function: [nodejs.util.inspect.custom]]}">
323-
<failure type="testCodeFailure" message="{ foo: 1, [Symbol(nodejs.util.inspect.custom)]: [Function: [nodejs.util.inspect.custom]]}">
322+
<testcase name="custom inspect symbol that throws fail" time="*" classname="test" failure="{ foo: 1, Symbol(nodejs.util.inspect.custom): [Function: [nodejs.util.inspect.custom]]}">
323+
<failure type="testCodeFailure" message="{ foo: 1, Symbol(nodejs.util.inspect.custom): [Function: [nodejs.util.inspect.custom]]}">
324324
[Error [ERR_TEST_FAILURE]: {
325325
foo: 1,
326-
[Symbol(nodejs.util.inspect.custom)]: [Function: [nodejs.util.inspect.custom]]
326+
Symbol(nodejs.util.inspect.custom): [Function: [nodejs.util.inspect.custom]]
327327
}] {
328328
code: 'ERR_TEST_FAILURE',
329329
failureType: 'testCodeFailure',
330-
cause: { foo: 1, [Symbol(nodejs.util.inspect.custom)]: [Function: [nodejs.util.inspect.custom]] }
330+
cause: { foo: 1, Symbol(nodejs.util.inspect.custom): [Function: [nodejs.util.inspect.custom]] }
331331
}
332332
</failure>
333333
</testcase>

test/fixtures/test-runner/output/output.snapshot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ not ok 51 - custom inspect symbol that throws fail
502502
error: |-
503503
{
504504
foo: 1,
505-
[Symbol(nodejs.util.inspect.custom)]: [Function: [nodejs.util.inspect.custom]]
505+
Symbol(nodejs.util.inspect.custom): [Function: [nodejs.util.inspect.custom]]
506506
}
507507
code: 'ERR_TEST_FAILURE'
508508
...

test/fixtures/test-runner/output/output_cli.snapshot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ not ok 51 - custom inspect symbol that throws fail
509509
error: |-
510510
{
511511
foo: 1,
512-
[Symbol(nodejs.util.inspect.custom)]: [Function: [nodejs.util.inspect.custom]]
512+
Symbol(nodejs.util.inspect.custom): [Function: [nodejs.util.inspect.custom]]
513513
}
514514
code: 'ERR_TEST_FAILURE'
515515
...

test/fixtures/test-runner/output/spec_reporter.snapshot

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@
194194
customized
195195

196196
custom inspect symbol that throws fail (*ms)
197-
{ foo: 1, [Symbol(nodejs.util.inspect.custom)]: [Function: [nodejs.util.inspect.custom]] }
197+
{ foo: 1, Symbol(nodejs.util.inspect.custom): [Function: [nodejs.util.inspect.custom]] }
198198

199199
subtest sync throw fails
200200
sync throw fails at first (*ms)
@@ -476,7 +476,7 @@
476476

477477
*
478478
custom inspect symbol that throws fail (*ms)
479-
{ foo: 1, [Symbol(nodejs.util.inspect.custom)]: [Function: [nodejs.util.inspect.custom]] }
479+
{ foo: 1, Symbol(nodejs.util.inspect.custom): [Function: [nodejs.util.inspect.custom]] }
480480

481481
*
482482
sync throw fails at first (*ms)

test/parallel/test-assert.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ test('Test strict assert', () => {
801801
'\n' +
802802
'+ {}\n' +
803803
'- {\n' +
804-
'- [Symbol(nodejs.util.inspect.custom)]: [Function (anonymous)],\n' +
804+
'- Symbol(nodejs.util.inspect.custom): [Function (anonymous)],\n' +
805805
"- loop: 'forever'\n" +
806806
'- }\n'
807807
});

test/parallel/test-console.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,10 @@ for (const expected of expectedStrings) {
234234
}
235235

236236
assert.strictEqual(strings.shift(),
237-
"{\n foo: 'bar',\n [Symbol(nodejs.util.inspect.custom)]:" +
237+
"{\n foo: 'bar',\n Symbol(nodejs.util.inspect.custom):" +
238238
' [Function: [nodejs.util.inspect.custom]]\n}\n');
239239
assert.strictEqual(strings.shift(),
240-
"{\n foo: 'bar',\n [Symbol(nodejs.util.inspect.custom)]:" +
240+
"{\n foo: 'bar',\n Symbol(nodejs.util.inspect.custom):" +
241241
' [Function: [nodejs.util.inspect.custom]]\n}\n');
242242
assert.ok(strings.shift().includes('foo: [Object]'));
243243
assert.strictEqual(strings.shift().includes('baz'), false);

test/parallel/test-util-inspect.js

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ assert.strictEqual(
145145
Object.assign(new String('hello'), { [Symbol('foo')]: 123 }),
146146
{ showHidden: true }
147147
),
148-
"[String: 'hello'] { [length]: 5, [Symbol(foo)]: 123 }"
148+
"[String: 'hello'] { [length]: 5, Symbol(foo): 123 }"
149149
);
150150

151151
assert.match(util.inspect((new JSStream())._externalStream),
@@ -823,7 +823,7 @@ assert.strictEqual(util.inspect({ __proto__: Date.prototype }), 'Date {}');
823823
{
824824
const x = { [util.inspect.custom]: util.inspect };
825825
assert(util.inspect(x).includes(
826-
'[Symbol(nodejs.util.inspect.custom)]: [Function: inspect] {\n'));
826+
'Symbol(nodejs.util.inspect.custom): [Function: inspect] {\n'));
827827
}
828828

829829
// `util.inspect` should display the escaped value of a key.
@@ -1045,7 +1045,7 @@ util.inspect({ hasOwnProperty: null });
10451045
const UIC = 'nodejs.util.inspect.custom';
10461046
assert.strictEqual(
10471047
util.inspect(subject),
1048-
`{\n a: 123,\n [Symbol(${UIC})]: [Function: [${UIC}]]\n}`
1048+
`{\n a: 123,\n Symbol(${UIC}): [Function: [${UIC}]]\n}`
10491049
);
10501050
}
10511051

@@ -1145,27 +1145,29 @@ if (typeof Symbol !== 'undefined') {
11451145

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

1148-
assert.strictEqual(util.inspect(subject), '{ [Symbol(sym\\nbol)]: 42 }');
1148+
assert.strictEqual(util.inspect(subject), '{ Symbol(sym\\nbol): 42 }');
11491149
assert.strictEqual(
11501150
util.inspect(subject, options),
1151-
'{ [Symbol(sym\\nbol)]: 42 }'
1151+
'{ Symbol(sym\\nbol): 42 }'
11521152
);
11531153

11541154
Object.defineProperty(
11551155
subject,
11561156
Symbol(),
11571157
{ enumerable: false, value: 'non-enum' });
1158-
assert.strictEqual(util.inspect(subject), '{ [Symbol(sym\\nbol)]: 42 }');
1158+
assert.strictEqual(util.inspect(subject), '{ Symbol(sym\\nbol): 42 }');
11591159
assert.strictEqual(
11601160
util.inspect(subject, options),
1161-
"{ [Symbol(sym\\nbol)]: 42, [Symbol()]: 'non-enum' }"
1161+
"{ Symbol(sym\\nbol): 42, [Symbol()]: 'non-enum' }"
11621162
);
11631163

11641164
subject = [1, 2, 3];
11651165
subject[Symbol('symbol')] = 42;
11661166

1167-
assert.strictEqual(util.inspect(subject),
1168-
'[ 1, 2, 3, [Symbol(symbol)]: 42 ]');
1167+
assert.strictEqual(
1168+
util.inspect(subject),
1169+
'[ 1, 2, 3, Symbol(symbol): 42 ]'
1170+
);
11691171
}
11701172

11711173
// Test Set.
@@ -1589,7 +1591,7 @@ util.inspect(process);
15891591
const obj = { [util.inspect.custom]: 'fhqwhgads' };
15901592
assert.strictEqual(
15911593
util.inspect(obj),
1592-
"{ [Symbol(nodejs.util.inspect.custom)]: 'fhqwhgads' }"
1594+
"{ Symbol(nodejs.util.inspect.custom): 'fhqwhgads' }"
15931595
);
15941596
}
15951597

@@ -1598,7 +1600,7 @@ util.inspect(process);
15981600
const obj = { [Symbol.toStringTag]: 'a' };
15991601
assert.strictEqual(
16001602
util.inspect(obj),
1601-
"{ [Symbol(Symbol.toStringTag)]: 'a' }"
1603+
"{ Symbol(Symbol.toStringTag): 'a' }"
16021604
);
16031605
Object.defineProperty(obj, Symbol.toStringTag, {
16041606
value: 'a',
@@ -2268,7 +2270,7 @@ assert.strictEqual(util.inspect('"\'${a}'), "'\"\\'${a}'");
22682270
value[Symbol('foo')] = 'yeah';
22692271
res = util.inspect(value);
22702272
assert.notStrictEqual(res, expectedWithoutProto);
2271-
assert.match(res, /\[Symbol\(foo\)]: 'yeah'/);
2273+
assert.match(res, /Symbol\(foo\): 'yeah'/);
22722274
});
22732275

22742276
assert.strictEqual(inspect(1n), '1n');
@@ -2284,7 +2286,7 @@ assert.strictEqual(
22842286
Object.defineProperty(obj, 'Non\nenumerable\tkey', { value: true });
22852287
assert.strictEqual(
22862288
util.inspect(obj, { showHidden: true }),
2287-
'{ [Non\\nenumerable\\tkey]: true }'
2289+
'{ [\'Non\\nenumerable\\tkey\']: true }'
22882290
);
22892291
}
22902292

@@ -2375,7 +2377,7 @@ assert.strictEqual(
23752377
arr[Symbol('a')] = false;
23762378
assert.strictEqual(
23772379
inspect(arr, { sorted: true }),
2378-
'[ 3, 2, 1, [Symbol(a)]: false, [Symbol(b)]: true, a: 1, b: 2, c: 3 ]'
2380+
'[ 3, 2, 1, Symbol(a): false, Symbol(b): true, a: 1, b: 2, c: 3 ]'
23792381
);
23802382
}
23812383

@@ -3263,7 +3265,7 @@ assert.strictEqual(
32633265
Object.defineProperty(o, '__proto__', { enumerable: false });
32643266
assert.strictEqual(
32653267
util.inspect(o, { showHidden: true }),
3266-
"{ ['__proto__']: { a: 1 } }"
3268+
"{ [['__proto__']]: { a: 1 } }"
32673269
);
32683270
}
32693271

@@ -3328,11 +3330,11 @@ assert.strictEqual(
33283330
get [Symbol.iterator]() {
33293331
throw new Error();
33303332
}
3331-
}), '{ [Symbol(Symbol.iterator)]: [Getter] }');
3333+
}), '{ Symbol(Symbol.iterator): [Getter] }');
33323334
}
33333335

33343336
{
3335-
const sym = Symbol('bar');
3337+
const sym = Symbol('bar()');
33363338
const o = {
33373339
'foo': 0,
33383340
'Symbol(foo)': 0,
@@ -3347,9 +3349,9 @@ assert.strictEqual(
33473349
'{\n' +
33483350
' foo: 0,\n' +
33493351
" 'Symbol(foo)': 0,\n" +
3350-
' [Symbol(foo)]: 0,\n' +
3351-
' [Symbol(foo())]: 0,\n' +
3352-
' [Symbol(bar)]: 0\n' +
3352+
' Symbol(foo): 0,\n' +
3353+
' Symbol(foo()): 0,\n' +
3354+
' [Symbol(bar())]: 0\n' +
33533355
'}',
33543356
);
33553357
}

test/parallel/test-whatwg-encoding-custom-textdecoder.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ if (common.hasIntl) {
127127
' encoding: \'utf-8\',\n' +
128128
' fatal: false,\n' +
129129
' ignoreBOM: true,\n' +
130-
' [Symbol(flags)]: 4,\n' +
131-
' [Symbol(handle)]: undefined\n' +
130+
' Symbol(flags): 4,\n' +
131+
' Symbol(handle): undefined\n' +
132132
'}'
133133
);
134134
} else {
@@ -138,10 +138,10 @@ if (common.hasIntl) {
138138
" encoding: 'utf-8',\n" +
139139
' fatal: false,\n' +
140140
' ignoreBOM: true,\n' +
141-
' [Symbol(flags)]: 4,\n' +
142-
' [Symbol(handle)]: StringDecoder {\n' +
141+
' Symbol(flags): 4,\n' +
142+
' Symbol(handle): StringDecoder {\n' +
143143
" encoding: 'utf8',\n" +
144-
' [Symbol(kNativeDecoder)]: <Buffer 00 00 00 00 00 00 01>\n' +
144+
' Symbol(kNativeDecoder): <Buffer 00 00 00 00 00 00 01>\n' +
145145
' }\n' +
146146
'}'
147147
);

test/parallel/test-whatwg-url-custom-inspect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ assert.strictEqual(
4545
search: '?que=ry',
4646
searchParams: URLSearchParams { 'que' => 'ry' },
4747
hash: '#hash',
48-
[Symbol(context)]: URLContext {
48+
Symbol(context): URLContext {
4949
href: 'https://username:[email protected]:8080/path/name/?que=ry#hash',
5050
protocol_end: 6,
5151
username_end: 16,

0 commit comments

Comments
 (0)