Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4140f49

Browse filesBrowse 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
Copy full SHA for 4140f49

File tree

Expand file treeCollapse file tree

2 files changed

+35
-3
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+35
-3
lines changed
Open diff view settings
Collapse file

‎lib/internal/util/inspect.js‎

Copy file name to clipboardExpand all lines: lib/internal/util/inspect.js
+5-3Lines 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
const value = 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
return output;
16781679
}
16791680

1680-
function formatProperty(ctx, value, recurseTimes, key, type, desc) {
1681+
function formatProperty(ctx, value, recurseTimes, key, type, desc,
1682+
original = value) {
16811683
let name, str;
16821684
let extra = ' ';
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-
const tmp = value[key];
1703+
const tmp = ReflectApply(desc.get, original, []);
17021704
ctx.indentationLvl += 2;
17031705
if (tmp === null) {
17041706
str = `${s(`[${label}:`, sp)} ${s('null', 'null')}${s(']', sp)}`;
Collapse file
+30Lines 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+
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.