Parse JSX and TSX source files#147
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughJSX and TSX are recognized as a shared source language. A dedicated parser extracts directly quoted ChangesJSX and TSX parsing
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant SourceDocument
participant attribute_parser
participant jsx_parser
participant Sorter
SourceDocument->>attribute_parser: provide source and JSX profile
attribute_parser->>jsx_parser: extract quoted class attributes
jsx_parser-->>attribute_parser: return attribute ranges
attribute_parser-->>Sorter: provide ClassAttribute ranges
Sorter->>SourceDocument: reorder class values within ranges
Possibly related PRs
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
| Filename | Overview |
|---|---|
| rustywind-core/src/jsx_parser.rs | Adds the Winnow-backed JSX parser for quoted class and className attributes. |
| rustywind-core/src/source.rs | Adds the shared JSX source language profile for .jsx and .tsx files. |
| rustywind-core/src/app.rs | Routes structured attribute parsing through the new shared parser profile. |
| tests/tailwind-compare/lib.mjs | Extends the comparison helpers to extract JSX attributes and verify source preservation. |
Reviews (3): Last reviewed commit: "Add recursion limit to JSX parser" | Re-trigger Greptile
Add a shared Winnow-backed source profile that isolates quoted class attributes from JavaScript and TypeScript syntax. Extend the pinned parity harness with JSX coverage and enforce source preservation outside parsed attribute values.
Accept comments between JSX attributes so valid TSX files do not fall back to unchanged output. Keep dynamic quoted Svelte attributes in source-preservation checks without treating them as static scrambling candidates.
be6643f to
4d9d45a
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
rustywind-core/src/jsx_parser.rs (1)
383-395: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winDuplicate
consume_characterinstead of reusingtemplate_parser's.This file redefines
consume_characterwith the same body as the existing privateconsume_characterintemplate_parser.rs. This PR already promotes five sibling helpers (consume_line_comment,consume_block_comment,consume_quoted,consume_escape,consume_regex,expression_keyword_allows_regex) topub(crate)specifically for reuse here —consume_charactershould follow the same pattern instead of being duplicated.♻️ Proposed fix
-fn consume_character(input: &mut Input<'_>, expected: char) -> Option<()> { - let character = any::<_, winnow::error::ContextError> - .parse_next(input) - .ok()?; - (character == expected).then_some(()) -} +// removed; use `crate::template_parser::consume_character` insteadAnd in
template_parser.rs:-fn consume_character(input: &mut Input<'_>, expected: char) -> Option<()> { +pub(crate) fn consume_character(input: &mut Input<'_>, expected: char) -> Option<()> {🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@rustywind-core/src/jsx_parser.rs` around lines 383 - 395, Remove the duplicate consume_character helper from jsx_parser.rs and reuse template_parser::consume_character instead. Promote the existing consume_character in template_parser.rs to pub(crate), then update JSX parser references and imports to call that shared helper while preserving its current behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@rustywind-core/src/jsx_parser.rs`:
- Around line 41-136: Add a recursion-depth guard covering the mutually
recursive JavaScript and JSX traversal rooted at parse_javascript, including
nested group parsing, template-expression parsing, and
parse_jsx/parse_jsx_children descent. Track depth through each recursive call,
reject or gracefully stop parsing when a configured maximum is reached, and
ensure depth is restored after returning so normal shallow input behavior
remains unchanged.
---
Nitpick comments:
In `@rustywind-core/src/jsx_parser.rs`:
- Around line 383-395: Remove the duplicate consume_character helper from
jsx_parser.rs and reuse template_parser::consume_character instead. Promote the
existing consume_character in template_parser.rs to pub(crate), then update JSX
parser references and imports to call that shared helper while preserving its
current behavior.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: fe78c1e4-b928-4a18-99cb-9ee44467199f
📒 Files selected for processing (19)
CHANGELOG.mdREADME.mdrustywind-cli/src/main.rsrustywind-cli/src/options.rsrustywind-core/CHANGELOG.mdrustywind-core/src/app.rsrustywind-core/src/attribute_parser.rsrustywind-core/src/jsx_parser.rsrustywind-core/src/lib.rsrustywind-core/src/markup_parser.rsrustywind-core/src/source.rsrustywind-core/src/template_parser.rsrustywind-core/tests/template_aware_sorting.rstests/tailwind-compare/README.mdtests/tailwind-compare/compare.mjstests/tailwind-compare/lib.mjstests/tailwind-compare/test/lib.test.mjsxtask/README.mdxtask/src/commands/compare.rs
Prevent unbounded recursive parsing when extracting JSX class names by adding a depth limit and failing safely when exceeded. This hardens parsing for deep nested groups, JSX, and template literals while preserving valid behavior for normal inputs.
Summary
.jsxand.tsxclassandclassNameattributes in real JSX elementsWhy
JSX and TSX previously used the unknown-language regex fallback. That sorted simple attributes, but it could not reliably distinguish JSX attributes from class-like text in the surrounding program. The real-world comparison suite also lacked a JSX corpus and did not verify that formatter changes stayed inside parsed attribute values.
Impact
JSX and TSX files now receive syntax-aware handling through
SourceLanguage::Jsx, whether inferred from a filename or selected with--language jsxor--language tsx. Static quoted attributes are sorted while dynamic expressions and other source text remain unchanged.This adds a public
SourceLanguagevariant, so downstream exhaustive matches must handleJsx.Validation
just fmtjust clippycargo test --workspace --no-fail-fastnpm testcargo xtask compare run --offlineAll four corpora passed with zero extraction mismatches, known-order mismatches, or source-preservation failures.
Summary by CodeRabbit
New Features
class/classNameattributes.jsxandtsxCLI language options, with.jsx/.tsxfiles handled automatically.Bug Fixes
Documentation
Breaking Changes
Jsxvariant shared by.jsxand.tsx, requiring downstream exhaustive handling updates.