Skip to content

Commit cfadbc2

Browse files
targositaloacasas
authored andcommitted
util: improve inspect for AsyncFunction
Use the constructor name in the output, if present. This is a backport of #11210 without the semver-major change to GeneratorFunction output. PR-URL: #11211 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: Yuta Hiroto <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Timothy Gu <[email protected]>
1 parent a2948fb commit cfadbc2

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

lib/util.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -400,11 +400,15 @@ function formatValue(ctx, value, recurseTimes) {
400400
});
401401
}
402402

403+
var constructor = getConstructorOf(value);
404+
403405
// Some type of object without properties can be shortcutted.
404406
if (keys.length === 0) {
405407
if (typeof value === 'function') {
406-
return ctx.stylize(`[Function${value.name ? `: ${value.name}` : ''}]`,
407-
'special');
408+
const ctorName = (constructor && constructor.name === 'AsyncFunction') ?
409+
'AsyncFunction' : 'Function';
410+
return ctx.stylize(
411+
`[${ctorName}${value.name ? `: ${value.name}` : ''}]`, 'special');
408412
}
409413
if (isRegExp(value)) {
410414
return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
@@ -440,12 +444,11 @@ function formatValue(ctx, value, recurseTimes) {
440444
// Can't do the same for DataView because it has a non-primitive
441445
// .buffer property that we need to recurse for.
442446
if (binding.isArrayBuffer(value) || binding.isSharedArrayBuffer(value)) {
443-
return `${getConstructorOf(value).name}` +
447+
return `${constructor.name}` +
444448
` { byteLength: ${formatNumber(ctx, value.byteLength)} }`;
445449
}
446450
}
447451

448-
var constructor = getConstructorOf(value);
449452
var base = '', empty = false, braces;
450453
var formatter = formatObject;
451454

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

537540
// Make functions say that they are functions
538541
if (typeof value === 'function') {
539-
base = ` [Function${value.name ? `: ${value.name}` : ''}]`;
542+
const ctorName = (constructor && constructor.name === 'AsyncFunction') ?
543+
'AsyncFunction' : 'Function';
544+
base = ` [${ctorName}${value.name ? `: ${value.name}` : ''}]`;
540545
}
541546

542547
// Make RegExps say that they are RegExps

test/parallel/test-util-inspect.js

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ assert.strictEqual(util.inspect(false), 'false');
99
assert.strictEqual(util.inspect(''), "''");
1010
assert.strictEqual(util.inspect('hello'), "'hello'");
1111
assert.strictEqual(util.inspect(function() {}), '[Function]');
12+
assert.strictEqual(util.inspect(async function() {}), '[AsyncFunction]');
13+
assert.strictEqual(util.inspect(function*() {}), '[Function]');
1214
assert.strictEqual(util.inspect(undefined), 'undefined');
1315
assert.strictEqual(util.inspect(null), 'null');
1416
assert.strictEqual(util.inspect(/foo(bar\n)?/gi), '/foo(bar\\n)?/gi');
@@ -28,6 +30,10 @@ assert.strictEqual(util.inspect([1, [2, 3]]), '[ 1, [ 2, 3 ] ]');
2830
assert.strictEqual(util.inspect({}), '{}');
2931
assert.strictEqual(util.inspect({a: 1}), '{ a: 1 }');
3032
assert.strictEqual(util.inspect({a: function() {}}), '{ a: [Function: a] }');
33+
assert.strictEqual(util.inspect({a: async function() {}}),
34+
'{ a: [AsyncFunction: a] }');
35+
assert.strictEqual(util.inspect({a: function*() {}}),
36+
'{ a: [Function: a] }');
3137
assert.strictEqual(util.inspect({a: 1, b: 2}), '{ a: 1, b: 2 }');
3238
assert.strictEqual(util.inspect({'a': {}}), '{ a: {} }');
3339
assert.strictEqual(util.inspect({'a': {'b': 2}}), '{ a: { b: 2 } }');

0 commit comments

Comments
 (0)