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: improve inspect for (Async|Generator)Function #11210

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion .eslintrc.yaml
Expand Up @@ -3,7 +3,7 @@ env:
es6: true

parserOptions:
ecmaVersion: 2016
ecmaVersion: 2017

rules:
# Possible Errors
Expand Down
13 changes: 8 additions & 5 deletions lib/util.js
Expand Up @@ -400,11 +400,14 @@ function formatValue(ctx, value, recurseTimes) {
});
}

var constructor = getConstructorOf(value);
Copy link
Member

@bnoordhuis bnoordhuis Feb 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe const it while you're here. (EDIT: nevermind, I see it's being reassigned in some places.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't make it const because the value may be changed later.


// 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 : 'Function';
return ctx.stylize(
`[${ctorName}${value.name ? `: ${value.name}` : ''}]`, 'special');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nested templates can be a little bit hard to read, maybe change it to use if-else? (though this LTGM too with this unchanged, now that there is a backport PR already)

}
if (isRegExp(value)) {
return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
Expand Down Expand Up @@ -440,12 +443,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 +538,8 @@ 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 : '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
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*() {}), '[GeneratorFunction]');
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: [GeneratorFunction: 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