Skip to content

Commit

Permalink
util: inspect __proto__ key as written in object literal
Browse files Browse the repository at this point in the history
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: #37713
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
  • Loading branch information
addaleax authored and targos committed May 1, 2021
1 parent 9fb10dc commit b5c5bd1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1739,6 +1739,8 @@ function formatProperty(ctx, value, recurseTimes, key, type, desc,
if (typeof key === 'symbol') {
const tmp = key.toString().replace(strEscapeSequencesReplacer, escapeFn);
name = `[${ctx.stylize(tmp, 'symbol')}]`;
} else if (key === '__proto__') {
name = "['__proto__']";
} else if (desc.enumerable === false) {
name = `[${key.replace(strEscapeSequencesReplacer, escapeFn)}]`;
} else if (keyStrRegExp.test(key)) {
Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -3094,3 +3094,10 @@ assert.strictEqual(
']'
);
}

{
assert.strictEqual(
util.inspect({ ['__proto__']: { a: 1 } }),
"{ ['__proto__']: { a: 1 } }"
);
}

0 comments on commit b5c5bd1

Please sign in to comment.