-
Notifications
You must be signed in to change notification settings - Fork 2k
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
[CS2] Destructured parameter default evaluation order is broken #4406
Comments
Compiling destructured parameters and their defaults to ES means losing the ability to use Perhaps another solution would be to detect if a function call is happening within parameters, and if so, do everything the “old way” (define the parameter variables within the function body). |
@GeoffreyBooth I've actually improved that on my branch for #4354 - Current output on my branch: ({ @a, b: { @b, c: { @c } } }) -> (function(arg) {
var a, b, c, ref, ref1;
a = arg.a, (ref = arg.b, b = ref.b, (ref1 = ref.c, c = ref1.c));
this.a = a;
this.b = b;
this.c = c;
}); |
Lets do it after. I might get a chance to look at it then, but I don't want to have multiple PRs ongoing. |
* Add failing test per #4406 * If a parameter is a function call, define it in an expression within the function body * Remove the space between `function` and `*` for generator functions, to follow usual ES idiom * We can collapse `isCall` into `isComplex` * Don’t need existence check here
There's another case not covered by the PR: i = 0
({ a = ++i }, b = ++i) -> It generates: // Generated by CoffeeScript 2.0.0-alpha
var i;
i = 0;
(function(arg, b = ++i) { // 'b' will be set first (b === 1)
var a, ref;
a = (ref = arg.a) != null ? ref : ++i; // 'a' will be set second (a === 2)
}); |
This makes me more inspired to rename |
I guess what it's really trying to capture is 'may have side effects'. |
* Add failing test per #4406 * If a parameter is a function call, define it in an expression within the function body * Remove the space between `function` and `*` for generator functions, to follow usual ES idiom * We can collapse `isCall` into `isComplex` * Don’t need existence check here * Correct destructured parameter default evaluation order with an incrementing variable (or more generally any complicated parameter that isComplex) * Try to pull complex parameters out of the parameter list if their order of execution matters; but don’t pull _all_ complex parameters out of the parameter list, so that we don’t lose parameter default values * Add lots of comments about node special properties * Err on the side of caution in deciding whether a complex parameter is allowable in a function parameter list rather than the function body (there are lots more detections we could add to find additional “safe” parameters) * Follow the ES and CS2 convention of assigning parameter default values only when undefined, not when null or undefined * Along with arrays and empty objects, also let values whose bases are not complex be allowed in the function parameter list (like `obj.prop`) * Better way to check for undefined parameters when declaring them in a function body * Once we’ve put a complex parameter in the function body, all following complex parameters go into the function body; no need to create lots of exceptions of when to choose whether to put a complex param in the body * Rename `isComplex` to `shouldCache` for clarity
In 2.0.0-beta1 @connec’s example compiles to: var i;
i = 0;
(function({a = ++i}, b = ++i) {}); which should be in the correct order. If there are further edge cases to fix, please open a new issue. |
Example:
Expected output:
[ 1, 2 ]
Output on
master
:[ 1, 2 ]
Output on
2
:[ 2, 1 ]
This is caused by the
2
branch compiling defaults for simple parameters into the argument list, but handling complex parameters' defaults in the function body.Output on
master
(simplified)Output on
2
(simplified)I encountered this whilst improving
super
handling in #4354 (coming soon).I guess the way we want to fix this is to compile destructured parameters and their defaults to ES also, but I wanted to create an issue to make sure we don't forget about it!
The text was updated successfully, but these errors were encountered: