From b378e7ecc9c43b0a20731f23459fbc61c03b17ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arnaud=20Barr=C3=A9?= Date: Mon, 11 Mar 2024 11:50:33 +0100 Subject: [PATCH] fix(parser): fix span for JSXEmptyExpression with comment (#2673) [playground](https://oxc-project.github.io/oxc/playground/?code=3YCAAICVgICAgICAgICejwtjmCpbllbPawdM2eEFKwhGb62iFlQWu39yrLCA) --------- Co-authored-by: Boshen --- crates/oxc_parser/src/jsx/mod.rs | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/crates/oxc_parser/src/jsx/mod.rs b/crates/oxc_parser/src/jsx/mod.rs index 502650c95..2262f6e67 100644 --- a/crates/oxc_parser/src/jsx/mod.rs +++ b/crates/oxc_parser/src/jsx/mod.rs @@ -231,20 +231,24 @@ impl<'a> ParserImpl<'a> { ) -> Result> { let span = self.start_span(); self.bump_any(); // bump `{` - let expr = match self.cur_kind() { - // {} empty - Kind::RCurly => { - let span = self.start_span(); - JSXExpression::EmptyExpression(self.ast.jsx_empty_expression(self.end_span(span))) - } - // {expr} - _ => self.parse_jsx_assignment_expression().map(JSXExpression::Expression)?, - }; - if in_jsx_child { - self.expect_jsx_child(Kind::RCurly)?; + + let expr = if self.eat(Kind::RCurly) { + // Handle comment between curly braces (ex. `{/* comment */}`) + // ^^^^^^^^^^^^^ span + let span = self.end_span(span); + JSXExpression::EmptyExpression( + self.ast.jsx_empty_expression(Span::new(span.start + 1, span.end - 1)), + ) } else { - self.expect(Kind::RCurly)?; - } + let expr = self.parse_jsx_assignment_expression().map(JSXExpression::Expression)?; + if in_jsx_child { + self.expect_jsx_child(Kind::RCurly) + } else { + self.expect(Kind::RCurly) + }?; + expr + }; + Ok(self.ast.jsx_expression_container(self.end_span(span), expr)) }