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

Object rest and function scope #3749

Open
magic-akari opened this issue May 3, 2024 · 4 comments
Open

Object rest and function scope #3749

magic-akari opened this issue May 3, 2024 · 4 comments

Comments

@magic-akari
Copy link
Contributor

Playground

https://esbuild.github.io/try/#dAAwLjIwLjIALS10YXJnZXQ9Y2hyb21lNTkAZnVuY3Rpb24gYSgpIHsKICBjb25zb2xlLmxvZygiYSIpOwogIHJldHVybiAiYSI7Cn0KCmZ1bmN0aW9uIGIoKSB7CiAgY29uc29sZS5sb2coImIiKTsKICByZXR1cm4gImIiOwp9CgpmdW5jdGlvbiBjKCkgewogIGNvbnNvbGUubG9nKCJjIik7CiAgcmV0dXJuICJjIjsKfQoKZnVuY3Rpb24gZm9vKHsgYWEsIC4uLmJiIH0sIGNjID0gYWEoKSkgewogIGNvbnNvbGUubG9nKGFhLCBiYiwgY2MpCn0KCgoKZm9vKHsgYWE6IGEsIGJiOiBiLCBjYzogYyB9KQ

Input

function a() {
  console.log("a");
  return "a";
}

function b() {
  console.log("b");
  return "b";
}

function c() {
  console.log("c");
  return "c";
}

function foo({ aa, ...bb }, cc = aa()) {
  console.log(aa, bb, cc)
}



foo({ aa: a, bb: b, cc: c })

with --target=chrome59

Expected Result

print some function and objects.

Actual Result

Uncaught ReferenceError: aa is not defined

Relates issue

babel/babel#16470

@magic-akari magic-akari changed the title Object rest, side effect order and function scope Object rest and function scope May 3, 2024
@evanw
Copy link
Owner

evanw commented May 3, 2024

This comment is relevant:

// Lower each argument individually instead of lowering all arguments
// together. There is a correctness tradeoff here around default values
// for function arguments, with no right answer.
//
// Lowering all arguments together will preserve the order of side effects
// for default values, but will mess up their scope:
//
// // Side effect order: a(), b(), c()
// function foo([{[a()]: w, ...x}, y = b()], z = c()) {}
//
// // Side effect order is correct but scope is wrong
// function foo(_a, _b) {
// var [[{[a()]: w, ...x}, y = b()], z = c()] = [_a, _b]
// }
//
// Lowering each argument individually will preserve the scope for default
// values that don't contain object rest binding patterns, but will mess up
// the side effect order:
//
// // Side effect order: a(), b(), c()
// function foo([{[a()]: w, ...x}, y = b()], z = c()) {}
//
// // Side effect order is wrong but scope for c() is correct
// function foo(_a, z = c()) {
// var [{[a()]: w, ...x}, y = b()] = _a
// }
//
// This transform chooses to lower each argument individually with the
// thinking that perhaps scope matters more in real-world code than side
// effect order.

@magic-akari
Copy link
Contributor Author

esbuild considers scope to be more important than side effect order.
However, in this example, the reference to aa is still incorrect.

@magic-akari
Copy link
Contributor Author

Thank you for the clear comments in esbuild.
This issue was actually discovered when I was reading the esbuild source code.
However, I feel that it might not be worth solving, or it might be unsolvable. I'm just bringing it up here for the record.

@evanw
Copy link
Owner

evanw commented May 3, 2024

Yes, makes sense. Thanks for the report. When I get the time to think about this, I'll have to load this back in my head to figure out what to do about it, if anything. That comment (and any relevant surrounding commit logs from then) will be a good starting point for me.

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

2 participants