Repository navigation
fix(compiler): keep a nested rule's parent selector in source order - #475
Merged
danstepanov merged 1 commit intoOct 31, 2026
Merged
Conversation
Contributor
Author
No current device pairIn my test app the bundler flattens a nested rule for browsers before the compiler sees it, and #461 is what stops that. So the build without this PR compiles the flat rule, which is already right, and a pair would show no difference. The tests in the description are the evidence. |
YevheniiKotyrlo
marked this pull request as ready for review
October 11, 2026 13:59
sammoore
approved these changes
Oct 30, 2026
sammoore
left a comment
Contributor
There was a problem hiding this comment.
Good fix! cc @danstepanov
danstepanov
force-pushed
the
fix/nested-rule-selector-order
branch
from
October 31, 2026 01:11
88d34f8 to
503385b
Compare
getClassNameSelectors reversed lightningcss's selector array in place to walk it from the subject outwards. A nested rule, such as @media, @container or &:hover inside a rule, reads its parent's selector list again, so it saw that selector reversed: `.group .x { @media (…) { … } }` put the nested declarations on `.group` inside an `.x` container. The walk now reads a reversed copy, so every rule compiles against the selector as written.
danstepanov
force-pushed
the
fix/nested-rule-selector-order
branch
from
October 31, 2026 01:39
503385b to
ddcc216
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
getClassNameSelectorswalks a selector from its subject outwards, and it reverses lightningcss's selector array in place to do so. A nested rule reads its parent's selector list again, so it sees that selector reversed. Measured onmain:main.group .x { color: red; @media (min-width: 0px) { color: blue } }.group, under an.xcontainer.x, under a.groupcontainer@container (min-width: 0px)or&:hoverSo a
.xinside.grouprenders red, where Chromium 153, Firefox 155 and WebKit 26.6 render blue. A single-compound selector reverses to itself, which is whymedia query nested in rulespasses.Solution
The walk reads
selector.toReversed(), a copy, so every rule compiles against the selector as written.Tests
src/__tests__/compiler/compiler.test.tsx:@media,@containerand&:hovernested in.group .xput every declaration on.xunder a.groupcontainer, and nothing on.group.src/__tests__/native/grouping.test.tsx: a.xinside.grouprenders the nested@mediadeclaration.main, 4 of the 22 cases in the two files fail. Restoring the in-place reverse fails the same 4.Verification
On Windows with Node 26:
yarn lintcleanyarn typecheckcleanyarn test --coverage: every failure also fails onmainon this machine (the four babel cases)yarn buildcleanyarn example expo export --platform webexportedNo existing issue tracks this. I searched for
nested mediaandnesting selector reversed.Merge order
It shares
compiler/selector-builder.tswith #459, which changes the same call; whichever lands second rebases.