Skip to content

Commit 4140f49

Browse files
RaisinTencodebytere
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 63494e4 commit 4140f49

2 files changed

Lines changed: 35 additions & 3 deletions

File tree

‎lib/internal/util/inspect.js‎

Lines changed: 5 additions & 3 deletions
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,
@@ -627,7 +628,7 @@ function addPrototypeProperties(ctx, main, obj, recurseTimes, output) {
627628
continue;
628629
}
629630
constvalue=formatProperty(
630-
ctx,obj,recurseTimes,key,kObjectType,desc);
631+
ctx,obj,recurseTimes,key,kObjectType,desc,main);
631632
if(ctx.colors){
632633
// Faint!
633634
output.push(`\u001b[2m${value}\u001b[22m`);
@@ -1677,7 +1678,8 @@ function formatPromise(ctx, value, recurseTimes) {
16771678
returnoutput;
16781679
}
16791680

1680-
functionformatProperty(ctx,value,recurseTimes,key,type,desc){
1681+
functionformatProperty(ctx,value,recurseTimes,key,type,desc,
1682+
original=value){
16811683
letname,str;
16821684
letextra=' ';
16831685
desc=desc||ObjectGetOwnPropertyDescriptor(value,key)||
@@ -1698,7 +1700,7 @@ function formatProperty(ctx, value, recurseTimes, key, type, desc) {
16981700
(ctx.getters==='get'&&desc.set===undefined)||
16991701
(ctx.getters==='set'&&desc.set!==undefined))){
17001702
try{
1701-
consttmp=value[key];
1703+
consttmp=ReflectApply(desc.get,original,[]);
17021704
ctx.indentationLvl+=2;
17031705
if(tmp===null){
17041706
str=`${s(`[${label}:`,sp)}${s('null','null')}${s(']',sp)}`;
Lines changed: 30 additions & 0 deletions
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+
constassert=require('assert');
9+
10+
constutil=require('util');
11+
12+
classX{
13+
constructor(){
14+
this._y=123;
15+
}
16+
17+
gety(){
18+
returnthis._y;
19+
}
20+
}
21+
22+
constresult=util.inspect(newX(),{
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)