Skip to content

Commit

Permalink
util: fix to inspect getters that access this
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
RaisinTen authored and BethGriggs committed Dec 15, 2020
1 parent b7441ea commit dfb353b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/internal/util/inspect.js
Expand Up @@ -46,6 +46,7 @@ const {
ObjectPrototypePropertyIsEnumerable,
ObjectSeal,
ObjectSetPrototypeOf,
ReflectApply,
RegExp,
RegExpPrototypeToString,
Set,
Expand Down Expand Up @@ -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`);
Expand Down Expand Up @@ -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) ||
Expand All @@ -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)}`;
Expand Down
30 changes: 30 additions & 0 deletions test/parallel/test-util-inspect-getters-accessing-this.js
@@ -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] }'
);

0 comments on commit dfb353b

Please sign in to comment.