fix(parser): fix failed to parse JSXChild after JSXEmptyExpression (#2726)

fixes #2723
This commit is contained in:
Boshen 2024-03-15 13:39:20 +08:00 committed by GitHub
parent 53a8e7ffbe
commit 798a1fde09
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 32 additions and 14 deletions

View file

@ -34,7 +34,7 @@ seq-macro = { workspace = true }
memchr = "2.7.1" memchr = "2.7.1"
[dev-dependencies] [dev-dependencies]
oxc_ast = { workspace = true } oxc_ast = { workspace = true, features = ["serialize"] }
miette = { workspace = true } miette = { workspace = true }
serde_json = { workspace = true } serde_json = { workspace = true }
ouroboros = "0.18.3" # for `multi-thread` example ouroboros = "0.18.3" # for `multi-thread` example

View file

@ -215,7 +215,7 @@ impl<'a> ParserImpl<'a> {
} }
// {expr} // {expr}
Kind::LCurly => self Kind::LCurly => self
.parse_jsx_expression_container(true) .parse_jsx_expression_container(/* is_jsx_child */ true)
.map(JSXChild::ExpressionContainer) .map(JSXChild::ExpressionContainer)
.map(Some), .map(Some),
// text // text
@ -232,13 +232,17 @@ impl<'a> ParserImpl<'a> {
let span = self.start_span(); let span = self.start_span();
self.bump_any(); // bump `{` self.bump_any(); // bump `{`
let expr = if self.eat(Kind::RCurly) { let expr = if self.at(Kind::RCurly) {
if in_jsx_child {
self.expect_jsx_child(Kind::RCurly)
} else {
self.expect(Kind::RCurly)
}?;
let span = self.end_span(span);
// Handle comment between curly braces (ex. `{/* comment */}`) // Handle comment between curly braces (ex. `{/* comment */}`)
// ^^^^^^^^^^^^^ span // ^^^^^^^^^^^^^ span
let span = self.end_span(span); let expr = self.ast.jsx_empty_expression(Span::new(span.start + 1, span.end - 1));
JSXExpression::EmptyExpression( JSXExpression::EmptyExpression(expr)
self.ast.jsx_empty_expression(Span::new(span.start + 1, span.end - 1)),
)
} else { } else {
let expr = self.parse_jsx_assignment_expression().map(JSXExpression::Expression)?; let expr = self.parse_jsx_assignment_expression().map(JSXExpression::Expression)?;
if in_jsx_child { if in_jsx_child {
@ -339,7 +343,7 @@ impl<'a> ParserImpl<'a> {
match self.cur_kind() { match self.cur_kind() {
Kind::Str => self.parse_literal_string().map(JSXAttributeValue::StringLiteral), Kind::Str => self.parse_literal_string().map(JSXAttributeValue::StringLiteral),
Kind::LCurly => { Kind::LCurly => {
let expr = self.parse_jsx_expression_container(false)?; let expr = self.parse_jsx_expression_container(/* is_jsx_child */ false)?;
Ok(JSXAttributeValue::ExpressionContainer(expr)) Ok(JSXAttributeValue::ExpressionContainer(expr))
} }
Kind::LAngle => { Kind::LAngle => {

View file

@ -1,3 +1,3 @@
codegen_misc Summary: codegen_misc Summary:
AST Parsed : 14/14 (100.00%) AST Parsed : 15/15 (100.00%)
Positive Passed: 14/14 (100.00%) Positive Passed: 15/15 (100.00%)

View file

@ -0,0 +1,13 @@
const A = (
<div>
{/* comment */}
AAAA
</div>
);
const B = (
<div>
BBBB
{/* comment */}
</div>
);

View file

@ -1,6 +1,6 @@
parser_misc Summary: parser_misc Summary:
AST Parsed : 14/14 (100.00%) AST Parsed : 15/15 (100.00%)
Positive Passed: 14/14 (100.00%) Positive Passed: 15/15 (100.00%)
Negative Passed: 8/8 (100.00%) Negative Passed: 8/8 (100.00%)
× Unexpected token × Unexpected token

View file

@ -1,9 +1,10 @@
prettier_misc Summary: prettier_misc Summary:
AST Parsed : 14/14 (100.00%) AST Parsed : 15/15 (100.00%)
Positive Passed: 8/14 (57.14%) Positive Passed: 8/15 (53.33%)
Expect to Parse: "pass/oxc-1740.tsx" Expect to Parse: "pass/oxc-1740.tsx"
Expect to Parse: "pass/oxc-2087.ts" Expect to Parse: "pass/oxc-2087.ts"
Expect to Parse: "pass/oxc-2394.ts" Expect to Parse: "pass/oxc-2394.ts"
Expect to Parse: "pass/oxc-2674.tsx" Expect to Parse: "pass/oxc-2674.tsx"
Expect to Parse: "pass/oxc-2723.jsx"
Expect to Parse: "pass/swc-1627.js" Expect to Parse: "pass/swc-1627.js"
Expect to Parse: "pass/swc-8243.tsx" Expect to Parse: "pass/swc-8243.tsx"