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

generate() produces incorrect output for arrow function expression #12055

Closed
1 task done
overlookmotel opened this issue Sep 11, 2020 · 5 comments · Fixed by #12082
Closed
1 task done

generate() produces incorrect output for arrow function expression #12055

overlookmotel opened this issue Sep 11, 2020 · 5 comments · Fixed by #12082
Labels
claimed Has PR i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: generator

Comments

@overlookmotel
Copy link
Contributor

Bug 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
@babel-bot
Copy link
Collaborator

Hey @overlookmotel! 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."

@JLHwung
Copy link
Contributor

JLHwung commented Sep 12, 2020

You can take a look at

// Walk up the print stack to determine if our node can come first
// in statement.
function isFirstInStatement(
printStack: Array<Object>,
{ considerArrow = false, considerDefaultExports = false } = {},
): boolean {
let i = printStack.length - 1;
let node = printStack[i];
i--;
let parent = printStack[i];
while (i > 0) {
if (
t.isExpressionStatement(parent, { expression: node }) ||
(considerDefaultExports &&
t.isExportDefaultDeclaration(parent, { declaration: node })) ||
(considerArrow && t.isArrowFunctionExpression(parent, { body: node }))
) {
return true;
}
if (
(hasPostfixPart(node, parent) && !t.isNewExpression(parent)) ||
(t.isSequenceExpression(parent) && parent.expressions[0] === node) ||
t.isConditional(parent, { test: node }) ||
t.isBinary(parent, { left: node }) ||
t.isAssignmentExpression(parent, { left: node })
) {
node = parent;
i--;
parent = printStack[i];
} else {
return false;
}
}
return false;
}

In your case, the printStack is Array (2) [ ArrowExpression, ObjectExpression ], it seems that the while-loop (which will consider arrow functions) is incorrectly skipped because i == 0. I believe the condition should be loosen and only applied to the second if condition where i-- is applied.

PR is welcome. If you don't know how to clone Babel, follow these steps: (you need to have make and yarn available on your machine).

  1. Write a comment there to let other possible contributors know that you are working on this bug.
  2. Fork the repo
  3. Run git clone https://github.com/<YOUR_USERNAME>/babel.git && cd babel
  4. Run yarn && make bootstrap
  5. Wait ⏳
  6. Run make watch (or make build whenever you change a file)
  7. Add a test (only input.js; output.js will be automatically generated) to packages/babel-generator/test/fixtures/parentheses
  8. Update the code!
  9. yarn jest babel-generator to run the tests
    • If some test outputs don't match but the new results are correct, you can delete the bad output.js files and run the tests again
  10. If it is working, run make test to run all the tests
  11. Run git push and open a PR!

@overlookmotel
Copy link
Contributor Author

@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.

overlookmotel added a commit to overlookmotel/babel that referenced this issue Sep 19, 2020
@overlookmotel
Copy link
Contributor Author

Failing test: https://github.com/overlookmotel/babel/tree/arrow-function-object-expression-fix

I've started work on a fix.

overlookmotel added a commit to overlookmotel/babel that referenced this issue Sep 19, 2020
overlookmotel added a commit to overlookmotel/babel that referenced this issue Sep 19, 2020
@overlookmotel
Copy link
Contributor Author

PR submitted #12082. @JLHwung please have a look when you get a chance.

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Dec 26, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
claimed Has PR i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: generator
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants