Skip to content

Commit

Permalink
util: improve inspect for AsyncFunction
Browse files Browse the repository at this point in the history
Use the constructor name in the output, if present.
This is a backport of nodejs#11210 without
the semver-major change to GeneratorFunction output.

PR-URL: nodejs#11211
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
  • Loading branch information
targos authored and italoacasas committed Feb 14, 2017
1 parent a2948fb commit cfadbc2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,11 +400,15 @@ function formatValue(ctx, value, recurseTimes) {
});
}

var constructor = getConstructorOf(value);

// Some type of object without properties can be shortcutted.
if (keys.length === 0) {
if (typeof value === 'function') {
return ctx.stylize(`[Function${value.name ? `: ${value.name}` : ''}]`,
'special');
const ctorName = (constructor && constructor.name === 'AsyncFunction') ?
'AsyncFunction' : 'Function';
return ctx.stylize(
`[${ctorName}${value.name ? `: ${value.name}` : ''}]`, 'special');
}
if (isRegExp(value)) {
return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
Expand Down Expand Up @@ -440,12 +444,11 @@ function formatValue(ctx, value, recurseTimes) {
// Can't do the same for DataView because it has a non-primitive
// .buffer property that we need to recurse for.
if (binding.isArrayBuffer(value) || binding.isSharedArrayBuffer(value)) {
return `${getConstructorOf(value).name}` +
return `${constructor.name}` +
` { byteLength: ${formatNumber(ctx, value.byteLength)} }`;
}
}

var constructor = getConstructorOf(value);
var base = '', empty = false, braces;
var formatter = formatObject;

Expand Down Expand Up @@ -536,7 +539,9 @@ function formatValue(ctx, value, recurseTimes) {

// Make functions say that they are functions
if (typeof value === 'function') {
base = ` [Function${value.name ? `: ${value.name}` : ''}]`;
const ctorName = (constructor && constructor.name === 'AsyncFunction') ?
'AsyncFunction' : 'Function';
base = ` [${ctorName}${value.name ? `: ${value.name}` : ''}]`;
}

// Make RegExps say that they are RegExps
Expand Down
6 changes: 6 additions & 0 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ assert.strictEqual(util.inspect(false), 'false');
assert.strictEqual(util.inspect(''), "''");
assert.strictEqual(util.inspect('hello'), "'hello'");
assert.strictEqual(util.inspect(function() {}), '[Function]');
assert.strictEqual(util.inspect(async function() {}), '[AsyncFunction]');
assert.strictEqual(util.inspect(function*() {}), '[Function]');
assert.strictEqual(util.inspect(undefined), 'undefined');
assert.strictEqual(util.inspect(null), 'null');
assert.strictEqual(util.inspect(/foo(bar\n)?/gi), '/foo(bar\\n)?/gi');
Expand All @@ -28,6 +30,10 @@ assert.strictEqual(util.inspect([1, [2, 3]]), '[ 1, [ 2, 3 ] ]');
assert.strictEqual(util.inspect({}), '{}');
assert.strictEqual(util.inspect({a: 1}), '{ a: 1 }');
assert.strictEqual(util.inspect({a: function() {}}), '{ a: [Function: a] }');
assert.strictEqual(util.inspect({a: async function() {}}),
'{ a: [AsyncFunction: a] }');
assert.strictEqual(util.inspect({a: function*() {}}),
'{ a: [Function: a] }');
assert.strictEqual(util.inspect({a: 1, b: 2}), '{ a: 1, b: 2 }');
assert.strictEqual(util.inspect({'a': {}}), '{ a: {} }');
assert.strictEqual(util.inspect({'a': {'b': 2}}), '{ a: { b: 2 } }');
Expand Down

0 comments on commit cfadbc2

Please sign in to comment.