From 90a43906ab5ca8bdf154fb7644d70da9e3acdf00 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 25 Oct 2017 12:06:40 +0200 Subject: [PATCH] repl: show proxies as Proxy objects Before this commit they transparently invoked their magic methods but that sometimes throws confusing exceptions with misbehaving proxies. This change is not wholly uncontroversial but we can always change the default if necessary. Let's see how it goes. Fixes: https://github.com/nodejs/node/issues/16483 PR-URL: https://github.com/nodejs/node/pull/16485 Reviewed-By: Colin Ihrig Reviewed-By: Franziska Hinkelmann Reviewed-By: James M Snell --- lib/repl.js | 11 ++++++----- test/parallel/test-repl.js | 11 +++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index 16e129641a1a5f..79acd217a4619a 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -100,7 +100,9 @@ function hasOwnProperty(obj, prop) { // Can overridden with custom print functions, such as `probe` or `eyes.js`. // This is the default "writer" value if none is passed in the REPL options. -exports.writer = util.inspect; +const writer = exports.writer = (obj) => util.inspect(obj, writer.options); +writer.options = + Object.assign(util.inspect.defaultOptions, { showProxy: true }); exports._builtinLibs = internalModule.builtinLibs; @@ -373,11 +375,10 @@ function REPLServer(prompt, } self.useColors = !!options.useColors; - if (self.useColors && self.writer === util.inspect) { + if (self.useColors && self.writer === writer) { // Turn on ANSI coloring. - self.writer = function(obj, showHidden, depth) { - return util.inspect(obj, showHidden, depth, true); - }; + self.writer = (obj) => util.inspect(obj, self.writer.options); + self.writer.options = Object.assign(writer.options, { colors: true }); } function filterInternalStackFrames(error, structuredStack) { diff --git a/test/parallel/test-repl.js b/test/parallel/test-repl.js index 6d4f0d8c7a5862..a771b3243169ac 100644 --- a/test/parallel/test-repl.js +++ b/test/parallel/test-repl.js @@ -413,6 +413,17 @@ function error_test() { expect: `${prompt_multiline}${prompt_multiline}undefined\n${prompt_unix}` }, + // https://github.com/nodejs/node/issues/16483 + { + client: client_unix, send: 'new Proxy({x:42}, {get(){throw null}});', + expect: `Proxy [ { x: 42 }, { get: [Function: get] } ]\n${prompt_unix}` + }, + { + client: client_unix, + send: 'repl.writer.options.showProxy = false, new Proxy({x:42}, {});', + expect: `{ x: 42 }\n${prompt_unix}` + }, + // Newline within template string maintains whitespace. { client: client_unix, send: '`foo \n`', expect: `${prompt_multiline}'foo \\n'\n${prompt_unix}` },