From 8fdc4a5cdfd2cb98b32b0fc6f040a8882dd506d6 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 11 Mar 2021 14:20:44 +0100 Subject: [PATCH] util: inspect __proto__ key as written in object literal MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since util.inspect() gives object-literal-like output, handle the special `__proto__` key in the way that it would be handled in object literals. PR-URL: https://github.com/nodejs/node/pull/37713 Reviewed-By: Ruben Bridgewater Reviewed-By: Benjamin Gruenbaum Reviewed-By: James M Snell Reviewed-By: Colin Ihrig Reviewed-By: Gus Caplan Reviewed-By: Darshan Sen Reviewed-By: Gerhard Stöbich --- lib/internal/util/inspect.js | 2 ++ test/parallel/test-util-inspect.js | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index f658103b954fbb..fed88e963a178c 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -1718,6 +1718,8 @@ function formatProperty(ctx, value, recurseTimes, key, type, desc, strEscapeSequencesReplacer, escapeFn ); name = `[${ctx.stylize(tmp, 'symbol')}]`; + } else if (key === '__proto__') { + name = "['__proto__']"; } else if (desc.enumerable === false) { const tmp = StringPrototypeReplace(key, strEscapeSequencesReplacer, escapeFn); diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index 8314b64c5ac345..f70090f48939d7 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -3095,3 +3095,10 @@ assert.strictEqual( ']' ); } + +{ + assert.strictEqual( + util.inspect({ ['__proto__']: { a: 1 } }), + "{ ['__proto__']: { a: 1 } }" + ); +}