Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fixup: simplify the code and change visualization
Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com
  • Loading branch information
daeyeon committed May 19, 2022
commit 13964d0edd9f482db6b3633cfcbffab27fdb9406
40 changes: 19 additions & 21 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -724,18 +724,16 @@ function getCtxStyle(value, constructor, tag) {
return getPrefix(constructor, tag, fallback);
}

function formatProxy(ctx, target, handler, recurseTimes, showProperties = true) {
function formatProxy(ctx, proxy, recurseTimes) {
if (recurseTimes > ctx.depth && ctx.depth !== null) {
return ctx.stylize('Proxy [Array]', 'special');
}
recurseTimes += 1;
ctx.indentationLvl += 2;
const res = showProperties ?
[
formatValue(ctx, target, recurseTimes),
formatValue(ctx, handler, recurseTimes),
] :
[];
const res = [
formatValue(ctx, proxy[0], recurseTimes),
formatValue(ctx, proxy[1], recurseTimes),
];
ctx.indentationLvl -= 2;
return reduceToSingleString(
ctx, res, '', ['Proxy [', ']'], kArrayExtrasType, recurseTimes);
Expand All @@ -759,15 +757,15 @@ function formatValue(ctx, value, recurseTimes, typedArray) {
const context = value;
// Always check for proxies to prevent side effects and to prevent triggering
// any proxy handlers.
const details = getProxyDetails(value, !!ctx.showProxy);
if (details !== undefined) {
const proxy = getProxyDetails(value, !!ctx.showProxy);
if (proxy !== undefined) {
if (proxy === null || proxy[0] === null) {
return ctx.stylize('<Revoked Proxy>', 'special');
}
if (ctx.showProxy) {
return formatProxy(ctx, details[0], details[1], recurseTimes);
} else if (details === null) {
// The proxy is revoked. Both target and handler of it are null.
return formatProxy(ctx, details, null, recurseTimes, ctx.showProxy);
return formatProxy(ctx, proxy, recurseTimes);
}
value = details;
value = proxy;
}

// Provide a hook for user-specified inspect functions.
Expand All @@ -783,7 +781,7 @@ function formatValue(ctx, value, recurseTimes, typedArray) {
// a counter internally.
const depth = ctx.depth === null ? null : ctx.depth - recurseTimes;
const isCrossContext =
details !== undefined || !(context instanceof Object);
proxy !== undefined || !(context instanceof Object);
const ret = FunctionPrototypeCall(
maybeCustom,
context,
Expand Down Expand Up @@ -1939,12 +1937,9 @@ function reduceToSingleString(
braces[0].length + base.length + 10;
if (isBelowBreakLength(ctx, output, start, base)) {
const joinedOutput = join(output, ', ');
const space = joinedOutput.length > 0 ? ' ' : '';
if (!joinedOutput.includes('\n')) {
return (
`${base ? `${base} ` : ''}${braces[0]}${space}${joinedOutput}` +
`${space}${braces[1]}`
);
return `${base ? `${base} ` : ''}${braces[0]} ${joinedOutput}` +
` ${braces[1]}`;
}
}
}
Expand Down Expand Up @@ -1975,11 +1970,14 @@ function hasBuiltInToString(value) {
const getFullProxy = false;
const proxyTarget = getProxyDetails(value, getFullProxy);
if (proxyTarget !== undefined) {
if (proxyTarget === null) {
return true;
}
value = proxyTarget;
}

// Count objects that have no `toString` function as built-in.
if (typeof value?.toString !== 'function') {
if (typeof value.toString !== 'function') {
return true;
}

Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-util-inspect-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ assert.strictEqual(details[1], null);
details = processUtil.getProxyDetails(r.proxy, false);
assert.strictEqual(details, null);

assert.strictEqual(util.inspect(r.proxy), 'Proxy []');
assert.strictEqual(util.inspect(r.proxy), '<Revoked Proxy>');
assert.strictEqual(
util.inspect(r, { showProxy: true }),
'{ proxy: Proxy [ null, null ], revoke: [Function (anonymous)] }',
'{ proxy: <Revoked Proxy>, revoke: [Function (anonymous)] }',
);

assert.strictEqual(util.format('%s', r.proxy), 'Proxy []');
assert.strictEqual(util.format('%s', r.proxy), '<Revoked Proxy>');

assert.strictEqual(
util.inspect(proxyObj, opts),
Expand Down