From 37976251b5f0ed16957de014c94324dd76fc4df1 Mon Sep 17 00:00:00 2001 From: Weijia Wang Date: Fri, 28 Dec 2018 10:03:00 +0800 Subject: [PATCH] util: fix util.inspect with proxied function PR-URL: https://github.com/nodejs/node/pull/25244 Fixes: https://github.com/nodejs/node/issues/25212 Reviewed-By: Ben Noordhuis Reviewed-By: Minwoo Jung Reviewed-By: Colin Ihrig Reviewed-By: Ujjwal Sharma --- lib/internal/util/inspect.js | 5 ++++- test/parallel/test-util-inspect-proxy.js | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index b47ed5411f46fb..4697829fba2756 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -628,7 +628,10 @@ function formatRaw(ctx, value, recurseTimes) { } } else if (typeof value === 'function') { const type = constructor || tag || 'Function'; - const name = `${type}${value.name ? `: ${value.name}` : ''}`; + let name = `${type}`; + if (value.name && typeof value.name === 'string') { + name += `: ${value.name}`; + } if (keys.length === 0) return ctx.stylize(`[${name}]`, 'special'); base = `[${name}]`; diff --git a/test/parallel/test-util-inspect-proxy.js b/test/parallel/test-util-inspect-proxy.js index b3438a625240a2..adc0810722d553 100644 --- a/test/parallel/test-util-inspect-proxy.js +++ b/test/parallel/test-util-inspect-proxy.js @@ -87,3 +87,17 @@ assert.strictEqual(util.inspect(proxy8, opts), expected8); assert.strictEqual(util.inspect(proxy9, opts), expected9); assert.strictEqual(util.inspect(proxy8), '[Function: Date]'); assert.strictEqual(util.inspect(proxy9), '[Function: Date]'); + +const proxy10 = new Proxy(() => {}, {}); +const proxy11 = new Proxy(() => {}, { + get() { + return proxy11; + }, + apply() { + return proxy11; + } +}); +const expected10 = '[Function]'; +const expected11 = '[Function]'; +assert.strictEqual(util.inspect(proxy10), expected10); +assert.strictEqual(util.inspect(proxy11), expected11);