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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Edge-case exception while printing single-parameter arrow function #13198

Closed
1 task done
Zalathar opened this issue Apr 24, 2021 · 2 comments 路 Fixed by #13204
Closed
1 task done

[Bug]: Edge-case exception while printing single-parameter arrow function #13198

Zalathar opened this issue Apr 24, 2021 · 2 comments 路 Fixed by #13204
Labels
i: needs triage outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@Zalathar
Copy link
Contributor

馃捇

  • Would you like to work on a fix?

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

const { parse } = require("@babel/parser");
const t = require("@babel/types");
const { default: generate } = require("@babel/generator");

// Create an identifier with a location and a leading comment.
const param = parse("(/*x*/ a)").program.body[0].expression;

// Create an async arrow function without a location.
const ast = t.arrowFunctionExpression(
    [param],
    t.identifier("a"),
    /* async */ true,
);

// TypeError: Cannot read property 'start' of undefined
generate(ast);

Configuration file name

No response

Configuration

No response

Current and expected behavior

Expected: Babel generates valid output source for the AST.

Actual:

$ yarn node repro.js 
/Users/user/babel/packages/babel-generator/lib/generators/methods.js:137
      if (firstParam.loc && firstParam.loc.start.line > node.loc.start.line) {
                                                                 ^

TypeError: Cannot read property 'start' of undefined
    at Generator.ArrowFunctionExpression (/Users/user/babel/packages/babel-generator/lib/generators/methods.js:137:66)
    at /Users/user/babel/packages/babel-generator/lib/printer.js:287:19
    at Buffer.withSource (/Users/user/babel/packages/babel-generator/lib/buffer.js:188:28)
    at Generator.withSource (/Users/user/babel/packages/babel-generator/lib/printer.js:153:15)
    at Generator.print (/Users/user/babel/packages/babel-generator/lib/printer.js:286:10)
    at Generator.generate (/Users/user/babel/packages/babel-generator/lib/printer.js:45:10)
    at Generator.generate (/Users/user/babel/packages/babel-generator/lib/index.js:26:18)
    at generate (/Users/user/babel/packages/babel-generator/lib/index.js:100:14)
    at Object.<anonymous> (/Users/user/babel/repro.js:8:1)
    at Module._compile (node:internal/modules/cjs/loader:1092:14)

Environment

Possible solution

Originally I was planning to PR an additional null check, but on closer inspection (and in light of #13136) I think it makes more sense to get rid of all this special-case code and just delegate to the regular param-printing code.

Additional context

No response

@babel-bot
Copy link
Collaborator

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

@nicolo-ribaudo
Copy link
Member

Yeah let's just remove that code at this point

@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 Jul 27, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 27, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
i: needs triage outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
3 participants