Repository navigation
fix(eslint-plugin): respect enableMediaQueryOrder in sort-keys - #1756
Conversation
The sort-keys rule sorted `@media` condition keys alphabetically. When a project compiles with StyleX's `enableMediaQueryOrder`, overlapping media queries are resolved by source order (last matching query wins), so the alphabetical autofix inverted the cascade and made the wrong breakpoint win. Add an `enableMediaQueryOrder` rule option (default false, keeping current behavior). When enabled, the rule preserves the authored relative order of `@media` keys instead of sorting them alphabetically, while still sorting all other property keys. Fixes meta#1416.
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
|
Thanks again for approving this, @nmn. It's been green and approved for about a month now — is there anything still needed from me before it can land, or is it just waiting on an import? The only red is the perf/size benchmark jobs, which look non-blocking, but happy to rebase or dig into those if it helps. |
|
Thanks for landing it, @mellyeliu — and @nmn for the review. Appreciate it! |
|
Any reason why this is fixed to PX units? I know mixed units are a problem but would be great that this also work with REM. |
|
Good question, but this option actually doesn't look at units at all, so it already works with rem (or em, or anything else). All So a rem-based cascade works the same way: default: '1rem',
'@media (min-width: 40rem)': '1.25rem',
'@media (min-width: 64rem)': '1.5rem',With If you're seeing rem media queries get reordered, that'll be the default (option off), where media-query keys are sorted as plain strings. That's unit-agnostic too, so turning the option on fixes it regardless of px vs rem. Happy to dig in if you've got a snippet that behaves differently from that. |
|
You nailed it — the compiler, not the lint rule. The reason it was px-only: the sort compares a single numeric bound value, and Opened #1964: it carries the unit on the bound and compares within a unit, so rem sorts by value like px does, with px and rem kept in separate blocks instead of interleaved. px-only output is unchanged. Your repro is the motivating case and there's a test for exactly that mix. Thanks for the precise write-up and the minimal repro — made this easy to pin. |




What
Adds an
enableMediaQueryOrderoption to the@stylexjs/sort-keysrule. When it is on, the rule keeps the authored order of@mediacondition keys instead of sorting them alphabetically. It defaults tofalse, so existing behavior is unchanged.Why
Fixes #1416. When a project compiles with StyleX's
enableMediaQueryOrder, overlapping media queries are resolved by source order, so the last matching query wins. Sorting@mediakeys alphabetically inverts that cascade. A correctly authoredmax-width: 768pxfollowed bymax-width: 640pxgets flipped by--fix, so mobile viewports pick up the tablet value. Right now the only workaround is wrapping every responsive property in aneslint-disableblock.How
All
@mediakeys share a single at-rule priority, so the comparator was falling through to an alphabetical tiebreak. With the option on, two media-query keys are treated as already ordered when reporting and compared as equal when autofixing (the sort is stable and falls back to the original index), so their source order is kept. Non-media keys still sort normally. This matches how the compiler orders queries by source order, and it can fall back to the #1407 breakpoint sort once that merges.Tests
Added regression tests to
stylex-sort-keys-test.js: valid cases for max-width and min-width cascades under the option, an invalid case confirming the default still reorders, and one confirming regular keys still sort with the option on.jest stylex-sort-keyspasses 59/59.