Skip to content

Commit dc97b50

Browse files
authored
util: mark proxied objects as such when inspecting them
Fixes: #60964 PR-URL: #61029 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: LiviaMedeiros <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent e5b6f89 commit dc97b50

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

lib/internal/util/inspect.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1180,7 +1180,13 @@ function formatValue(ctx, value, recurseTimes, typedArray) {
11801180
return ctx.stylize(`[Circular *${index}]`, 'special');
11811181
}
11821182

1183-
return formatRaw(ctx, value, recurseTimes, typedArray);
1183+
const formatted = formatRaw(ctx, value, recurseTimes, typedArray);
1184+
1185+
if (proxy !== undefined) {
1186+
return `${ctx.stylize('Proxy(', 'special')}${formatted}${ctx.stylize(')', 'special')}`;
1187+
}
1188+
1189+
return formatted;
11841190
}
11851191

11861192
function formatRaw(ctx, value, recurseTimes, typedArray) {

test/parallel/test-assert-deep.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,7 @@ test('Check proxies', () => {
10681068
assert.throws(
10691069
() => assert.deepStrictEqual(arrProxy, [1, 2, 3]),
10701070
{ message: `${defaultMsgStartFull}\n\n` +
1071-
' [\n 1,\n 2,\n- 3\n ]\n' }
1071+
'+ Proxy([\n- [\n 1,\n 2,\n+ ])\n- 3\n- ]\n' }
10721072
);
10731073
util.inspect.defaultOptions = tmp;
10741074

test/parallel/test-repl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ const errorTests = [
730730
},
731731
{
732732
send: 'repl.writer.options.showProxy = false, new Proxy({x:42}, {});',
733-
expect: '{ x: 42 }'
733+
expect: 'Proxy({ x: 42 })'
734734
},
735735

736736
// Newline within template string maintains whitespace.

test/parallel/test-util-inspect-proxy.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ const proxy3 = new Proxy(proxy2, proxy1);
116116
const proxy4 = new Proxy(proxy1, proxy2);
117117
const proxy5 = new Proxy(proxy3, proxy4);
118118
const proxy6 = new Proxy(proxy5, proxy5);
119-
const expected0 = '{}';
119+
const expected0 = 'Proxy({})';
120120
const expected1 = 'Proxy [ {}, {} ]';
121121
const expected2 = 'Proxy [ Proxy [ {}, {} ], {} ]';
122122
const expected3 = 'Proxy [ Proxy [ Proxy [ {}, {} ], {} ], Proxy [ {}, {} ] ]';
@@ -154,7 +154,7 @@ assert.strictEqual(util.inspect(proxy6), expected0);
154154
const proxy7 = new Proxy([], []);
155155
const expected7 = 'Proxy [ [], [] ]';
156156
assert.strictEqual(util.inspect(proxy7, opts), expected7);
157-
assert.strictEqual(util.inspect(proxy7), '[]');
157+
assert.strictEqual(util.inspect(proxy7), 'Proxy([])');
158158

159159
// Now we're just getting silly, right?
160160
const proxy8 = new Proxy(Date, []);
@@ -163,8 +163,8 @@ const expected8 = 'Proxy [ [Function: Date], [] ]';
163163
const expected9 = 'Proxy [ [Function: Date], [Function: String] ]';
164164
assert.strictEqual(util.inspect(proxy8, opts), expected8);
165165
assert.strictEqual(util.inspect(proxy9, opts), expected9);
166-
assert.strictEqual(util.inspect(proxy8), '[Function: Date]');
167-
assert.strictEqual(util.inspect(proxy9), '[Function: Date]');
166+
assert.strictEqual(util.inspect(proxy8), 'Proxy([Function: Date])');
167+
assert.strictEqual(util.inspect(proxy9), 'Proxy([Function: Date])');
168168

169169
const proxy10 = new Proxy(() => {}, {});
170170
const proxy11 = new Proxy(() => {}, {
@@ -175,7 +175,16 @@ const proxy11 = new Proxy(() => {}, {
175175
return proxy11;
176176
}
177177
});
178-
const expected10 = '[Function (anonymous)]';
179-
const expected11 = '[Function (anonymous)]';
178+
const expected10 = 'Proxy([Function (anonymous)])';
179+
const expected11 = 'Proxy([Function (anonymous)])';
180180
assert.strictEqual(util.inspect(proxy10), expected10);
181181
assert.strictEqual(util.inspect(proxy11), expected11);
182+
183+
const proxy12 = new Proxy([1, 2, 3], proxy5);
184+
assert.strictEqual(
185+
util.inspect(proxy12, { colors: true, breakLength: 1 }),
186+
'\x1B[36mProxy(\x1B[39m' +
187+
'[\n \x1B[33m1\x1B[39m,\n \x1B[33m2\x1B[39m,\n \x1B[33m3\x1B[39m\n]\x1B[36m' +
188+
')\x1B[39m'
189+
);
190+
assert.strictEqual(util.format('%s', proxy12), 'Proxy([ 1, 2, 3 ])');

0 commit comments

Comments
 (0)