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 codebytere committed Nov 22, 2020
1 parent 63494e4 commit 4140f49
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
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const {
ObjectPrototypePropertyIsEnumerable,
ObjectSeal,
ObjectSetPrototypeOf,
ReflectApply,
RegExp,
RegExpPrototypeToString,
Set,
Expand Down Expand Up @@ -627,7 +628,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 @@ -1677,7 +1678,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 @@ -1698,7 +1700,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
Original file line number Diff line number Diff line change
@@ -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 4140f49

Please sign in to comment.