Skip to content

util.inspect/format doesn't parse ES6+ function parameters correctly. #7338

@rezen

Description

@rezen
  • v6.0.0
  • Win7x64
  • util

I talked with @jasnell about this on Monday, so I'm tagging him on this :)

When using util.inspect for functions, the output length value seems to indicates the number of parameters in the inspected function.
Where things break is with functions that have default values. Below is a straight-forward proof
of concept that demonstrates the incorrect behavior. I assume with is related to the addition of the = sign
and commas which may or may not be param separators.

Code

'use strict';

const util = require('util');

function cats(a, b, c = [1, 2, 3], d, e, f) {}
function dogs(a, b, c = {d: '1', e: 2}, f, g) {}
function mice(a = '1,2,3', b='cheese') {}
function unicorns(a, b, c) {}

console.log(util.inspect(cats, { showHidden: true, depth: null }));
console.log(util.inspect(dogs, { showHidd
n: true, depth: null }));
console.log(util.inspect(mice, { showHidden: true, depth: null }));
console.log(util.inspect(unicorns, { showHidden: true, depth: null }));```

Output

{ [Function: cats]
  [length]: 2,
  [name]: 'cats',
  [prototype]: cats { [constructor]: [Circular] } }
{ [Function: dogs]
  [length]: 2,
  [name]: 'dogs',
  [prototype]: dogs { [constructor]: [Circular] } }
{ [Function: mice]
  [length]: 0,
  [name]: 'mice',
  [prototype]: mice { [constructor]: [Circular] } }
{ [Function: unicorns]
  [length]: 3,
  [name]: 'unicorns',
  [prototype]: unicorns { [constructor]: [Circular] } }

Metadata

Metadata

Assignees

No one assigned

    Labels

    invalidIssues and PRs that are invalid.v8 engineIssues and PRs related to the V8 dependency.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions