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 <anna@addaleax.net> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: LiviaMedeiros <livia@cirno.name> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent e5b6f89 commit dc97b50

4 files changed

Lines changed: 24 additions & 9 deletions

File tree

‎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
returnctx.stylize(`[Circular *${index}]`,'special');
11811181
}
11821182

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

11861192
functionformatRaw(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
constproxy4=newProxy(proxy1,proxy2);
117117
constproxy5=newProxy(proxy3,proxy4);
118118
constproxy6=newProxy(proxy5,proxy5);
119-
constexpected0='{}';
119+
constexpected0='Proxy({})';
120120
constexpected1='Proxy [ {}, {} ]';
121121
constexpected2='Proxy [ Proxy [ {}, {} ], {} ]';
122122
constexpected3='Proxy [ Proxy [ Proxy [ {}, {} ], {} ], Proxy [ {}, {} ] ]';
@@ -154,7 +154,7 @@ assert.strictEqual(util.inspect(proxy6), expected0);
154154
constproxy7=newProxy([],[]);
155155
constexpected7='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
constproxy8=newProxy(Date,[]);
@@ -163,8 +163,8 @@ const expected8 = 'Proxy [ [Function: Date], [] ]';
163163
constexpected9='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
constproxy10=newProxy(()=>{},{});
170170
constproxy11=newProxy(()=>{},{
@@ -175,7 +175,16 @@ const proxy11 = new Proxy(() => {}, {
175175
returnproxy11;
176176
}
177177
});
178-
constexpected10='[Function (anonymous)]';
179-
constexpected11='[Function (anonymous)]';
178+
constexpected10='Proxy([Function (anonymous)])';
179+
constexpected11='Proxy([Function (anonymous)])';
180180
assert.strictEqual(util.inspect(proxy10),expected10);
181181
assert.strictEqual(util.inspect(proxy11),expected11);
182+
183+
constproxy12=newProxy([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)