-
Notifications
You must be signed in to change notification settings - Fork 1.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
disallow collapse_vars constant replacement in for-in statements #1543
Conversation
Have to rename the commit comment from for-of to for-in. |
Just to clarify, this PR is to be applied to |
lib/compress.js
Outdated
@@ -479,7 +479,7 @@ merge(Compressor.prototype, { | |||
if (ref.scope.uses_eval || ref.scope.uses_with) break; | |||
|
|||
// Constant single use vars can be replaced in any scope. | |||
if (var_decl.value.is_constant()) { | |||
if (!(stat instanceof AST_ForIn) && var_decl.value.is_constant()) { | |||
var ctt = new TreeTransformer(function(node) { | |||
if (node === ref) { | |||
var parent = ctt.parent(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean to remove/revert this part like you do in #1537 (comment)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've changed this patch a few times. I'm not happy with it. Might have a potential issue on harmony
with pathological ES6 input. I'll revisit it tomorrow.
Just a quick note to say that when I haven't studied it rigorously yet, but I reckon |
Indeed that's true for constant values. The collapse_vars fix in master is fine for ES5. I just want to fix this one specific esoteric thing in collapse_vars for harmony whether or not reduce_vars is enabled. |
Keeping them separate is good, I agree. I'm just thinking that given that |
Now that you mention it, AST_Destructuring will also require a harmony specific fix for collapse_vars. And I'm pretty sure I know of a new way to break reduce_vars on harmony with array destructuring because of the way the harmony AST is designed:
Sure enough. AST_Array can be regular array or it can be used for destructuring. There might be a flag on the object to differentiate it, but it really ought to be a different class altogether - AST_ArrayDestructuring. Compress transforms are so broken on harmony in general related to let/const scopes that ES6 users would be well advised to disable compress and mangle altogether. A short-term harmony fix could be to only optimize functions that do not have ES6-specific nodes. |
You learn something new every day, they say. It's gonna be fun, they say. 🙄 Jokes aside, I think your suggestions on blanket blocking |
@alexlamsl Maybe we should abandon harmony altogether? There are no active harmony maintainers addressing these key compress and mangle issues related to let/const scope. Uglify transforms were written with ES5 in mind, it's non-trivial to adapt to ES6+. |
I don't understand ES6 enough to work on it, so you won't get much push-back from me for abandoning it per se. The question is to whether keep the branch around just for merges between releases. But #1460 might make even that difficult, if we are making |
At the very least we should disable certain compress options on harmony by default - unused, collapse_vars, reduce_vars, and probably a half dozen others. |
Would you mind putting together the list of known bad cases for I'd put them in say |
I'm not exaggerating when I say nearly every compress transform breaks ES6 code. Just dig through the [ES6] issues. Mangle is also broken related to destructuring. I don't have the time to catalog all the ES6 breakage. Harmony does work without compress and mangle as far as I know - except for complex import and export statements which are not implemented. |
... wait, what is |
Evidently, some people are able to use harmony successfully with a subset of ES6 features - simple classes and methods I imagine. Anything with destructuring is use at own risk. Edit: the harmony ES6 template string support works well too. |
Ah, so the problem is with default options and people aren't aware what options to turn on/off for their own specific use case. It's not a showstopper per se as every option can be disabled regardless of their default values. I guess it's then just an argument of whether it's worth to effort to diverge from In that case I'd vote for status quo, as it's less work for future merges, and the |
True enough.
+1 |
@alexlamsl I was wrong about harmony array destructuring - it also uses AST_Destructuring on the LHS:
|
This PR is intended for See #1537 (comment) for how to fix |
Probably unrelated to this PR:
Since #1459 would happen before |
That's correct - as designed. Let's see if anyone complains. If they do, you can add a check for the |
I don't see the toplevel var I have no issue with you having We could remove |
I thought for-loops don't form scopes? $ node
> b
ReferenceError: 'b' is undefined
at Global code (repl:1:1)
...
> for (var a in {}) {var b = 2}
undefined
> b
undefined
Nah - I think your "let's see if anyone complains" is probably fine for this case. I think if we were to change anything in I ran into this just because I was testing some trivial cases for this PR. |
They shouldn't - for Hmm. Odd. It might have something to do with this: At least the output is not incorrect. We can revisit later if we decide to have |
@alexlamsl I dislike unknown behaviors, so I named all the anonymous functions in
I used The |
@kzc thanks for the detailed investigation - something to bear in mind when we revisit this later 😉 I did follow |
Indeed. Microsoft had to fake out a V8 C++ API shim in order to get their Chakra port of Node.JS to work. I've heard that the former jxcore lead is now working at Microsoft on the Chakra engine. |
... which is what I'm using for development. So apologies in advance if I hit some
Good to know 👍 |
How's the uglify speed compared to Node/V8 for a large file like |
For the work I do I rarely run into issues, the latest being nodejs/node-chakracore#170 but those are always questionable use of "feature" IMHO.
Not that far behind in terms of performance either. |
A more comprehensive fix for: #1537