-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
Preserve parens around expressions in default export declaration that start with function or class #6133
Conversation
It's not something specific to as-expressions. As per the spec, the same happens with other default-exported expressions whose first operand starts with one of these keywords: Prettier 1.17.1 --parser typescript Input: export default function log(){} * 2; Output: SyntaxError: Expression expected. (1:33)
> 1 | export default function log(){} * 2;
| ^
2 | |
@thorn0 Can you think of practical cases that aren't yet covered? The one you mentioned is correct per the spec, but doesn't happen in practice because isn't valid in the runtime, so we shouldn't need to worry about that. |
It surely feels like a stretch, but still, I can imagine somebody doing things like these: export default (function(){ /* ... */ }.toString());
export default (class WeirdSingleton { /* ... */ }.getInstance()); And don't forget about the upcoming pipeline operator. It totally makes sense for a function or a class expression to be its left-hand operand. |
Thanks @thorn0, those are more practical use cases. Unfortunately because of the format of the AST it's a bit hard to detect when there's a We do have some code to try and guess what is the "left most" node in an expression to add ASI protection. I'll see if we can reuse some of the code for that. Otherwise, we have to manually handle all possible cases. |
Tagged template literals get extra parens: Prettier pr-6133 --parser babylon Input: export default (function() {})`a`; Output: export default ((function() {})`a`); |
@thorn0 Thank you for testing! Now it only wraps the expression being exported if the function/class isn't already wrapped. |
|
||
exports[`function_in_template.js 1`] = ` | ||
====================================options===================================== | ||
parsers: ["flow", "typescript"] |
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 wonder why these tests aren't run using babel
.
return false; | ||
} | ||
|
||
return path.call.apply( |
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.
Why not use spread syntax here instead of apply
and concat
?
Overall looks great, but I did manage to produce superfluous parens: Prettier pr-6133 --parser babel Input: export default (function(){} |> a)``; Output: export default ((function() {} |> a)``); |
@thorn0 Thanks for stress testing! After years of Prettier this is the first bug report we've had, I think we're on the safe side now. From a practical point of view, we've covered enough cases IMO. |
Closes #6083
✨Try the playground for this PR✨