`generate()` produces incorrect output for arrow function expression
See original GitHub issueBug Report
- I would like to work on a fix!
Current behavior A clear and concise description of the behavior.
generate()
produces incorrect code for arrow function expression.
const generate = require('@babel/generator').default;
const node = t.arrowFunctionExpression( [], t.objectExpression( [] ) );
console.log( generate( node ) );
Output:
() => {}
Output should be:
() => ({})
Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)
No config used. The above is the complete reproduction case.
Environment
System:
OS: macOS Mojave 10.14.6
Binaries:
Node: 14.9.0 - ~/.nvm/versions/node/v14.9.0/bin/node
npm: 6.14.8 - ~/.nvm/versions/node/v14.9.0/bin/npm
npmPackages:
@babel/core: ^7.11.6 => 7.11.6
@babel/generator: ^7.11.6 => 7.11.6
@babel/helper-module-transforms: ^7.11.0 => 7.11.0
@babel/parser: ^7.11.5 => 7.11.5
@babel/plugin-transform-modules-commonjs: ^7.10.4 => 7.10.4
@babel/plugin-transform-react-jsx: ^7.10.4 => 7.10.4
@babel/register: ^7.11.5 => 7.11.5
@babel/traverse: ^7.11.5 => 7.11.5
@babel/types: ^7.11.5 => 7.11.5
babel-jest: ^26.3.0 => 26.3.0
babel-plugin-dynamic-import-node: ^2.3.3 => 2.3.3
eslint: ^7.8.1 => 7.8.1
jest: ^26.4.2 => 26.4.2
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Arrow function expressions - JavaScript - MDN Web Docs
Arrow functions cannot use yield within their body and cannot be created as generator functions. Try it.
Read more >Anomalies in JavaScript arrow functions - LogRocket Blog
The following example shows how an arrow function can be used as a callback function, especially with array methods like map() , filter()...
Read more >Arrow Functions in JavaScript: Fat & Concise Syntax - SitePoint
Learn how to use JavaScript arrow functions, understand fat and concise arrow syntax, and what to be aware of when using them in...
Read more >Why am I obtaining this error in this TypeScript arrow function?
First the way your arrow function is defined doesn't look right. It looks like you are defining a function but the parameter definitions...
Read more >How To Use Javascript Arrow Functions & This Keyword
ES6 introduced a new way of writing JavaScript functions called arrow function that uses a fat arrow ( => ). This guide will...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You can take a look at https://github.com/babel/babel/blob/c8f0b6dc1ecbe99d34f9188863b41582bf55d043/packages/babel-generator/src/node/parentheses.js#L286-L322
In your case, the
printStack
isArray (2) [ ArrowExpression, ObjectExpression ]
, it seems that thewhile
-loop (which will consider arrow functions) is incorrectly skipped becausei == 0
. I believe the condition should be loosen and only applied to the secondif
condition wherei--
is applied.PR is welcome. If you don’t know how to clone Babel, follow these steps: (you need to have
make
andyarn
available on your machine).git clone https://github.com/<YOUR_USERNAME>/babel.git && cd babel
yarn && make bootstrap
make watch
(ormake build
whenever you change a file)input.js
;output.js
will be automatically generated) topackages/babel-generator/test/fixtures/parentheses
yarn jest babel-generator
to run the testsoutput.js
files and run the tests againmake test
to run all the testsgit push
and open a PR!@JLHwung Thanks loads for the swift reply and advice to where to look in the code base.
I will try to put a PR together in next few days. I’ll post another comment here when/if I start work on it, so there’s no duplicated effort by another contributor.