feat(formatter): support css prop, styled jsx, and member/computed styled.tags#17990
Conversation
CodSpeed Performance ReportMerging this PR will not alter performanceComparing Summary
Footnotes
|
Allow formatting of tagged templates with member expressions like `styled.div`, enabling better support for styled-components and similar libraries.
a3342c1 to
ae389fa
Compare
styled.div tagged templatesstyled.tags
There was a problem hiding this comment.
Pull request overview
This PR extends the formatter's embedded language support to handle CSS in styled-components and styled-jsx patterns, building on existing embedded language formatting infrastructure.
Changes:
- Extended tag name extraction to support member expressions (
styled.div) and computed members (styled["a"]) - Added context tracking for template literals in CSS props and styled-jsx elements
- Implemented formatting for CSS content in JSX
cssprops and<style jsx>elements
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 10 comments.
Show a summary per file
| File | Description |
|---|---|
crates/oxc_formatter/src/print/template.rs |
Added get_tag_name function to extract tag names from complex expressions; added try_format_css_template to format template literals in CSS contexts |
crates/oxc_formatter/src/print/jsx/mod.rs |
Added context management for css prop and styled-jsx template literals with helper functions to detect these patterns |
crates/oxc_formatter/src/formatter/context.rs |
Added TemplateLiteralContext enum and stack management methods for tracking template literal formatting contexts |
crates/oxc_formatter/src/formatter/mod.rs |
Exported new TemplateLiteralContext type |
apps/oxfmt/test/cli/embedded_languages/fixtures/embedded_languages.js |
Added test cases for member expression tags, css props, and styled-jsx |
apps/oxfmt/test/cli/embedded_languages/__snapshots__/embedded_languages.test.ts.snap |
Updated snapshots with formatted output for new test cases |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
apps/oxfmt/test/cli/embedded_languages/fixtures/embedded_languages.js
Outdated
Show resolved
Hide resolved
apps/oxfmt/test/cli/embedded_languages/fixtures/embedded_languages.js
Outdated
Show resolved
Hide resolved
Instead of manually maintaining a context stack for `styled-jsx` and `css` props, derive the context directly from the AST parent nodes. This simplifies `FormatContext` and reduces state management overhead in JSX printing.
Dunqing
left a comment
There was a problem hiding this comment.
Looks good, just some refactoring needed.
Dunqing
left a comment
There was a problem hiding this comment.
Thank you! I pushed a commit to rename get_template_literal_context and add comments
|
@leaysgur, would you mind reviewing this last round? If okay, merge it! |
|
Thanks, I will take a look soon. 🙆🏻 |
leaysgur
left a comment
There was a problem hiding this comment.
Thank you so much! LGMT this time. 🙆🏻
This PR adds support for embedded language formatting in styled-components and styled-jsx patterns:
Changes
styled.div`...`tagged templates: Allow formatting of tagged templates with member expressions likestyled.div, enabling better support for styled-componentsstyled["computed"]`...`: Handle static computed member expressions in styled tagsstyled(Component)`...`: Handle styled calls with component arguments for styled-components<div css={`...`} />: Format CSS content in JSXcssprop<style jsx>{`...`}</style>: Format Styled JSX content in JSX elementsRelated Issues
Known Limitations
css`color: ${THIS_VAR};`are not yet supported