Skip to content

Commit e652781

Browse files
ljharbaduh95
authored andcommitted
util: do not crash on inspecting function with Symbol name
Refs: #56570 PR-URL: #56572 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 33f5345 commit e652781

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

lib/internal/util/inspect.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,7 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
989989
keys = getKeys(value, ctx.showHidden);
990990
braces = ['{', '}'];
991991
if (typeof value === 'function') {
992-
base = getFunctionBase(value, constructor, tag);
992+
base = getFunctionBase(ctx, value, constructor, tag);
993993
if (keys.length === 0 && protoProps === undefined)
994994
return ctx.stylize(base, 'special');
995995
} else if (constructor === 'Object') {
@@ -1223,7 +1223,7 @@ function getClassBase(value, constructor, tag) {
12231223
return `[${base}]`;
12241224
}
12251225

1226-
function getFunctionBase(value, constructor, tag) {
1226+
function getFunctionBase(ctx, value, constructor, tag) {
12271227
const stringified = FunctionPrototypeToString(value);
12281228
if (StringPrototypeStartsWith(stringified, 'class') && stringified[stringified.length - 1] === '}') {
12291229
const slice = StringPrototypeSlice(stringified, 5, -1);
@@ -1250,7 +1250,7 @@ function getFunctionBase(value, constructor, tag) {
12501250
if (value.name === '') {
12511251
base += ' (anonymous)';
12521252
} else {
1253-
base += `: ${value.name}`;
1253+
base += `: ${typeof value.name === 'string' ? value.name : formatValue(ctx, value.name)}`;
12541254
}
12551255
base += ']';
12561256
if (constructor !== type && constructor !== null) {

test/parallel/test-util-inspect.js

+10
Original file line numberDiff line numberDiff line change
@@ -3394,3 +3394,13 @@ assert.strictEqual(
33943394
Object.defineProperty(BuiltinPrototype, 'constructor', desc);
33953395
}
33963396
}
3397+
3398+
{
3399+
function f() {}
3400+
Object.defineProperty(f, 'name', { value: Symbol('f') });
3401+
3402+
assert.strictEqual(
3403+
util.inspect(f),
3404+
'[Function: Symbol(f)]',
3405+
);
3406+
}

0 commit comments

Comments
 (0)