Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: fix to inspect getters that access this #36052

Merged
merged 1 commit into from Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -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
@@ -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] }'
);