From a1f581a61eae6ed3d83ec9c53b4a7ca3eb9e6e3b Mon Sep 17 00:00:00 2001 From: cola119 Date: Wed, 20 Apr 2022 10:48:52 +0900 Subject: [PATCH] util: fix TypeError of symbol in template literals PR-URL: https://github.com/nodejs/node/pull/42790 Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater --- lib/internal/util/inspect.js | 2 +- test/parallel/test-util-inspect.js | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 6569822096a689..948e2f354ac96d 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -581,7 +581,7 @@ function getConstructorName(obj, ctx, recurseTimes, protoProps) { addPrototypeProperties( ctx, tmp, firstProto || tmp, recurseTimes, protoProps); } - return descriptor.value.name; + return String(descriptor.value.name); } obj = ObjectGetPrototypeOf(obj); diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index 8a32c6532223c2..8092c658966493 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -1396,6 +1396,9 @@ if (typeof Symbol !== 'undefined') { class SetSubclass extends Set {} class MapSubclass extends Map {} class PromiseSubclass extends Promise {} + class SymbolNameClass { + static name = Symbol('name'); + } const x = new ObjectSubclass(); x.foo = 42; @@ -1409,6 +1412,8 @@ if (typeof Symbol !== 'undefined') { "MapSubclass(1) [Map] { 'foo' => 42 }"); assert.strictEqual(util.inspect(new PromiseSubclass(() => {})), 'PromiseSubclass [Promise] { }'); + assert.strictEqual(util.inspect(new SymbolNameClass()), + 'Symbol(name) {}'); assert.strictEqual( util.inspect({ a: { b: new ArraySubclass([1, [2], 3]) } }, { depth: 1 }), '{ a: { b: [ArraySubclass] } }'