Skip to content
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

Correctly render function expression inside simplified expression statements #2696

Merged

Conversation

lukastaegert
Copy link
Member

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • no

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

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

@lukastaegert lukastaegert merged commit 14a17af into master Feb 17, 2019
@lukastaegert lukastaegert deleted the gh-2684-add-parentheses-to-functions-if-necessary branch February 17, 2019 07:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invalid code generated from large minified file
1 participant