Skip to content

fix(parser/css): parse conditional block correctly inside embedded snippets#9966

Merged
siketyan merged 1 commit intobiomejs:mainfrom
siketyan:fix/GH-9113
Apr 13, 2026
Merged

fix(parser/css): parse conditional block correctly inside embedded snippets#9966
siketyan merged 1 commit intobiomejs:mainfrom
siketyan:fix/GH-9113

Conversation

@siketyan
Copy link
Copy Markdown
Member

@siketyan siketyan commented Apr 13, 2026

Summary

Note

AI Assistance Disclosure: I used the Codex agent to investigate the problem, create a regression test, and draft the PR description. All changes and output are reviewed by a human (me).

Fixes #9113

Embedded CSS snippets are parsed as CSS_SNIPPET_ROOT, which allows top-level declarations, but nested conditional blocks like @media { ... } were still using the regular parse_rule_block() path unless the parser was already inside a nesting block. As a result, declarations such as height: 40px; inside @media were misparsed as selectors.

This pull request fixes the parser to treat conditional blocks in embedded snippets the same way as nesting blocks, and parse them with parse_declaration_or_rule_list_block(). This allows declarations inside @media within CSS-in-JS snippets to be parsed correctly.

Test Plan

Added a snapshot test.

Docs

N/A

…ippets

Co-authored-by: Codex <noreply@openai.com>
@siketyan siketyan requested review from a team April 13, 2026 15:20
@siketyan siketyan self-assigned this Apr 13, 2026
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Apr 13, 2026

🦋 Changeset detected

Latest commit: 6fbd0b6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 13 packages
Name Type
@biomejs/biome Patch
@biomejs/cli-win32-x64 Patch
@biomejs/cli-win32-arm64 Patch
@biomejs/cli-darwin-x64 Patch
@biomejs/cli-darwin-arm64 Patch
@biomejs/cli-linux-x64 Patch
@biomejs/cli-linux-arm64 Patch
@biomejs/cli-linux-x64-musl Patch
@biomejs/cli-linux-arm64-musl Patch
@biomejs/wasm-web Patch
@biomejs/wasm-bundler Patch
@biomejs/wasm-nodejs Patch
@biomejs/backend-jsonrpc Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added A-Project Area: project A-Parser Area: parser L-CSS Language: CSS and super languages labels Apr 13, 2026
@siketyan siketyan marked this pull request as ready for review April 13, 2026 15:21
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 13, 2026

Walkthrough

This pull request addresses issue #9113 by fixing the parsing and formatting of @media and other conditional blocks within embedded CSS snippets. The CSS parser now recognises styled-component embeddings and processes their conditional blocks correctly. A regression test has been added to validate the fix with a TypeScript file containing styled-components template literals with media queries.

Suggested reviewers

  • ematipico
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: fixing parser handling of conditional blocks within embedded CSS snippets.
Description check ✅ Passed The description clearly explains the problem, solution, and includes a test plan; it relates directly to the changeset.
Linked Issues check ✅ Passed The changes fully address issue #9113 by enabling correct parsing of @media and conditional blocks within embedded CSS snippets.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the conditional block parsing issue in embedded CSS; no unrelated modifications detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
crates/biome_css_parser/src/syntax/block/conditional_block.rs (1)

22-24: Keep the rustdoc aligned with the new branch condition.

The docs above still describe only nesting-block state, but Line 23 now also depends on styled embedding context. Worth updating to avoid future head-scratching.

Suggested doc tweak
-/// If the current state of the parser indicates that it's inside a nesting block (e.g., inside a style rule),
-/// this function parses the content as a declaration or rule list block. Otherwise, it parses the content
-/// as a rule block.
+/// If the parser is inside a nesting block (e.g., inside a style rule), or if the source is an embedded
+/// styled snippet, this function parses the content as a declaration-or-rule list block.
+/// Otherwise, it parses the content as a rule block.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@crates/biome_css_parser/src/syntax/block/conditional_block.rs` around lines
22 - 24, Update the rustdoc above the conditional in conditional_block.rs to
reflect that the branch now fires not only when p.state().is_nesting_block is
true but also when the source embedding kind is styled
(p.source_type.as_embedding_kind() matches EmbeddingKind::Styled); edit the doc
text to mention both conditions (nesting-block state and styled embedding
context) and, if present, adjust any examples or notes to show the
styled-embedding case alongside the nesting-block case.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@crates/biome_css_parser/src/syntax/block/conditional_block.rs`:
- Around line 22-24: Update the rustdoc above the conditional in
conditional_block.rs to reflect that the branch now fires not only when
p.state().is_nesting_block is true but also when the source embedding kind is
styled (p.source_type.as_embedding_kind() matches EmbeddingKind::Styled); edit
the doc text to mention both conditions (nesting-block state and styled
embedding context) and, if present, adjust any examples or notes to show the
styled-embedding case alongside the nesting-block case.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 8ff06353-2838-40ff-9609-333dabbca8bb

📥 Commits

Reviewing files that changed from the base of the PR and between 1df7e3a and 6fbd0b6.

📒 Files selected for processing (3)
  • .changeset/fast-zebras-move.md
  • crates/biome_css_parser/src/syntax/block/conditional_block.rs
  • crates/biome_service/src/workspace/server.tests.rs

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Apr 13, 2026

Merging this PR will not alter performance

✅ 29 untouched benchmarks
⏩ 227 skipped benchmarks1


Comparing siketyan:fix/GH-9113 (6fbd0b6) with main (0a60ce0)2

Open in CodSpeed

Footnotes

  1. 227 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

  2. No successful run was found on main (1df7e3a) during the generation of this report, so 0a60ce0 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@siketyan siketyan merged commit 322675e into biomejs:main Apr 13, 2026
30 checks passed
@github-actions github-actions bot mentioned this pull request Apr 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Parser Area: parser A-Project Area: project L-CSS Language: CSS and super languages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 Invalid parsing of @media in embedded CSS-in-JS

2 participants