Repository: nodejs/node
Pull request: util: mark proxied objects as such when inspecting them

Fixes: https://github.com/nodejs/node/issues/60964

Changed files:
filename: lib/internal/util/inspect.js
patch: @@ -1180,7 +1180,13 @@ function formatValue(ctx, value, recurseTimes, typedArray) {
     return ctx.stylize(`[Circular *${index}]`, 'special');
   }
 
-  return formatRaw(ctx, value, recurseTimes, typedArray);
+  const formatted = formatRaw(ctx, value, recurseTimes, typedArray);
+
+  if (proxy !== undefined) {
+    return `${ctx.stylize('Proxy(', 'special')}${formatted}${ctx.stylize(')', 'special')}`;
+  }
+
+  return formatted;
 }
 
 function formatRaw(ctx, value, recurseTimes, typedArray) {

filename: test/parallel/test-assert-deep.js
patch: @@ -1068,7 +1068,7 @@ test('Check proxies', () => {
   assert.throws(
     () => assert.deepStrictEqual(arrProxy, [1, 2, 3]),
     { message: `${defaultMsgStartFull}\n\n` +
-               '  [\n    1,\n    2,\n-   3\n  ]\n' }
+               '+ Proxy([\n- [\n    1,\n    2,\n+ ])\n-   3\n- ]\n' }
   );
   util.inspect.defaultOptions = tmp;
 

filename: test/parallel/test-repl.js
patch: @@ -730,7 +730,7 @@ const errorTests = [
   },
   {
     send: 'repl.writer.options.showProxy = false, new Proxy({x:42}, {});',
-    expect: '{ x: 42 }'
+    expect: 'Proxy({ x: 42 })'
   },
 
   // Newline within template string maintains whitespace.

filename: test/parallel/test-util-inspect-proxy.js
patch: @@ -116,7 +116,7 @@ const proxy3 = new Proxy(proxy2, proxy1);
 const proxy4 = new Proxy(proxy1, proxy2);
 const proxy5 = new Proxy(proxy3, proxy4);
 const proxy6 = new Proxy(proxy5, proxy5);
-const expected0 = '{}';
+const expected0 = 'Proxy({})';
 const expected1 = 'Proxy [ {}, {} ]';
 const expected2 = 'Proxy [ Proxy [ {}, {} ], {} ]';
 const expected3 = 'Proxy [ Proxy [ Proxy [ {}, {} ], {} ], Proxy [ {}, {} ] ]';
@@ -154,7 +154,7 @@ assert.strictEqual(util.inspect(proxy6), expected0);
 const proxy7 = new Proxy([], []);
 const expected7 = 'Proxy [ [], [] ]';
 assert.strictEqual(util.inspect(proxy7, opts), expected7);
-assert.strictEqual(util.inspect(proxy7), '[]');
+assert.strictEqual(util.inspect(proxy7), 'Proxy([])');
 
 // Now we're just getting silly, right?
 const proxy8 = new Proxy(Date, []);
@@ -163,8 +163,8 @@ const expected8 = 'Proxy [ [Function: Date], [] ]';
 const expected9 = 'Proxy [ [Function: Date], [Function: String] ]';
 assert.strictEqual(util.inspect(proxy8, opts), expected8);
 assert.strictEqual(util.inspect(proxy9, opts), expected9);
-assert.strictEqual(util.inspect(proxy8), '[Function: Date]');
-assert.strictEqual(util.inspect(proxy9), '[Function: Date]');
+assert.strictEqual(util.inspect(proxy8), 'Proxy([Function: Date])');
+assert.strictEqual(util.inspect(proxy9), 'Proxy([Function: Date])');
 
 const proxy10 = new Proxy(() => {}, {});
 const proxy11 = new Proxy(() => {}, {
@@ -175,7 +175,16 @@ const proxy11 = new Proxy(() => {}, {
     return proxy11;
   }
 });
-const expected10 = '[Function (anonymous)]';
-const expected11 = '[Function (anonymous)]';
+const expected10 = 'Proxy([Function (anonymous)])';
+const expected11 = 'Proxy([Function (anonymous)])';
 assert.strictEqual(util.inspect(proxy10), expected10);
 assert.strictEqual(util.inspect(proxy11), expected11);
+
+const proxy12 = new Proxy([1, 2, 3], proxy5);
+assert.strictEqual(
+  util.inspect(proxy12, { colors: true, breakLength: 1 }),
+  '\x1B[36mProxy(\x1B[39m' +
+    '[\n  \x1B[33m1\x1B[39m,\n  \x1B[33m2\x1B[39m,\n  \x1B[33m3\x1B[39m\n]\x1B[36m' +
+    ')\x1B[39m'
+);
+assert.strictEqual(util.format('%s', proxy12), 'Proxy([ 1, 2, 3 ])');