Skip to content

fix(eslint-plugin): respect enableMediaQueryOrder in sort-keys - #1756

Merged
mellyeliu merged 2 commits into
meta:mainfrom
gunjanjaswal:fix/1416-sort-keys-media-query-cascade
Aug 16, 2026
Merged

mellyeliu merged 2 commits into
meta:mainfrom
gunjanjaswal:fix/1416-sort-keys-media-query-cascade

Conversation

@gunjanjaswal

Copy link
Copy Markdown
Contributor

What

Adds an enableMediaQueryOrder option to the @stylexjs/sort-keys rule. When it is on, the rule keeps the authored order of @media condition keys instead of sorting them alphabetically. It defaults to false, 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 @media keys alphabetically inverts that cascade. A correctly authored max-width: 768px followed by max-width: 640px gets flipped by --fix, so mobile viewports pick up the tablet value. Right now the only workaround is wrapping every responsive property in an eslint-disable block.

How

All @media keys 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-keys passes 59/59.

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.
@vercel

vercel Bot commented Jul 13, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
stylex Ignored Ignored Aug 16, 2026 9:09am

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jul 13, 2026

@nmn nmn left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@gunjanjaswal

Copy link
Copy Markdown
Contributor Author

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.

@mellyeliu
mellyeliu merged commit 65974e2 into meta:main Aug 16, 2026
2 checks passed
@gunjanjaswal

Copy link
Copy Markdown
Contributor Author

Thanks for landing it, @mellyeliu — and @nmn for the review. Appreciate it!

@gunjanjaswal
gunjanjaswal deleted the fix/1416-sort-keys-media-query-cascade branch August 17, 2026 07:16
@davidhellmann

Copy link
Copy Markdown

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.

@gunjanjaswal

Copy link
Copy Markdown
Contributor Author

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 enableMediaQueryOrder does is tell the rule to leave the authored order of @media keys alone, because StyleX resolves overlapping queries by source order (last matching one wins). The only thing it checks is whether a key is a media query, via isMediaQuery → name.startsWith('@media'). There's no px parsing or numeric breakpoint comparison anywhere in that path; the px breakpoints in the examples are just illustrative.

So a rem-based cascade works the same way:

default: '1rem',
'@media (min-width: 40rem)': '1.25rem',
'@media (min-width: 64rem)': '1.5rem',

With enableMediaQueryOrder: true, the rule keeps that authored order untouched, exactly as it would for px.

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.

@davidhellmann

Copy link
Copy Markdown

Thanks, makes sense for the lint rule. My problem is actually in the compiler though, and only when the media queries come from defineConsts. Literal keys get rewritten into ranges, so rem works there. With consts, collectWidthBounds in processStylexRules only picks up px, so rem queries get no width sort key and end up ordered by the declaration value.

Small repro with 0.19.1, tablet red / desktop orange / monitor green:

Repro (0.19.1)
// media.stylex.js
export const px = stylex.defineConsts({
  tablet: "@media (min-width: 768px)",
  desktop: "@media (min-width: 1024px)",
  monitor: "@media (min-width: 1440px)",
});

export const rem = stylex.defineConsts({
  tablet: "@media (min-width: 48rem)",
  desktop: "@media (min-width: 64rem)",
  monitor: "@media (min-width: 90rem)",
});
// styles.js
import { px, rem } from "./media.stylex.js";

export const styles = stylex.create({
  withPx: {
    backgroundColor: {
      default: "gray",
      [px.tablet]: "red",
      [px.desktop]: "orange",
      [px.monitor]: "green",
    },
  },
  withRem: {
    backgroundColor: {
      default: "gray",
      [rem.tablet]: "red",
      [rem.desktop]: "orange",
      [rem.monitor]: "green",
    },
  },
});

Output of processStylexRules:

@media (min-width: 768px){.x4cdzuf.x4cdzuf:not(#\#){background-color:red}}
@media (min-width: 1024px){.xrxp6g0.xrxp6g0:not(#\#){background-color:orange}}
@media (min-width: 1440px){.xrv0v99.xrv0v99:not(#\#){background-color:green}}
@media (min-width: 90rem){.x1jwv5v0.x1jwv5v0:not(#\#){background-color:green}}
@media (min-width: 64rem){.xk0p8vg.xk0p8vg:not(#\#){background-color:orange}}
@media (min-width: 48rem){.xeow5kk.xeow5kk:not(#\#){background-color:red}}
image image image image

Thanks!

@gunjanjaswal

Copy link
Copy Markdown
Contributor Author

You nailed it — the compiler, not the lint rule. collectWidthBounds in processStylexRules only produced a sort key when the bound's unit was px (or 0), so rem/em returned null and those queries fell through to source order. With defineConsts that's exactly your reversed-cascade repro.

The reason it was px-only: the sort compares a single numeric bound value, and 48 (rem) and 768 (px) aren't commensurable without knowing the root font size, which can be customized — so rather than assume 1rem = 16px and risk interleaving px and rem wrong, it left rem unsorted. The gap is that two rem bounds (or two em bounds) keep their order for any root size, so they can safely sort against each other; only cross-unit comparison is the ambiguous part.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@stylexjs/sort-keys ESLint rule breaks max-width media query cascade when using enableMediaQueryOrder

4 participants