Skip to content

Commit b5c5bd1

Browse files
addaleaxtargos
authored andcommitted
util: inspect __proto__ key as written in object literal
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]>
1 parent 9fb10dc commit b5c5bd1

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

lib/internal/util/inspect.js

+2
Original file line numberDiff line numberDiff line change
@@ -1739,6 +1739,8 @@ function formatProperty(ctx, value, recurseTimes, key, type, desc,
17391739
if (typeof key === 'symbol') {
17401740
const tmp = key.toString().replace(strEscapeSequencesReplacer, escapeFn);
17411741
name = `[${ctx.stylize(tmp, 'symbol')}]`;
1742+
} else if (key === '__proto__') {
1743+
name = "['__proto__']";
17421744
} else if (desc.enumerable === false) {
17431745
name = `[${key.replace(strEscapeSequencesReplacer, escapeFn)}]`;
17441746
} else if (keyStrRegExp.test(key)) {

test/parallel/test-util-inspect.js

+7
Original file line numberDiff line numberDiff line change
@@ -3094,3 +3094,10 @@ assert.strictEqual(
30943094
']'
30953095
);
30963096
}
3097+
3098+
{
3099+
assert.strictEqual(
3100+
util.inspect({ ['__proto__']: { a: 1 } }),
3101+
"{ ['__proto__']: { a: 1 } }"
3102+
);
3103+
}

0 commit comments

Comments
 (0)