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

🐛 Ternaries with parenthesized identifiers as consequents parse as Arrow Function Expressions #1077

Closed
1 task done
faultyserver opened this issue Dec 6, 2023 · 3 comments · Fixed by #1113
Closed
1 task done
Assignees
Labels
A-Parser Area: parser L-JavaScript Language: JavaScript and super languages S-Bug-confirmed Status: report has been confirmed as a valid bug

Comments

@faultyserver
Copy link
Contributor

Environment information

main

What happened?

Playground Link

This is a weirdly specific error that I noticed while trying to test formatting our monorepo for regressions. As shown in the playground, this code fails to parse:

!isDisabled ? (
    foo
  ) : (
    <Tooltip>
      {() => null}
    </Tooltip>
  );

It fails on the arrow function expression inside the JSX expression (the {() => null}). But, this only fails to parse when:

  • The consequent of the ternary (the first branch after the ?) is a plain identifier
  • The consequent is wrapped in parentheses

After messing around a bit, it looks like what's actually happening here is the parser thinks this is a conditional expression where the consequent is an Arrow Function Expression, rather than treating it as a parenthesized identifier, which is confirmed by looking at the Syntax tab on that playground link as well. Then the JSX in the alternate ends up being parsed as a return type rather than JSX at all, hence the errors that show up here.

It also doesn't matter if the file is parsed as TypeScript or JavaScript, or even with JSX enabled or not. The parse always errors.

As a comparison, ASTExplorer shows that all other JS parsers are able to understand this as a parenthesized identifier rather than starting an arrow expression.

Expected result

Parsing the ternary should succeed as a conditional expression with a parenthesized identifier as the consequent and a JSX expression as the alternate.

Code of Conduct

  • I agree to follow Biome's Code of Conduct
@faultyserver faultyserver added A-Parser Area: parser L-JavaScript Language: JavaScript and super languages S-Bug-confirmed Status: report has been confirmed as a valid bug labels Dec 6, 2023
@kalleep
Copy link
Contributor

kalleep commented Dec 6, 2023

Hey I can take a look at this one if you don't mind

@faultyserver
Copy link
Contributor Author

@kalleep Sure! I'll assign it to you

@kalleep
Copy link
Contributor

kalleep commented Dec 6, 2023

Thanks started to do some digging earlier and pretty sure the issue is from here: https://github.com/biomejs/biome/blob/main/crates/biome_js_parser/src/syntax/function.rs#L587

// error
a ? (b) : (<p>{() => null}</p>);
// error
a ? (b) : <p>{() => null}</p>;

Both of these fails to parse correctly and seems like part of the consequence, up until the arrow, is parsed as typescript return parameter. This results in https://github.com/biomejs/biome/blob/main/crates/biome_js_parser/src/syntax/function.rs#L599 succeeding.

Will keep looking at this tomorrow and work on a fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Parser Area: parser L-JavaScript Language: JavaScript and super languages S-Bug-confirmed Status: report has been confirmed as a valid bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants