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

using a variable in a default parameter expression that is shadowed in that function's body causes a reference error #1509

Closed
aweary opened this issue Mar 25, 2024 · 2 comments

Comments

@aweary
Copy link

aweary commented Mar 25, 2024

Bug report or Feature request?

Bug report

If you have a default function parameter that refers to a variable, and then you shadow that same variable inside the same function, then Terser confuses the outer variable with the inner one and ends up pointing the default parameter to the inner variable which is not actually in scope yet.

To make that clearer: in the example below database is defined outside of query; the query function has a db parameter which defaults to that outer database variable. Then query defines its own database variable that it accesses the value through.

terser input

var database = { open() { /* ... */ } }

function query(db = database) {
  var database = db;
  database.open();
}

terser output or error
You can see that query(a=e) means its defaulting to the e variable within the query function

var database={open(){}};function query(a=e){var e=a;e.open()}

Expected result
but thats incorrect, it should be defaulting to the outer database value.

var database={open(){}};function query(a=database){var e=a;e.open()}
@dasa
Copy link

dasa commented Mar 25, 2024

This looks similar to #1478

@fabiosantoscode
Copy link
Collaborator

Yes, it's duplicated. The next quarter I'll be working on this

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

3 participants