diff options
author | Gus Caplan <me@gus.host> | 2018-05-16 12:07:20 -0500 |
---|---|---|
committer | Gus Caplan <me@gus.host> | 2018-05-18 20:40:41 -0500 |
commit | 064057b7ad0ee6f76f43846b4f002072dafa1702 (patch) | |
tree | 8ffa9c433580b07edb966d4ed2109d97d02a62c8 /lib/util.js | |
parent | 70cc5da0f11a024cf5be1ff20fd885556c1d2153 (diff) | |
download | android-node-v8-064057b7ad0ee6f76f43846b4f002072dafa1702.tar.gz android-node-v8-064057b7ad0ee6f76f43846b4f002072dafa1702.tar.bz2 android-node-v8-064057b7ad0ee6f76f43846b4f002072dafa1702.zip |
util: support inspecting namespaces of unevaluated modules
PR-URL: https://github.com/nodejs/node/pull/20782
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib/util.js')
-rw-r--r-- | lib/util.js | 53 |
1 files changed, 42 insertions, 11 deletions
diff --git a/lib/util.js b/lib/util.js index f358adefdb..2ef7e4e54c 100644 --- a/lib/util.js +++ b/lib/util.js @@ -629,6 +629,9 @@ function formatValue(ctx, value, recurseTimes, ln) { } else { extra = '[items unknown]'; } + } else if (types.isModuleNamespaceObject(value)) { + braces[0] = `[${tag}] {`; + formatter = formatNamespaceObject; } else { // Check boxed primitives other than string with valueOf() // NOTE: `Date` has to be checked first! @@ -757,6 +760,15 @@ function formatObject(ctx, value, recurseTimes, keys) { return output; } +function formatNamespaceObject(ctx, value, recurseTimes, keys) { + const len = keys.length; + const output = new Array(len); + for (var i = 0; i < len; i++) { + output[i] = formatNamespaceProperty(ctx, value, recurseTimes, keys[i]); + } + return output; +} + // The array is sparse and/or has extra keys function formatSpecialArray(ctx, value, recurseTimes, keys, maxLength, valLen) { const output = []; @@ -980,8 +992,36 @@ function formatPromise(ctx, value, recurseTimes, keys) { return output; } +function formatKey(ctx, key, enumerable) { + if (typeof key === 'symbol') { + return `[${ctx.stylize(key.toString(), 'symbol')}]`; + } + if (enumerable === false) { + return `[${key}]`; + } + if (keyStrRegExp.test(key)) { + return ctx.stylize(key, 'name'); + } + return ctx.stylize(strEscape(key), 'string'); +} + +function formatNamespaceProperty(ctx, ns, recurseTimes, key) { + let value; + try { + value = formatValue(ctx, ns[key], recurseTimes, true); + } catch (err) { + if (err instanceof ReferenceError) { + value = ctx.stylize('<uninitialized>', 'special'); + } else { + throw err; + } + } + + return `${formatKey(ctx, key)}: ${value}`; +} + function formatProperty(ctx, value, recurseTimes, key, array) { - let name, str; + let str; const desc = Object.getOwnPropertyDescriptor(value, key) || { value: value[key], enumerable: true }; if (desc.value !== undefined) { @@ -1003,17 +1043,8 @@ function formatProperty(ctx, value, recurseTimes, key, array) { if (array === 1) { return str; } - if (typeof key === 'symbol') { - name = `[${ctx.stylize(key.toString(), 'symbol')}]`; - } else if (desc.enumerable === false) { - name = `[${key}]`; - } else if (keyStrRegExp.test(key)) { - name = ctx.stylize(key, 'name'); - } else { - name = ctx.stylize(strEscape(key), 'string'); - } - return `${name}: ${str}`; + return `${formatKey(ctx, key, desc.enumerable)}: ${str}`; } function reduceToSingleString(ctx, output, base, braces, addLn) { |