util: fix to inspect getters that access this

Fixes: https://github.com/nodejs/node/issues/36045

Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>

PR-URL: https://github.com/nodejs/node/pull/36052
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
raisintenandAntoine du Hamel authored and Beth Griggs committed 2020-12-15 20:15:18 +00:00
1 parent b7441ea4d2
commit dfb353b882
2 files changed
+35 -3

No files matched your search

+5 -3
View File
@@ -46,6 +46,7 @@ const {
ObjectPrototypePropertyIsEnumerable,
ObjectSeal,
ObjectSetPrototypeOf,
ReflectApply,
RegExp,
RegExpPrototypeToString,
Set,
@@ -628,7 +629,7 @@ function addPrototypeProperties(ctx, main, obj, recurseTimes, output) {
continue;
}
const value = formatProperty(
ctx, obj, recurseTimes, key, kObjectType, desc);
ctx, obj, recurseTimes, key, kObjectType, desc, main);
if (ctx.colors) {
// Faint!
output.push(`\u001b[2m${value}\u001b[22m`);
@@ -1678,7 +1679,8 @@ function formatPromise(ctx, value, recurseTimes) {
return output;
}
function formatProperty(ctx, value, recurseTimes, key, type, desc) {
function formatProperty(ctx, value, recurseTimes, key, type, desc,
original = value) {
let name, str;
let extra = ' ';
desc = desc || ObjectGetOwnPropertyDescriptor(value, key) ||
@@ -1699,7 +1701,7 @@ function formatProperty(ctx, value, recurseTimes, key, type, desc) {
(ctx.getters === 'get' && desc.set === undefined) ||
(ctx.getters === 'set' && desc.set !== undefined))) {
try {
const tmp = value[key];
const tmp = ReflectApply(desc.get, original, []);
ctx.indentationLvl += 2;
if (tmp === null) {
str = `${s(`[${label}:`, sp)} ${s('null', 'null')}${s(']', sp)}`;
@@ -0,0 +1,30 @@
'use strict';
require('../common');
// This test ensures that util.inspect logs getters
// which access this.
const assert = require('assert');
const util = require('util');
class X {
constructor() {
this._y = 123;
}
get y() {
return this._y;
}
}
const result = util.inspect(new X(), {
getters: true,
showHidden: true
});
assert.strictEqual(
result,
'X { _y: 123, [y]: [Getter: 123] }'
);