diff options
Diffstat (limited to 'cli')
-rw-r--r-- | cli/dts/lib.deno.ns.d.ts | 2 | ||||
-rw-r--r-- | cli/rt/02_console.js | 19 | ||||
-rw-r--r-- | cli/tests/unit/console_test.ts | 47 |
3 files changed, 67 insertions, 1 deletions
diff --git a/cli/dts/lib.deno.ns.d.ts b/cli/dts/lib.deno.ns.d.ts index 15b390fd1..141e66a24 100644 --- a/cli/dts/lib.deno.ns.d.ts +++ b/cli/dts/lib.deno.ns.d.ts @@ -1952,6 +1952,8 @@ declare namespace Deno { compact?: boolean; /** The maximum number of iterable entries to print. Defaults to 100. */ iterableLimit?: number; + /** Show a Proxy's target and handler. Defaults to false. */ + showProxy?: boolean; } /** Converts the input into a string that has the same format as printed by diff --git a/cli/rt/02_console.js b/cli/rt/02_console.js index e3492772d..00ecd3aac 100644 --- a/cli/rt/02_console.js +++ b/cli/rt/02_console.js @@ -154,6 +154,7 @@ trailingComma: false, compact: true, iterableLimit: 100, + showProxy: false, }; const DEFAULT_INDENT = " "; // Default indent string @@ -400,6 +401,13 @@ level, inspectOptions, ) { + const proxyDetails = Deno.core.getProxyDetails(value); + if (proxyDetails != null) { + return inspectOptions.showProxy + ? inspectProxy(proxyDetails, ctx, level, inspectOptions) + : inspectValue(proxyDetails[0], ctx, level, inspectOptions); + } + switch (typeof value) { case "string": return value; @@ -657,7 +665,16 @@ return `Promise { ${str} }`; } - // TODO: Proxy + function inspectProxy( + targetAndHandler, + ctx, + level, + inspectOptions, + ) { + return `Proxy ${ + inspectArray(targetAndHandler, ctx, level, inspectOptions) + }`; + } function inspectRawObject( value, diff --git a/cli/tests/unit/console_test.ts b/cli/tests/unit/console_test.ts index 3d1ace6bd..384b5d2c8 100644 --- a/cli/tests/unit/console_test.ts +++ b/cli/tests/unit/console_test.ts @@ -1367,3 +1367,50 @@ unitTest(function inspectIterableLimit(): void { `Map { "a" => 1, "b" => 2, ... 1 more items }`, ); }); + +unitTest(function inspectProxy(): void { + assertEquals( + Deno.inspect( + new Proxy([1, 2, 3], { get(): void {} }), + ), + "[ 1, 2, 3 ]", + ); + assertEquals( + Deno.inspect( + new Proxy({ key: "value" }, { get(): void {} }), + ), + `{ key: "value" }`, + ); + assertEquals( + Deno.inspect(new Proxy([1, 2, 3], { get(): void {} }), { showProxy: true }), + "Proxy [ [ 1, 2, 3 ], { get: [Function: get] } ]", + ); + assertEquals( + Deno.inspect( + new Proxy({ a: 1 }, { + set(): boolean { + return false; + }, + }), + { showProxy: true }, + ), + "Proxy [ { a: 1 }, { set: [Function: set] } ]", + ); + assertEquals( + Deno.inspect( + new Proxy([1, 2, 3, 4, 5, 6, 7], { get(): void {} }), + { showProxy: true }, + ), + `Proxy [ [ + 1, 2, 3, 4, + 5, 6, 7 + ], { get: [Function: get] } ]`, + ); + assertEquals( + Deno.inspect( + new Proxy(function fn() {}, { get(): void {} }), + { showProxy: true }, + ), + "Proxy [ [Function: fn], { get: [Function: get] } ]", + ); +}); |