-
Notifications
You must be signed in to change notification settings - Fork 346
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
identity transformation adds parens to IIFE #81
Comments
Apparently because needsParens returns true for the function. I don't understand why needsParens checks:
|
I believe this happens because the If this theory is true, then (function() {
})(); should print without the extra layer of parentheses (because |
Yes, that already reprints correctly. |
For what it's worth, the function definitely does need parentheses, since the following code would be illegal: function() {
}(); Also, |
also doesn't add extra parens. |
That'll be because the function expression is no longer By the way, these explanations are descriptive (of how things work currently, which is broken) rather than normative (I'm not trying to justify the way things work!). |
Ahh, I see. |
After thinking about this a little bit, I think needsParens is conflating two concepts that should be separate (precedence and
but with the expression
the parens can be removed as long as there's another leading parenthesis to distinguish it from a statement. |
Some thoughts: in the failing case we're talking about, the The I'm not yet sure what algorithm is suggested by this observation, but there's a possibility of salvaging |
As @spicyj pointed out in benjamn/recast#81 (comment), the NodePath.prototype.needsParens method has been conflating precedence logic with garden-path parser lookahead quirks. Rather than removing that edge case logic from the function, I have made it possible to skip the canBeFirstInStatement/firstInStatement checks by explicitly passing true to needsParens. Since the behavior of needsParens differs only if client code passes true for the assumeExpressionContext parameter, this change is completely backwards-compatible and does not require a minor version bump.
Just published v0.5.19 to NPM with a fix for this issue: https://www.npmjs.org/package/recast Thanks for bringing my attention to this problem area, @spicyj! |
Thanks for fixing! |
As @spicyj pointed out in benjamn/recast#81 (comment), the NodePath.prototype.needsParens method has been conflating precedence logic with garden-path parser lookahead quirks. Rather than removing that edge case logic from the function, I have made it possible to skip the canBeFirstInStatement/firstInStatement checks by explicitly passing true to needsParens. Since the behavior of needsParens differs only if client code passes true for the assumeExpressionContext parameter, this change is completely backwards-compatible and does not require a minor version bump.
As @spicyj pointed out in benjamn/recast#81 (comment), the NodePath.prototype.needsParens method has been conflating precedence logic with garden-path parser lookahead quirks. Rather than removing that edge case logic from the function, I have made it possible to skip the canBeFirstInStatement/firstInStatement checks by explicitly passing true to needsParens. Since the behavior of needsParens differs only if client code passes true for the assumeExpressionContext parameter, this change is completely backwards-compatible and does not require a minor version bump.
As @spicyj pointed out in benjamn/recast#81 (comment), the NodePath.prototype.needsParens method has been conflating precedence logic with garden-path parser lookahead quirks. Rather than removing that edge case logic from the function, I have made it possible to skip the canBeFirstInStatement/firstInStatement checks by explicitly passing true to needsParens. Since the behavior of needsParens differs only if client code passes true for the assumeExpressionContext parameter, this change is completely backwards-compatible and does not require a minor version bump.
Given the source file
(with no parens around the function expression), recast outputs the file as
even if no changes are made (e.g., using
examples/identity
).The text was updated successfully, but these errors were encountered: