Skip to content

Commit dadd6e1

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 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 16210ca commit dadd6e1

File tree

5 files changed

+63
-15
lines changed

5 files changed

+63
-15
lines changed

doc/api/util.md

+40-6
Original file line numberDiff line numberDiff line change
@@ -574,9 +574,10 @@ terminals.
574574

575575
<!-- type=misc -->
576576

577-
Objects may also define their own `[util.inspect.custom](depth, opts)` function
578-
that `util.inspect()` will invoke and use the result of when inspecting the
579-
object:
577+
Objects may also define their own
578+
[`[util.inspect.custom](depth, opts)`][util.inspect.custom] function,
579+
which `util.inspect()` will invoke and use the result of when inspecting
580+
the object:
580581

581582
```js
582583
const util = require('util');
@@ -628,10 +629,41 @@ util.inspect(obj);
628629
### util.inspect.custom
629630
<!-- YAML
630631
added: v6.6.0
632+
changes:
633+
- version: REPLACEME
634+
pr-url: https://github.com/nodejs/node/pull/20857
635+
description: This is now defined as a shared symbol.
631636
-->
632637

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

636668
### util.inspect.defaultOptions
637669
<!-- 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
@@ -400,7 +400,7 @@ module.exports = {
400400

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

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

test/parallel/test-assert.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ assert.throws(
603603
'+ {}\n' +
604604
'- {\n' +
605605
"- loop: 'forever',\n" +
606-
'- [Symbol(util.inspect.custom)]: [Function]\n' +
606+
'- [Symbol(nodejs.util.inspect.custom)]: [Function]\n' +
607607
'- }'
608608
});
609609

test/parallel/test-console.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,11 @@ for (const expected of expectedStrings) {
214214
}
215215

216216
assert.strictEqual(strings.shift(),
217-
"{ foo: 'bar',\n [Symbol(util.inspect.custom)]: " +
218-
'[Function: [util.inspect.custom]] }\n');
217+
"{ foo: 'bar',\n [Symbol(nodejs.util.inspect.custom)]: " +
218+
'[Function: [nodejs.util.inspect.custom]] }\n');
219219
assert.strictEqual(strings.shift(),
220-
"{ foo: 'bar',\n [Symbol(util.inspect.custom)]: " +
221-
'[Function: [util.inspect.custom]] }\n');
220+
"{ foo: 'bar',\n [Symbol(nodejs.util.inspect.custom)]: " +
221+
'[Function: [nodejs.util.inspect.custom]] }\n');
222222
assert.ok(strings.shift().includes('foo: [Object]'));
223223
assert.strictEqual(strings.shift().includes('baz'), false);
224224
assert.strictEqual(strings.shift(), 'inspect inspect\n');

test/parallel/test-util-inspect.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ assert.strictEqual(util.inspect(Object.create(Date.prototype)), 'Date {}');
627627
{
628628
const x = { [util.inspect.custom]: util.inspect };
629629
assert(util.inspect(x).includes(
630-
'[Symbol(util.inspect.custom)]:\n { [Function: inspect]'));
630+
'[Symbol(nodejs.util.inspect.custom)]:\n { [Function: inspect]'));
631631
}
632632

633633
// `util.inspect` should display the escaped value of a key.
@@ -758,12 +758,26 @@ util.inspect({ hasOwnProperty: null });
758758
};
759759

760760
util.inspect(subject, { customInspectOptions: true });
761+
762+
// util.inspect.custom is a shared symbol which can be accessed as
763+
// Symbol.for("nodejs.util.inspect.custom").
764+
const inspect = Symbol.for('nodejs.util.inspect.custom');
765+
766+
subject[inspect] = () => ({ baz: 'quux' });
767+
768+
assert.strictEqual(util.inspect(subject), '{ baz: \'quux\' }');
769+
770+
subject[inspect] = (depth, opts) => {
771+
assert.strictEqual(opts.customInspectOptions, true);
772+
};
773+
774+
util.inspect(subject, { customInspectOptions: true });
761775
}
762776

763777
{
764778
// Returning `this` from a custom inspection function works.
765779
const subject = { a: 123, [util.inspect.custom]() { return this; } };
766-
const UIC = 'util.inspect.custom';
780+
const UIC = 'nodejs.util.inspect.custom';
767781
assert.strictEqual(util.inspect(subject),
768782
`{ a: 123,\n [Symbol(${UIC})]: [Function: [${UIC}]] }`);
769783
}
@@ -1188,7 +1202,7 @@ util.inspect(process);
11881202
const obj = { [util.inspect.custom]: 'fhqwhgads' };
11891203
assert.strictEqual(
11901204
util.inspect(obj),
1191-
"{ [Symbol(util.inspect.custom)]: 'fhqwhgads' }"
1205+
"{ [Symbol(nodejs.util.inspect.custom)]: 'fhqwhgads' }"
11921206
);
11931207
}
11941208

0 commit comments

Comments
 (0)