Skip to content

refactor(parser): align jsx parsing to tsc#11314

Merged
graphite-app[bot] merged 1 commit intomainfrom
r/jsx
May 27, 2025
Merged

refactor(parser): align jsx parsing to tsc#11314
graphite-app[bot] merged 1 commit intomainfrom
r/jsx

Conversation

@leaysgur
Copy link
Member

@leaysgur leaysgur commented May 27, 2025

My original plan was to rework the JSX parser from the entry point, as I had found in #11232.

However, when I read through all the TSC JSX parser code, I found that what we are doing is not much different.


As for perf perspective, the OXC currently requires rewind in 3 places for JSX:

  • 1: parse_jsx_expression() - lookahead
    • To determine if it is a JSXElement(<C) OR JSXFragment(<>) at the entry point
  • 2: parse_jsx_child() - checkpoint+rewind
    • The same above OR breaking list(</)
  • 3: parse_jsx_child() - checkpoint+rewind
    • To determine if it is JSXChild::Spread({...Expression}) OR JSXChild::ExpressionContainer({JSXExpression})

These don't exist in TSC, they just use union type or loose ASTs.

But we have different and explicit ASTs in Rust, it seems they can't be easily eliminated.

And unfortunately, I tried, but there was no remarkable improvement.

  • For 1, 3045952 => Minifier +3%, but no effect for parser
  • For 2, I wish we had Kind::LessThanSlash...
  • For 3, a12ff21 => No effect for parser

As a result, I think it would be better to keep the current clean and explicit code.

@graphite-app
Copy link
Contributor

graphite-app bot commented May 27, 2025

How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

@github-actions github-actions bot added A-parser Area - Parser C-cleanup Category - technical debt or refactoring. Solution not expected to change behavior labels May 27, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented May 27, 2025

CodSpeed Instrumentation Performance Report

Merging #11314 will degrade performances by 8.23%

Comparing r/jsx (08eb1eb) with main (b53b053)

Summary

❌ 1 regressions
✅ 37 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark BASE HEAD Change
mangler[cal.com.tsx] 2.8 ms 3 ms -8.23%

@Boshen Boshen changed the title refactor(parser): Make JSX parser TS like refactor(parser): align jsx parsing to tsc May 27, 2025
@leaysgur leaysgur marked this pull request as ready for review May 27, 2025 08:27
Copilot AI review requested due to automatic review settings May 27, 2025 08:27
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the JSX parser to mirror TypeScript’s approach by centralizing JSX element and fragment parsing and updating lookahead logic.

  • Replaced manual lookahead branches in parse_jsx_attribute_value with a unified parse_jsx_expression match.
  • Enhanced parse_literal_or_identifier lookahead to detect fragments via Kind::RAngle.
  • Removed outdated JSX comment and streamlined fragment detection.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
crates/oxc_parser/src/jsx/mod.rs Simplify attribute value parsing using parse_jsx_expression
crates/oxc_parser/src/js/expression.rs Extend lookahead to recognize <> fragment start
Comments suppressed due to low confidence (1)

crates/oxc_parser/src/js/expression.rs:1018

  • The new lookahead condition for fragment detection (p.at(Kind::RAngle)) isn't covered by existing tests. Add a parser test case for <> ... </> inside expressions to ensure this branch is exercised.
p.at(Kind::RAngle) || p.cur_kind().is_identifier_or_keyword()

@Boshen Boshen added the 0-merge Merge with Graphite Merge Queue label May 27, 2025
Copy link
Member

Boshen commented May 27, 2025

Merge activity

My original plan was to rework the JSX parser from the entry point, as I had found in #11232.

However, when I read through all the TSC JSX parser code, I found that what we are doing is not much different.

---

As for perf perspective, the OXC currently requires rewind in 3 places for JSX:

- 1: `parse_jsx_expression()` - `lookahead`
  - To determine if it is a `JSXElement`(`<C`) OR `JSXFragment`(`<>`) at the entry point
- 2: `parse_jsx_child()` - `checkpoint+rewind`
  - The same above OR breaking list(`</`)
- 3: `parse_jsx_child()` - `checkpoint+rewind`
  - To determine if it is `JSXChild::Spread`(`{...Expression}`) OR `JSXChild::ExpressionContainer`(`{JSXExpression}`)

These don't exist in TSC, they just use union type or loose ASTs.

But we have different and explicit ASTs in Rust, it seems they can't be easily eliminated.

And unfortunately, I tried, but there was no remarkable improvement.

- For 1, 3045952 => Minifier +3%, but no effect for parser
- For 2, I wish we had `Kind::LessThanSlash`...
- For 3, a12ff21 => No effect for parser

As a result, I think it would be better to keep the current clean and explicit code.
@graphite-app graphite-app bot merged commit 08eb1eb into main May 27, 2025
25 checks passed
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label May 27, 2025
@graphite-app graphite-app bot deleted the r/jsx branch May 27, 2025 09:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-parser Area - Parser C-cleanup Category - technical debt or refactoring. Solution not expected to change behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants