GeezerVison is showing github.com — util: fix to inspect getters that access this · nodejs/node@dfb353b
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/util/inspect.js +5-3Lines changed: 5 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -46,6 +46,7 @@ const { | 46 | 46 | ObjectPrototypePropertyIsEnumerable, | 47 | 47 | ObjectSeal, | 48 | 48 | ObjectSetPrototypeOf, | | 49 | + ReflectApply, | 49 | 50 | RegExp, | 50 | 51 | RegExpPrototypeToString, | 51 | 52 | Set, | @@ -628,7 +629,7 @@ function addPrototypeProperties(ctx, main, obj, recurseTimes, output) { | 628 | 629 | continue; | 629 | 630 | } | 630 | 631 | const value = formatProperty( | 631 | | - ctx, obj, recurseTimes, key, kObjectType, desc); | | 632 | + ctx, obj, recurseTimes, key, kObjectType, desc, main); | 632 | 633 | if (ctx.colors) { | 633 | 634 | // Faint! | 634 | 635 | output.push(`\u001b[2m${value}\u001b[22m`); | @@ -1678,7 +1679,8 @@ function formatPromise(ctx, value, recurseTimes) { | 1678 | 1679 | return output; | 1679 | 1680 | } | 1680 | 1681 |
| 1681 | | -function formatProperty(ctx, value, recurseTimes, key, type, desc) { | | 1682 | +function formatProperty(ctx, value, recurseTimes, key, type, desc, | | 1683 | + original = value) { | 1682 | 1684 | let name, str; | 1683 | 1685 | let extra = ' '; | 1684 | 1686 | desc = desc || ObjectGetOwnPropertyDescriptor(value, key) || | @@ -1699,7 +1701,7 @@ function formatProperty(ctx, value, recurseTimes, key, type, desc) { | 1699 | 1701 | (ctx.getters === 'get' && desc.set === undefined) || | 1700 | 1702 | (ctx.getters === 'set' && desc.set !== undefined))) { | 1701 | 1703 | try { | 1702 | | - const tmp = value[key]; | | 1704 | + const tmp = ReflectApply(desc.get, original, []); | 1703 | 1705 | ctx.indentationLvl += 2; | 1704 | 1706 | if (tmp === null) { | 1705 | 1707 | str = `${s(`[${label}:`, sp)} ${s('null', 'null')}${s(']', sp)}`; | |
Collapse file Copy file name to clipboard +30Lines changed: 30 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | +); |
|