Skip to content

Commit a2a1ebf

Browse files
chocolateboytargos
authored andcommitted
util: use a shared symbol for util.inspect.custom
Define `util.inspect.custom` as `Symbol.for("nodejs.util.inspect.custom")` rather than `Symbol("util.inspect.custom")`. This allows `inspect` hooks to easily/safely be defined in non-Node.js environments. Fixes: #20821 Refs: #22684 Backport-PR-URL: #23039 PR-URL: #20857 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: John-David Dalton <[email protected]>
1 parent 077e7e0 commit a2a1ebf

File tree

5 files changed

+69
-17
lines changed

5 files changed

+69
-17
lines changed

doc/api/util.md

+40-6
Original file line numberDiff line numberDiff line change
@@ -572,9 +572,10 @@ terminals.
572572

573573
<!-- type=misc -->
574574

575-
Objects may also define their own `[util.inspect.custom](depth, opts)`
576-
(or the equivalent but deprecated `inspect(depth, opts)`) function that
577-
`util.inspect()` will invoke and use the result of when inspecting the object:
575+
Objects may also define their own
576+
[`[util.inspect.custom](depth, opts)`][util.inspect.custom] (or the equivalent
577+
but deprecated `inspect(depth, opts)`) function, which `util.inspect()` will
578+
invoke and use the result of when inspecting the object:
578579

579580
```js
580581
const util = require('util');
@@ -626,10 +627,41 @@ util.inspect(obj);
626627
### util.inspect.custom
627628
<!-- YAML
628629
added: v6.6.0
630+
changes:
631+
- version: REPLACEME
632+
pr-url: https://github.com/nodejs/node/pull/20857
633+
description: This is now defined as a shared symbol.
629634
-->
630635

631-
A {symbol} that can be used to declare custom inspect functions, see
632-
[Custom inspection functions on Objects][].
636+
* {symbol} that can be used to declare custom inspect functions.
637+
638+
In addition to being accessible through `util.inspect.custom`, this
639+
symbol is [registered globally][global symbol registry] and can be
640+
accessed in any environment as `Symbol.for('nodejs.util.inspect.custom')`.
641+
642+
```js
643+
const inspect = Symbol.for('nodejs.util.inspect.custom');
644+
645+
class Password {
646+
constructor(value) {
647+
this.value = value;
648+
}
649+
650+
toString() {
651+
return 'xxxxxxxx';
652+
}
653+
654+
[inspect]() {
655+
return `Password <${this.toString()}>`;
656+
}
657+
}
658+
659+
const password = new Password('r0sebud');
660+
console.log(password);
661+
// Prints Password <xxxxxxxx>
662+
```
663+
664+
See [Custom inspection functions on Objects][] for more details.
633665

634666
### util.inspect.defaultOptions
635667
<!-- YAML
@@ -2076,7 +2108,6 @@ Deprecated predecessor of `console.log`.
20762108
[`Array.isArray()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray
20772109
[`ArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
20782110
[`ArrayBuffer.isView()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/isView
2079-
[async function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
20802111
[`assert.deepStrictEqual()`]: assert.html#assert_assert_deepstrictequal_actual_expected_message
20812112
[`Buffer.isBuffer()`]: buffer.html#buffer_class_method_buffer_isbuffer_obj
20822113
[`console.error()`]: console.html#console_console_error_data_args
@@ -2118,6 +2149,9 @@ Deprecated predecessor of `console.log`.
21182149
[Module Namespace Object]: https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects
21192150
[WHATWG Encoding Standard]: https://encoding.spec.whatwg.org/
21202151
[Common System Errors]: errors.html#errors_common_system_errors
2152+
[async function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
21212153
[constructor]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor
2154+
[global symbol registry]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/for
21222155
[list of deprecated APIS]: deprecations.html#deprecations_list_of_deprecated_apis
21232156
[semantically incompatible]: https://github.com/nodejs/node/issues/4179
2157+
[util.inspect.custom]: #util_util_inspect_custom

lib/internal/util.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ module.exports = {
399399

400400
// Symbol used to provide a custom inspect function for an object as an
401401
// alternative to using 'inspect'
402-
customInspectSymbol: Symbol('util.inspect.custom'),
402+
customInspectSymbol: Symbol.for('nodejs.util.inspect.custom'),
403403

404404
// Used by the buffer module to capture an internal reference to the
405405
// default isEncoding implementation, just in case userland overrides it.

test/parallel/test-assert.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ assert.throws(
594594
'- {}\n' +
595595
'+ {\n' +
596596
"+ loop: 'forever',\n" +
597-
'+ [Symbol(util.inspect.custom)]: [Function]\n' +
597+
'+ [Symbol(nodejs.util.inspect.custom)]: [Function]\n' +
598598
'+ }'
599599
});
600600

test/parallel/test-console.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,11 @@ for (const expected of expectedStrings) {
202202
}
203203

204204
assert.strictEqual(strings.shift(),
205-
"{ foo: 'bar',\n [Symbol(util.inspect.custom)]: " +
206-
'[Function: [util.inspect.custom]] }\n');
205+
"{ foo: 'bar',\n [Symbol(nodejs.util.inspect.custom)]: " +
206+
'[Function: [nodejs.util.inspect.custom]] }\n');
207207
assert.strictEqual(strings.shift(),
208-
"{ foo: 'bar',\n [Symbol(util.inspect.custom)]: " +
209-
'[Function: [util.inspect.custom]] }\n');
208+
"{ foo: 'bar',\n [Symbol(nodejs.util.inspect.custom)]: " +
209+
'[Function: [nodejs.util.inspect.custom]] }\n');
210210
assert.ok(strings.shift().includes('foo: [Object]'));
211211
assert.strictEqual(strings.shift().includes('baz'), false);
212212
assert.strictEqual(strings.shift(), 'inspect inspect\n');

test/parallel/test-util-inspect.js

+23-5
Original file line numberDiff line numberDiff line change
@@ -624,8 +624,9 @@ util.inspect([{ inspect: () => 123 }]);
624624

625625
// GH-2225
626626
{
627-
const x = { inspect: util.inspect };
628-
assert.strictEqual(util.inspect(x).includes('inspect'), true);
627+
const x = { [util.inspect.custom]: util.inspect };
628+
assert(util.inspect(x).includes(
629+
'[Symbol(nodejs.util.inspect.custom)]:\n { [Function: inspect]'));
629630
}
630631

631632
// util.inspect should display the escaped value of a key.
@@ -781,6 +782,20 @@ util.inspect({ hasOwnProperty: null });
781782
};
782783

783784
util.inspect(subject, { customInspectOptions: true });
785+
786+
// util.inspect.custom is a shared symbol which can be accessed as
787+
// Symbol.for("nodejs.util.inspect.custom").
788+
const inspect = Symbol.for('nodejs.util.inspect.custom');
789+
790+
subject[inspect] = () => ({ baz: 'quux' });
791+
792+
assert.strictEqual(util.inspect(subject), '{ baz: \'quux\' }');
793+
794+
subject[inspect] = (depth, opts) => {
795+
assert.strictEqual(opts.customInspectOptions, true);
796+
};
797+
798+
util.inspect(subject, { customInspectOptions: true });
784799
}
785800

786801
{
@@ -814,7 +829,7 @@ util.inspect({ hasOwnProperty: null });
814829
'{ a: 123, inspect: [Function: inspect] }');
815830

816831
const subject = { a: 123, [util.inspect.custom]() { return this; } };
817-
const UIC = 'util.inspect.custom';
832+
const UIC = 'nodejs.util.inspect.custom';
818833
assert.strictEqual(util.inspect(subject),
819834
`{ a: 123,\n [Symbol(${UIC})]: [Function: [${UIC}]] }`);
820835
}
@@ -1235,8 +1250,11 @@ util.inspect(process);
12351250

12361251
// Setting custom inspect property to a non-function should do nothing.
12371252
{
1238-
const obj = { inspect: 'fhqwhgads' };
1239-
assert.strictEqual(util.inspect(obj), "{ inspect: 'fhqwhgads' }");
1253+
const obj = { [util.inspect.custom]: 'fhqwhgads' };
1254+
assert.strictEqual(
1255+
util.inspect(obj),
1256+
"{ [Symbol(nodejs.util.inspect.custom)]: 'fhqwhgads' }"
1257+
);
12401258
}
12411259

12421260
{

0 commit comments

Comments
 (0)