fix: keep the media type first when flattening nested @media queries - #4481
Conversation
📝 WalkthroughWalkthroughNested media-query evaluation detects media types, including ChangesNested media-query ordering
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 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 |
|
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
packages/test-data/tests-unit/media-nested-type/media-nested-type.less (1)
8-12: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick winAdd a
notregression case.The implementation handles both
notandonly, but this fixture only exercisesonly. Add nested@media not screeninput and the matching expected CSS; otherwise modifier-specific regressions can pass unnoticed.notnegates the entire query, unlikeonly. (w3.org)🤖 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 `@packages/test-data/tests-unit/media-nested-type/media-nested-type.less` around lines 8 - 12, Add a nested `@media` not screen regression fixture alongside the existing `@media` only screen case, and add its matching expected CSS output. Keep the current only case unchanged so the fixture covers both media-query modifiers and verifies that not negates the entire query.
🤖 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 `@packages/less/lib/less/tree/nested-at-rule.js`:
- Around line 42-45: Update startsWithMediaType to recognize any string
identifier as a media type after optional “not” or “only” modifiers, rather than
restricting matches to MEDIA_TYPES. Exclude the operators “and”, “or”, and “,”
so nested unknown media types are reordered correctly without treating query
operators as media types.
---
Nitpick comments:
In `@packages/test-data/tests-unit/media-nested-type/media-nested-type.less`:
- Around line 8-12: Add a nested `@media` not screen regression fixture alongside
the existing `@media` only screen case, and add its matching expected CSS output.
Keep the current only case unchanged so the fixture covers both media-query
modifiers and verifies that not negates the entire query.
🪄 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: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 71e7a9f8-d106-427b-826a-f82ce3e65417
📒 Files selected for processing (3)
packages/less/lib/less/tree/nested-at-rule.jspackages/test-data/tests-unit/media-nested-type/media-nested-type.csspackages/test-data/tests-unit/media-nested-type/media-nested-type.less
4de62db to
449944a
Compare
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
packages/test-data/tests-unit/media-nested-type/media-nested-type.css (1)
1-35: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winCover the
notmodifier in the regression fixture.The implementation explicitly handles leading
not, but this fixture only exercisesonlyand bare/unknown media types. Add a nestednot screeninput and expected output to protect that path.🤖 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 `@packages/test-data/tests-unit/media-nested-type/media-nested-type.css` around lines 1 - 35, Extend the media nested-type regression fixture with a case using a leading “not screen” modifier, including its nested rule and expected output. Keep the existing only, bare, and unknown media-type cases unchanged, and ensure the new fixture exercises the implementation path handling leading not.
🤖 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 `@packages/less/lib/less/tree/nested-at-rule.js`:
- Around line 54-64: Update the media-type detection logic around the fragment
head calculation to inspect the first non-whitespace token of Anonymous values
rather than treating the entire value as a media type. After optional not/only,
require the derived head to be a bare identifier; return false for parenthesized
or otherwise non-identifier conditions, while preserving Keyword and valid
Expression handling.
In `@packages/test-data/tests-unit/media-nested-type/media-nested-type.less`:
- Line 23: Insert a blank line immediately before each of the three SCSS
comments in media-nested-type.less, including the locations around the existing
comments at lines 23, 29, and 35, without changing their ordering or other
styling.
---
Nitpick comments:
In `@packages/test-data/tests-unit/media-nested-type/media-nested-type.css`:
- Around line 1-35: Extend the media nested-type regression fixture with a case
using a leading “not screen” modifier, including its nested rule and expected
output. Keep the existing only, bare, and unknown media-type cases unchanged,
and ensure the new fixture exercises the implementation path handling leading
not.
🪄 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: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 8db31cd5-d61e-4ea5-be5f-f9ba8fe7f5a1
📒 Files selected for processing (3)
packages/less/lib/less/tree/nested-at-rule.jspackages/test-data/tests-unit/media-nested-type/media-nested-type.csspackages/test-data/tests-unit/media-nested-type/media-nested-type.less
449944a to
6615258
Compare
|
The Anonymous case can't occur: a feature condition like |
I think it can occur, if someone does |
Signed-off-by: 林晨 (Leo Cheng) <leo-cheng@vip.qq.com>
6615258 to
412f135
Compare
|
You're right - it read the whole Anonymous value as the head, so |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 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 `@packages/less/lib/less/tree/nested-at-rule.js`:
- Around line 52-71: Update startsWithMediaType so Keyword and Anonymous
fragments also skip leading not/only modifiers before evaluating the media type.
Inspect the first non-modifier token and preserve the existing condition
handling for following parenthesized expressions, so inputs like “not (color)”
are not classified as media types.
In `@packages/test-data/tests-unit/media-nested-type/media-nested-type.less`:
- Line 46: Update the LESS variable declaration `@fc` in the media-nested-type
fixture so the active scss/at-rule-no-unknown lint rule no longer rejects it,
using a narrowly scoped LESS-specific override or suppression for this line
only.
🪄 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: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 8ea5efec-824b-4708-a3ff-0b8fc25aa791
📒 Files selected for processing (3)
packages/less/lib/less/tree/nested-at-rule.jspackages/test-data/tests-unit/media-nested-type/media-nested-type.csspackages/test-data/tests-unit/media-nested-type/media-nested-type.less
|
Both are non-issues after checking against actual behavior:
|
When nested
@mediablocks are flattened, a media type coming from an inner block currently lands after the outer conditions, e.g.produced
@media (max-width: 500px) and screen { … }, which is invalid CSS - the spec requires the media type to lead the query.This moves any media type (
screen,only screen,print and (color), …) to the front of the flattened query, so the example now yields@media screen and (max-width: 500px). Queries without a media type, and those already in the correct order, are unchanged.Closes #3694
Closes #3764
Summary by CodeRabbit
Bug Fixes
@mediaflattening to preserve and correctly reorder media types when they appear inside conditional media queries, includingnot/onlyprefixed cases.Tests
@mediablocks, including unknown media-type and variable/feature-condition guard scenarios.