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

Fix arrow function body parsing #117

Open
wants to merge 3 commits into
base: new-ast
Choose a base branch
from

Conversation

gabejohnson
Copy link

Fixes #116

The issue with the current parsing rules and representation is that arrow function bodies are not statements, they're either function blocks or expressions. Looking at the specification we see that the expressions are AssignmentExpression in particular.

Making this change fixes the issues mentioned in #116, but breaks minification tests. This turns out not to be a problem, as minification for the cases covered by the failing tests resulted in behavior inconsistent with the non-minified version:

let x = (() => { 42 })(); // not minified, x == undefined

let x=(()=>42)(); // minified before change, x == 42

let x=(()=>{42})(); // minified after change, x == undefined

We add three failing tests:
  1. {()=>{};``}
  2. {x=>x;``}
  3. {f:x=>x,y:2}

We also add a fourth test mirroring the third but with a function block
body instead of an expression. The hope is that we get no regressions
when we change the representation of arrow functions.
The issue with the current parsing rules and representation is that
arrow function bodies are not statements, they're either function blocks
or expressions. Looking at the
specification (https://www.ecma-international.org/ecma-262/#prod-ExpressionBody)
we see that the expressions are `AssignmentExpression` in particular.

Making this change appears to fix the additional tests, but it breaks
minification.
The minifier was treating arrow functions with block bodies incorrectly
if they contained a single expression statement with no return
statement. It was transforming them into arrow functions with expression
bodies.

This means for instance that in
```js
let x = (() => { 42 })();
```

`x` would equal `undefined` before minification, but would equal `42`
after minification. The fix is to change the tests to match the correct
behavior.
@chrisnevers
Copy link

Thanks for the fix, @gabejohnson, I'm encountering the same error. @erikd are there any issues with this being merged?

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.

Exception when parsing object literals containing arrow functions
2 participants