Skip to content

Commit 3517ced

Browse files
RaisinTenBethGriggs
authored andcommitted
util: fix to inspect getters that access this
Fixes: #36045 Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com> PR-URL: #36052 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 61daa72 commit 3517ced

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

lib/internal/util/inspect.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const {
4646
ObjectPrototypePropertyIsEnumerable,
4747
ObjectSeal,
4848
ObjectSetPrototypeOf,
49+
ReflectApply,
4950
RegExp,
5051
RegExpPrototypeToString,
5152
Set,
@@ -628,7 +629,7 @@ function addPrototypeProperties(ctx, main, obj, recurseTimes, output) {
628629
continue;
629630
}
630631
const value = formatProperty(
631-
ctx, obj, recurseTimes, key, kObjectType, desc);
632+
ctx, obj, recurseTimes, key, kObjectType, desc, main);
632633
if (ctx.colors) {
633634
// Faint!
634635
output.push(`\u001b[2m${value}\u001b[22m`);
@@ -1678,7 +1679,8 @@ function formatPromise(ctx, value, recurseTimes) {
16781679
return output;
16791680
}
16801681

1681-
function formatProperty(ctx, value, recurseTimes, key, type, desc) {
1682+
function formatProperty(ctx, value, recurseTimes, key, type, desc,
1683+
original = value) {
16821684
let name, str;
16831685
let extra = ' ';
16841686
desc = desc || ObjectGetOwnPropertyDescriptor(value, key) ||
@@ -1699,7 +1701,7 @@ function formatProperty(ctx, value, recurseTimes, key, type, desc) {
16991701
(ctx.getters === 'get' && desc.set === undefined) ||
17001702
(ctx.getters === 'set' && desc.set !== undefined))) {
17011703
try {
1702-
const tmp = value[key];
1704+
const tmp = ReflectApply(desc.get, original, []);
17031705
ctx.indentationLvl += 2;
17041706
if (tmp === null) {
17051707
str = `${s(`[${label}:`, sp)} ${s('null', 'null')}${s(']', sp)}`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
require('../common');
4+
5+
// This test ensures that util.inspect logs getters
6+
// which access this.
7+
8+
const assert = require('assert');
9+
10+
const util = require('util');
11+
12+
class X {
13+
constructor() {
14+
this._y = 123;
15+
}
16+
17+
get y() {
18+
return this._y;
19+
}
20+
}
21+
22+
const result = util.inspect(new X(), {
23+
getters: true,
24+
showHidden: true
25+
});
26+
27+
assert.strictEqual(
28+
result,
29+
'X { _y: 123, [y]: [Getter: 123] }'
30+
);

0 commit comments

Comments
 (0)