Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(eslint-plugin): stop autofixing hover keys that carry a pseudo-element (#5487)

Ojas Mutrejacfc97c4bd327
diff --git a/internal/eslint-plugin-astryx/README.md b/internal/eslint-plugin-astryx/README.md
index 33d563ad24..42ac66ec16 100644
--- a/internal/eslint-plugin-astryx/README.md
+++ b/internal/eslint-plugin-astryx/README.md
@@ -587,7 +587,9 @@ const styles = stylex.create({
**Scope:** `:hover` on the styled element itself. A key that hovers something else — `:is(th:hover *)`, `stylex.when.ancestor(':hover')` — styles a descendant when an **ancestor** is hovered, which is a different question (a row may legitimately highlight around a disabled control) and is left alone. The rule is deliberately unconditional rather than scoped to components that have a disabled state: on an element that can never be disabled the guard is a no-op, and asking the question per component is what leaves the gaps.
-Autofixable, and mirrored at runtime by `.github/scripts/disabled-hover-audit.js`, which forces `:hover` on every disabled element in every story in Chromium and fails on any painted difference.
+**Pseudo-elements:** `:hover::after` and `:hover::before` are reported but never autofixed. StyleX 0.19.0 drops the pseudo-element's weight from the ranking of any key that also carries a `:not()` guard (priority 8330 becomes 3240), so the guarded rule loses to a plain `::after` rule and the hover treatment silently never paints. No spelling of the guard inside such a key avoids it, and an already guarded pseudo-element key is reported too. The fix is outside the selector: keep the disabled case out in JS (apply the class only when the control is enabled) and silence that one key with an `eslint-disable` comment that says so.
+
+Autofixable for every other key, and mirrored at runtime by `.github/scripts/disabled-hover-audit.js`, which forces `:hover` on every disabled element in every story in Chromium and fails on any painted difference.
Ships as an **error in both tiers**: core and lab are clean, and this keeps them that way.
diff --git a/internal/eslint-plugin-astryx/no-hover-on-disabled.js b/internal/eslint-plugin-astryx/no-hover-on-disabled.js
index 7afc89c56a..670b0d34d4 100644
--- a/internal/eslint-plugin-astryx/no-hover-on-disabled.js
+++ b/internal/eslint-plugin-astryx/no-hover-on-disabled.js
@@ -38,6 +38,16 @@
* descendant when an ANCESTOR is hovered, which is a different question (a
* row may legitimately highlight around a disabled control) and is left
* alone.
+ *
+ * PSEUDO-ELEMENTS: `:hover::after` and `:hover::before` are reported but never
+ * autofixed. StyleX 0.19.0 ranks a key by the pseudo-classes it understands,
+ * and a `:not()` guard in the same key as a pseudo-element drops the
+ * pseudo-element's weight from the ranking (priority 8330 becomes 3240). The
+ * guarded rule then loses to a plain `::after` rule at 8000, so the hover
+ * treatment silently never paints (#5442 broke SelectableCard, Thumbnail and
+ * ClickableCard's overlay this way). No spelling of the guard inside such a key
+ * escapes it, so the fix for these keys is outside the selector: keep the
+ * disabled case out in JS and silence the one key with a comment that says so.
*/
/** Zero-specificity guard appended to a self-hover selector. */
@@ -59,6 +69,11 @@ function hasDisabledGuard(key) {
return /:not\([^)]*(?::disabled|\[aria-disabled)/.test(key);
}
+/** Carries a pseudo-ELEMENT (`::after`, `::before`, ...). */
+function hasPseudoElement(key) {
+ return key.includes('::');
+}
+
/**
* Insert the guard, keeping any pseudo-ELEMENT last.
*
@@ -110,6 +125,16 @@ const rule = {
unguardedHover:
"'{{key}}' still matches a disabled element — browsers suppress a disabled control's events, not its hover styling. " +
"Write '{{fixed}}' instead (`:where()` adds no specificity, so overrides are unaffected).",
+ unguardedHoverPseudoElement:
+ "'{{key}}' still matches a disabled element, and the rule cannot autofix it: StyleX 0.19.0 drops the pseudo-element's " +
+ 'priority from a key that also carries a `:not()` guard, so a guarded `::after`/`::before` hover rule loses to a plain ' +
+ 'one and never paints. Keep the disabled case out in JS instead (apply the class only when the control is enabled) ' +
+ 'and silence this key with an eslint-disable comment that says so.',
+ guardedHoverPseudoElement:
+ "'{{key}}' is guarded, but StyleX 0.19.0 drops the pseudo-element's priority from a key that carries a `:not()` guard, " +
+ 'so a plain `::after`/`::before` rule outranks it and the hover treatment never paints. Drop the guard from the key, ' +
+ 'keep the disabled case out in JS (apply the class only when the control is enabled), and silence this key with an ' +
+ 'eslint-disable comment that says so.',
},
schema: [],
},
@@ -118,7 +143,22 @@ const rule = {
Property(node) {
if (!isInsideStylexCreate(node)) return;
const key = keyOf(node);
- if (!isSelfHoverKey(key) || hasDisabledGuard(key)) return;
+ if (!isSelfHoverKey(key)) return;
+
+ // No guard written into a pseudo-element key survives StyleX's
+ // ranking, so these are reported without a fix, guarded or not.
+ if (hasPseudoElement(key)) {
+ context.report({
+ node: node.key,
+ messageId: hasDisabledGuard(key)
+ ? 'guardedHoverPseudoElement'
+ : 'unguardedHoverPseudoElement',
+ data: {key},
+ });
+ return;
+ }
+
+ if (hasDisabledGuard(key)) return;
const fixed = guardKey(key);
context.report({
@@ -143,4 +183,4 @@ const rule = {
};
export default rule;
-export {GUARD, guardKey, isSelfHoverKey, hasDisabledGuard};
+export {GUARD, guardKey, isSelfHoverKey, hasDisabledGuard, hasPseudoElement};
diff --git a/internal/eslint-plugin-astryx/no-hover-on-disabled.test.mjs b/internal/eslint-plugin-astryx/no-hover-on-disabled.test.mjs
index 2e6eeb74dd..c5f0b3e130 100644
--- a/internal/eslint-plugin-astryx/no-hover-on-disabled.test.mjs
+++ b/internal/eslint-plugin-astryx/no-hover-on-disabled.test.mjs
@@ -18,6 +18,40 @@ const GUARD = ':where(:not(:disabled,[aria-disabled="true"]))';
ruleTester.run('no-hover-on-disabled', rule, {
valid: [
+ // The escape hatch for a pseudo-element hover key: the component keeps the
+ // disabled case out in JS and says so on the one key.
+ {
+ code: `
+ import * as stylex from '@stylexjs/stylex';
+ const styles = stylex.create({
+ hoverOnPointer: {
+ '@media (hover: hover)': {
+ // eslint-disable-next-line rule-to-test/no-hover-on-disabled -- only applied when !isDisabled
+ ':hover::after': {backgroundColor: 'red'},
+ },
+ },
+ });
+ `,
+ },
+ // A pseudo-element WITHOUT hover is not this rule's concern.
+ {
+ code: `
+ import * as stylex from '@stylexjs/stylex';
+ const styles = stylex.create({
+ card: {'::after': {backgroundColor: 'transparent'}},
+ });
+ `,
+ },
+ // Hover on an ancestor styling a pseudo-element stays out of scope, like
+ // any other ancestor hover.
+ {
+ code: `
+ import * as stylex from '@stylexjs/stylex';
+ const styles = stylex.create({
+ cell: {':is(tr:hover *)::after': {backgroundColor: 'red'}},
+ });
+ `,
+ },
// The guarded form, property-first.
{
code: `
@@ -118,7 +152,9 @@ ruleTester.run('no-hover-on-disabled', rule, {
});
`,
},
- // A pseudo-ELEMENT has to stay last, so the guard goes before it.
+ // A pseudo-ELEMENT key is reported but never autofixed: StyleX 0.19.0
+ // ranks the guarded form below a plain `::after` rule, so the fix would
+ // turn a disabled-hover lint into a hover that never paints.
{
code: `
import * as stylex from '@stylexjs/stylex';
@@ -126,13 +162,46 @@ ruleTester.run('no-hover-on-disabled', rule, {
tab: {opacity: {default: 0, ':hover::after': 1}},
});
`,
- errors: [{messageId: 'unguardedHover'}],
- output: `
+ errors: [{messageId: 'unguardedHoverPseudoElement'}],
+ },
+ {
+ code: `
+ import * as stylex from '@stylexjs/stylex';
+ const styles = stylex.create({
+ tab: {opacity: {default: 0, ':hover::before': 1}},
+ });
+ `,
+ errors: [{messageId: 'unguardedHoverPseudoElement'}],
+ },
+ // A condition-first pseudo-element key is held to the same standard.
+ {
+ code: `
+ import * as stylex from '@stylexjs/stylex';
+ const styles = stylex.create({
+ card: {':hover::after': {'@media (hover: hover)': {backgroundColor: 'red'}}},
+ });
+ `,
+ errors: [{messageId: 'unguardedHoverPseudoElement'}],
+ },
+ // An already guarded pseudo-element key is reported too: it is the shape
+ // the old autofix produced, and it silently loses to a plain `::after`.
+ {
+ code: `
+ import * as stylex from '@stylexjs/stylex';
+ const styles = stylex.create({
+ card: {backgroundColor: {default: 'transparent', ':hover${GUARD}::after': 'red'}},
+ });
+ `,
+ errors: [{messageId: 'guardedHoverPseudoElement'}],
+ },
+ {
+ code: `
import * as stylex from '@stylexjs/stylex';
const styles = stylex.create({
- tab: {opacity: {default: 0, ':hover${GUARD}::after': 1}},
+ card: {backgroundColor: {default: 'transparent', ':hover:not(:disabled)::after': 'red'}},
});
`,
+ errors: [{messageId: 'guardedHoverPseudoElement'}],
},
// A double-quoted key is rewritten single-quoted: the guard carries its
// own double quotes.