From a25730bda39b862da24a1dd633574cfebb926ee1 Mon Sep 17 00:00:00 2001 From: Gus Caplan Date: Fri, 25 May 2018 08:36:26 -0500 Subject: [PATCH] util: fix inspection of module namespaces MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/20962 Reviewed-By: Guy Bedford Reviewed-By: Matheus Marchini Reviewed-By: Michaƫl Zasso Reviewed-By: Ruben Bridgewater --- lib/util.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/util.js b/lib/util.js index 27ad26d746055b..d5c3b3a8be8cbc 100644 --- a/lib/util.js +++ b/lib/util.js @@ -478,7 +478,23 @@ function formatValue(ctx, value, recurseTimes) { if (ctx.showHidden) { keys = Object.getOwnPropertyNames(value); } else { - keys = Object.keys(value); + // This might throw if `value` is a Module Namespace Object from an + // unevaluated module, but we don't want to perform the actual type + // check because it's expensive. + // TODO(devsnek): track https://github.com/tc39/ecma262/issues/1209 + // and modify this logic as needed. + try { + keys = Object.keys(value); + } catch (err) { + if (types.isNativeError(err) && + err.name === 'ReferenceError' && + types.isModuleNamespaceObject(value)) { + keys = Object.getOwnPropertyNames(value); + } else { + throw err; + } + } + if (symbols.length !== 0) symbols = symbols.filter((key) => propertyIsEnumerable.call(value, key)); } @@ -772,7 +788,7 @@ function formatNamespaceObject(ctx, value, recurseTimes, keys) { try { output[i] = formatProperty(ctx, value, recurseTimes, keys[i], 0); } catch (err) { - if (!(err instanceof ReferenceError)) { + if (!(types.isNativeError(err) && err.name === 'ReferenceError')) { throw err; } // Use the existing functionality. This makes sure the indentation and