istanbuljs / nyc

the Istanbul command line interface
https://istanbul.js.org/
ISC License
5.54k stars 353 forks source link

Code rewriting breaks function name inferencing #1543

Open getify opened 6 months ago

getify commented 6 months ago

Link to bug demonstration repository

nyc-bug-demo

Expected Behavior

I expect that the code-rewriting that NYC does will not interfere with expected language behavior, specifically the name inference that occurs when an anonymous arrow function is assigned via a "default parameter value" in a function definition:

function foo(fn = () => 1) {
   fn.name;   // "fn"  <--- inferred name
}

Observed Behavior

If you clone the linked bug demo repo, and run the tests normally (node test.js), it works fine, because the name inference works. But then when you run the same tests via NYC, the tests fail.

NYC rewrites the code in a way that loses this "fn" name inference, such that the value is "" instead (because it's an anonymous function). My test relies on that proper name inference, as it's a standard part of the JS language behavior.

Troubleshooting steps

Environment Information

System:
    OS: Linux 5.15 Debian GNU/Linux 11 (bullseye) 11 (bullseye)
    CPU: (12) x64 12th Gen Intel(R) Core(TM) i7-1255U
    Memory: 1.48 GB / 1.86 GB
  Binaries:
    Node: 19.9.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 9.7.2 - /usr/local/bin/npm
  npmPackages:
    nyc: ~15.1.0 => 15.1.0
getify commented 6 months ago

NOTE: my only work-around for now is I've had to use a full named function in my parameter default:

function foo(fn = function fn() { return 1; }) {
   fn.name;   // "fn" always even with NYC code rewriting
}