diff options
author | Ruben Bridgewater <ruben@bridgewater.de> | 2019-05-16 13:25:59 +0200 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2019-05-20 14:15:42 +0200 |
commit | 370ddefc14a0bae1027749e0b75b65d0b6b786e9 (patch) | |
tree | c9eb4d7aca2e136be2fea6150e188365c55fe9eb /lib/internal/util/inspect.js | |
parent | 79e55f07741dcd31f28be4a322629400380fef3e (diff) | |
download | android-node-v8-370ddefc14a0bae1027749e0b75b65d0b6b786e9.tar.gz android-node-v8-370ddefc14a0bae1027749e0b75b65d0b6b786e9.tar.bz2 android-node-v8-370ddefc14a0bae1027749e0b75b65d0b6b786e9.zip |
util: unify constructor inspection in `util.inspect`
This makes sure that an objects constructor name is always returned
in a similar fashion instead of having different outputs depending
on the object shape and the code path taken.
PR-URL: https://github.com/nodejs/node/pull/27733
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaƫl Zasso <targos@protonmail.com>
Diffstat (limited to 'lib/internal/util/inspect.js')
-rw-r--r-- | lib/internal/util/inspect.js | 34 |
1 files changed, 19 insertions, 15 deletions
diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 2d5080bd6d..791c6653a7 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -418,8 +418,15 @@ function getKeys(value, showHidden) { return keys; } -function getCtxStyle(constructor, tag) { - return constructor || tag || 'Object'; +function getCtxStyle(value, constructor, tag) { + let fallback = ''; + if (constructor === null) { + fallback = internalGetConstructorName(value); + if (fallback === tag) { + fallback = 'Object'; + } + } + return getPrefix(constructor, tag, fallback); } function formatProxy(ctx, proxy, recurseTimes) { @@ -723,25 +730,21 @@ function formatRaw(ctx, value, recurseTimes, typedArray) { formatter = formatIterator; // Handle other regular objects again. } else { - let fallback = ''; - if (constructor === null) { - fallback = internalGetConstructorName(value); - if (fallback === tag) { - fallback = 'Object'; - } - } if (keys.length === 0) { if (isExternal(value)) return ctx.stylize('[External]', 'special'); - return `${getPrefix(constructor, tag, fallback)}{}`; + return `${getCtxStyle(value, constructor, tag)}{}`; } - braces[0] = `${getPrefix(constructor, tag, fallback)}{`; + braces[0] = `${getCtxStyle(value, constructor, tag)}{`; } } } if (recurseTimes > ctx.depth && ctx.depth !== null) { - return ctx.stylize(`[${getCtxStyle(constructor, tag)}]`, 'special'); + let constructorName = getCtxStyle(value, constructor, tag).slice(0, -1); + if (constructor !== null) + constructorName = `[${constructorName}]`; + return ctx.stylize(constructorName, 'special'); } recurseTimes += 1; @@ -756,7 +759,8 @@ function formatRaw(ctx, value, recurseTimes, typedArray) { formatProperty(ctx, value, recurseTimes, keys[i], extrasType)); } } catch (err) { - return handleMaxCallStackSize(ctx, err, constructor, tag, indentationLvl); + const constructorName = getCtxStyle(value, constructor, tag).slice(0, -1); + return handleMaxCallStackSize(ctx, err, constructorName, indentationLvl); } ctx.seen.pop(); @@ -1016,12 +1020,12 @@ function groupArrayElements(ctx, output) { return output; } -function handleMaxCallStackSize(ctx, err, constructor, tag, indentationLvl) { +function handleMaxCallStackSize(ctx, err, constructorName, indentationLvl) { if (isStackOverflowError(err)) { ctx.seen.pop(); ctx.indentationLvl = indentationLvl; return ctx.stylize( - `[${getCtxStyle(constructor, tag)}: Inspection interrupted ` + + `[${constructorName}: Inspection interrupted ` + 'prematurely. Maximum call stack size exceeded.]', 'special' ); |