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

Missing parens when generating ArrowFunctionExpression #9836

Open
wonnage opened this Issue Apr 9, 2019 · 1 comment

Comments

Projects
None yet
2 participants
@wonnage
Copy link

commented Apr 9, 2019

Bug Report

Generating code for ArrowFunctionExpression not wrapped in ExpressionStatement is missing parens when the body is an ObjectExpression.

Input Code

import { parse } from '@babel/parser';
import generate from '@babel/generator';

ast = parse("() => ({ foo: 'bar' })");
node = ast.program.body[0];
generate(node, {});

Expected behavior/code

"() => ({\n  foo: "bar"\n});"

Actual output

"() => {\n  foo: 'bar'\n}"

Babel Configuration (.babelrc, package.json, cli command)
None (just using the default options)

Environment

  • Babel version(s): 7.4.0
  • Node/npm version:Node 8/npm 6
  • OS: Linux
  • Monorepo: no
  • How you are using Babel: Node API

Possible Solution
The issue is that the parentheses injection logic doesn't work on a lone ArrowFunctionExpression here: https://github.com/babel/babel/blob/master/packages/babel-generator/src/node/parentheses.js#L274

The two nodes in printStack are [ArrowFunctionExpression, ObjectExpression]; after popping both of those, i is 0 and the loop never runs. Some options include checking for i >= 0, adding another case on L235 (https://github.com/babel/babel/blob/master/packages/babel-generator/src/node/parentheses.js#L285), or maybe this is an invalid use case and I should close this bug. I'm not familiar with the code so I'm not sure what side effects changing the conditions might introduce.

@babel-bot

This comment has been minimized.

Copy link
Collaborator

commented Apr 9, 2019

Hey @wonnage! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community
that typically always has someone willing to help. You can sign-up here
for an invite.

@wonnage wonnage changed the title Possible off by one error in parens generation for arrow functions Missing parens when generating ArrowFunctionExpression Apr 10, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.