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 danielleadams committed Mar 16, 2021
1 parent 1022d3d commit 80af610
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 @@ -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);
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 @@ -3095,3 +3095,10 @@ assert.strictEqual(
']'
);
}

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

0 comments on commit 80af610

Please sign in to comment.