Skip to content

Commit

Permalink
repl: show proxies as Proxy objects
Browse files Browse the repository at this point in the history
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: #16483
PR-URL: #16485
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
bnoordhuis committed Nov 7, 2017
1 parent 6563e56 commit 90a4390
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}` },
Expand Down

0 comments on commit 90a4390

Please sign in to comment.