🐛 Ternaries with parenthesized identifiers as consequents parse as Arrow Function Expressions #1077
Closed
1 task done
Labels
A-Parser
Area: parser
L-JavaScript
Language: JavaScript and super languages
S-Bug-confirmed
Status: report has been confirmed as a valid bug
Environment information
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:
It fails on the arrow function expression inside the JSX expression (the
{() => null}
). But, this only fails to parse when:?
) is a plain identifierAfter 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
The text was updated successfully, but these errors were encountered: