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

Code rewriting breaks function name inferencing #1543

Open
getify opened this issue Jan 4, 2024 · 1 comment
Open

Code rewriting breaks function name inferencing #1543

getify opened this issue Jan 4, 2024 · 1 comment

Comments

@getify
Copy link

getify commented Jan 4, 2024

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

  • [ X ] still occurring when I put cache: false in my nyc config

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
Copy link
Author

getify commented Jan 4, 2024

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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant