Correctly render function expression inside simplified expression statements #2696
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR contains:
Are tests included?
Breaking Changes?
List any relevant issue numbers:
Resolves #2684
Description
When a call expression containing a function expression is rendered inside a simplified expression statement (i.e. conditional expression, logical expression, sequence expression, see below for examples), it could happen that the function expression would be rendered as a function statement which have slightly different semantics. Specifically, a function statement cannot be used as an IIFE. This PR fixes this by wrapping the call expression in round brackets whenever it would be rendered as an expression statement.
It also contains a comprehensive test suite based on the nice list of examples provided by @kzc at #2684, thanks a lot!
Examples where Rollup currently fails that are all fixed by this PR:
https://rollupjs.org/repl?version=1.1.2&shareable=JTdCJTIybW9kdWxlcyUyMiUzQSU1QiU3QiUyMm5hbWUlMjIlM0ElMjJtYWluLmpzJTIyJTJDJTIyY29kZSUyMiUzQSUyMiUyRiUyRiUyMGFsbCUyMGxpbmVzJTIwYXJlJTIwdmFsaWQlMjBjb2RlJTIwd2hpY2glMjBiZWNvbWVzJTIwaW52YWxpZCU1Q24lMkYlMkYlMjB3aGVuJTIwcmVuZGVyZWQlNUNudHJ1ZSUyMCUzRiUyMGZ1bmN0aW9uKHgpJTdCY29uc29sZS5sb2coJ29rJyklN0QoKSUyMCUzQSUyMDIlM0IlNUNuZmFsc2UlMjAlM0YlMjBudWxsJTIwJTNBJTIwZnVuY3Rpb24oeCklN0Jjb25zb2xlLmxvZygnb2snKSU3RCgpJTNCJTVDbiU1Q250cnVlJTIwJTI2JTI2JTIwZnVuY3Rpb24oeCklN0Jjb25zb2xlLmxvZygnb2snKSU3RCgpJTNCJTVDbmZhbHNlJTIwJTdDJTdDJTIwZnVuY3Rpb24oeCklN0Jjb25zb2xlLmxvZygnb2snKSU3RCgpJTNCJTVDbiU1Q24wJTJDJTIwZnVuY3Rpb24oeCklN0Jjb25zb2xlLmxvZygnb2snKSU3RCgpJTNCJTVDbiUyMiUyQyUyMmlzRW50cnklMjIlM0F0cnVlJTdEJTVEJTJDJTIyb3B0aW9ucyUyMiUzQSU3QiUyMmZvcm1hdCUyMiUzQSUyMmVzbSUyMiUyQyUyMm5hbWUlMjIlM0ElMjJteUJ1bmRsZSUyMiUyQyUyMmFtZCUyMiUzQSU3QiUyMmlkJTIyJTNBJTIyJTIyJTdEJTJDJTIyZ2xvYmFscyUyMiUzQSU3QiU3RCU3RCUyQyUyMmV4YW1wbGUlMjIlM0FudWxsJTdE