lib: add support for inherited custom inspection methods

PR-URL: https://github.com/nodejs/node/pull/48306
Fixes: https://github.com/nodejs/node/issues/48207
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Antoine du Hamel 2023-06-11 10:10:47 +02:00 committed by GitHub
parent 0c875bbfc8
commit 2c6698b4db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 3 deletions

View File

@ -13,7 +13,6 @@ const {
ObjectGetOwnPropertyNames,
ObjectGetPrototypeOf,
ObjectKeys,
ObjectPrototypeHasOwnProperty,
ObjectPrototypeToString,
RangeError,
ReferenceError,
@ -134,8 +133,7 @@ function serializeError(error) {
// Continue regardless of error.
}
try {
if (error != null &&
ObjectPrototypeHasOwnProperty(error, customInspectSymbol)) {
if (error != null && customInspectSymbol in error) {
return Buffer.from(StringFromCharCode(kCustomInspectedObject) + inspect(error), 'utf8');
}
} catch {

View File

@ -125,3 +125,11 @@ const data = {
}
};
assert.strictEqual(inspect(cycle(data)), 'barbaz');
const inheritedCustomInspect = new class {
foo = 'bar';
[inspect.custom]() {
return 'barbaz';
}
}();
assert.strictEqual(inspect(cycle(inheritedCustomInspect)), 'barbaz');