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 <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Gus Caplan <[email protected]>
Reviewed-By: Darshan Sen <[email protected]>
Reviewed-By: Gerhard Stöbich <[email protected]>
  • Loading branch information
addaleax authored and danielleadams committed Mar 16, 2021
1 parent 0563260 commit 8fdc4a5
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 8fdc4a5

Please sign in to comment.