bun icon indicating copy to clipboard operation
bun copied to clipboard

`toString()` a function omits function name, makes lighthouse fail

Open hilja opened this issue 1 year ago • 0 comments

What version of Bun is running?

1.0.25

What platform is your computer?

Darwin 23.3.0 arm64 arm

What steps can reproduce the bug?

Make a file:

// test.js
function fooBar() {
  return 'bar'
}

function foo(fn) {
  console.log(fn.toString())
}

foo(fooBar)

Run it:

$ node test.js
function fooBar() {
  return 'bar'
}
$ bun test.js
function() {
  return "bar";
}

What is the expected behavior?

String with the function name:

function fooBar() {
  return 'bar'
}

What do you see instead?

Function name omitted:

function() {
  return "bar";
}

Additional information

Lighthouse performance metric package fails here https://github.com/GoogleChrome/lighthouse/blob/5e18c5a0656b427e59890dc9c125164ef9f276c3/core/lib/page-functions.js#L578

They have a util like this:

/**
 * @param {Function} fn
 * @return {string}
 */
function getRuntimeFunctionName(fn) {
  const match = fn.toString().match(/function ([\w$]+)/);
  if (!match) throw new Error(`could not find function name for: ${fn}`);
  return match[1];
}

Bun can't find the match and errors out:

576 |  */
577 | function getRuntimeFunctionName(fn) {
578 |   const match = fn.toString().match(/function ([\w$]+)/);
579 |   if (!match) throw new Error(`could not find function name for: ${fn}`);
                          ^
error: could not find function name for: function(string, characterLimit) {
  return Util.truncate(string, characterLimit);
}
      at getRuntimeFunctionName (/Users/bob/web/foo/node_modules/.pnpm/[email protected]/node_modules/lighthouse/core/lib/page-functions.js:581:21)
      at /Users/bob/web/foo/node_modules/.pnpm/[email protected]/node_modules/lighthouse/core/lib/page-functions.js:589:13

hilja avatar Feb 01 '24 16:02 hilja