From 7fb16f801f97ef0282975389cf90bc8b6dae6e1d Mon Sep 17 00:00:00 2001 From: kimseongyu Date: Mon, 30 Nov 2026 14:30:09 +0900 Subject: [PATCH] [eslint-plugin] allow compound pseudo-classes in valid-styles Co-Authored-By: Gemini 4 Carbon --- .../__tests__/stylex-valid-styles-test.js | 143 ++++++++++++++++++ .../src/reference/cssProperties.js | 25 +-- .../src/rules/isPseudoClassSelector.js | 115 ++++++++++++++ .../eslint-plugin/src/stylex-valid-styles.js | 55 ++++--- 4 files changed, 297 insertions(+), 41 deletions(-) create mode 100644 packages/@stylexjs/eslint-plugin/src/rules/isPseudoClassSelector.js diff --git a/packages/@stylexjs/eslint-plugin/__tests__/stylex-valid-styles-test.js b/packages/@stylexjs/eslint-plugin/__tests__/stylex-valid-styles-test.js index 3aabed866..c8f5de70d 100644 --- a/packages/@stylexjs/eslint-plugin/__tests__/stylex-valid-styles-test.js +++ b/packages/@stylexjs/eslint-plugin/__tests__/stylex-valid-styles-test.js @@ -22,6 +22,20 @@ const eslintTester = new ESLintTester({ eslintTester.run('stylex-valid-styles', rule.default, { valid: [ + // issue #1618 — compound and functional pseudo-classes + ` + import * as stylex from "@stylexjs/stylex"; + + const styles = stylex.create({ + button: { + backgroundColor: { + default: "red", + ":active:not(:disabled)": "blue", + ":hover:not(:disabled)": { default: null, "@media (any-hover: hover) and (any-pointer: fine)": "blue" }, + }, + }, + }); + `, // issue #1701 — numeric values are valid for grid line properties ` import * as stylex from '@stylexjs/stylex'; @@ -1471,6 +1485,135 @@ revert`, }, ], }, + { + code: ` + import * as stylex from '@stylexjs/stylex'; + stylex.create({ + button: { + backgroundColor: { + default: 'transparent', + ':hover:focs': { + default: 'blue', + '@media (prefers-color-scheme: dark)': 'navy', + }, + }, + }, + }); + `, + errors: [ + { + message: + 'Invalid Pseudo class or At Rule used for conditional style value', + }, + ], + }, + { + code: ` + import * as stylex from '@stylexjs/stylex'; + stylex.create({ + button: { + backgroundColor: { + default: 'transparent', + ':hover:not(:disabld)': { + default: 'blue', + '@media (prefers-color-scheme: dark)': 'navy', + }, + }, + }, + }); + `, + errors: [ + { + message: + 'Invalid Pseudo class or At Rule used for conditional style value', + }, + ], + }, + { + code: ` + import * as stylex from '@stylexjs/stylex'; + stylex.create({ + button: { + backgroundColor: { + default: 'transparent', + ':hover:not()': { + default: 'blue', + '@media (prefers-color-scheme: dark)': 'navy', + }, + }, + }, + }); + `, + errors: [ + { + message: + 'Invalid Pseudo class or At Rule used for conditional style value', + }, + ], + }, + { + code: ` + import * as stylex from '@stylexjs/stylex'; + stylex.create({ + button: { + backgroundColor: { + default: 'transparent', + ':hover:not(:disabled': { + default: 'blue', + '@media (prefers-color-scheme: dark)': 'navy', + }, + }, + }, + }); + `, + errors: [ + { + message: + 'Invalid Pseudo class or At Rule used for conditional style value', + }, + ], + }, + { + code: ` + import * as stylex from '@stylexjs/stylex'; + stylex.create({ + button: { + ':hover:not(:focs)': { + opacity: 1, + }, + }, + }); + `, + options: [{ allowOuterPseudoAndMedia: true }], + errors: [ + { + message: + 'Nested styles can only be used for the pseudo selectors in the stylex allowlist and for @media queries', + }, + ], + }, + { + code: ` + import * as stylex from '@stylexjs/stylex'; + stylex.create({ + button: { + backgroundColor: { + default: 'transparent', + '@unknown (min-width: 600px)': { + default: 'blue', + ':hover': 'navy', + }, + }, + }, + }); + `, + errors: [ + { + message: + 'Invalid Pseudo class or At Rule used for conditional style value', + }, + ], + }, { code: ` import * as stylex from '@stylexjs/stylex'; diff --git a/packages/@stylexjs/eslint-plugin/src/reference/cssProperties.js b/packages/@stylexjs/eslint-plugin/src/reference/cssProperties.js index 74e0db8ae..097c8556c 100644 --- a/packages/@stylexjs/eslint-plugin/src/reference/cssProperties.js +++ b/packages/@stylexjs/eslint-plugin/src/reference/cssProperties.js @@ -21,6 +21,7 @@ import makeUnionRule from '../rules/makeUnionRule'; import isNumber from '../rules/isNumber'; import isPercentage from '../rules/isPercentage'; import isAbsoluteLength from '../rules/isAbsoluteLength'; +import isPseudoClassSelector from '../rules/isPseudoClassSelector'; import isRelativeLength from '../rules/isRelativeLength'; import { borderSplitter } from '../utils/split-css-value'; import formatPropertiesWithNodeIndentation from '../utils/formatPropertiesWithNodeIndentation'; @@ -2386,29 +2387,13 @@ export const pseudoElements: RuleCheck = makeUnionRule( makeLiteralRule('::-moz-progress-bar'), ); -export const pseudoClassesAndAtRules: RuleCheck = makeUnionRule( - makeLiteralRule(':first-child'), - makeLiteralRule(':last-child'), - makeLiteralRule(':only-child'), - makeLiteralRule(':nth-child'), - makeLiteralRule(':nth-of-type'), - makeLiteralRule(':empty'), - makeLiteralRule(':hover'), - makeLiteralRule(':focus'), - makeLiteralRule(':focus-visible'), - makeLiteralRule(':focus-within'), - makeLiteralRule(':active'), - makeLiteralRule(':visited'), - makeLiteralRule(':disabled'), +export const pseudoClasses: RuleCheck = isPseudoClassSelector; + +export const atRules: RuleCheck = makeUnionRule( makeRegExRule(/^@media/, 'a media query'), - makeRegExRule(/^@container/, 'a media query'), + makeRegExRule(/^@container/, 'a container query'), makeRegExRule(/^@supports/, 'a supports query'), makeLiteralRule('@starting-style'), ); -export const allModifiers: RuleCheck = makeUnionRule( - pseudoElements, - pseudoClassesAndAtRules, -); - export { CSSProperties }; diff --git a/packages/@stylexjs/eslint-plugin/src/rules/isPseudoClassSelector.js b/packages/@stylexjs/eslint-plugin/src/rules/isPseudoClassSelector.js new file mode 100644 index 000000000..73932059e --- /dev/null +++ b/packages/@stylexjs/eslint-plugin/src/rules/isPseudoClassSelector.js @@ -0,0 +1,115 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow strict + */ + +'use strict'; + +import type { + RuleCheck, + RuleResponse, + Variables, +} from '../stylex-valid-styles'; +import type { Node } from 'estree'; +import makeVariableCheckingRule from '../utils/makeVariableCheckingRule'; + +const SIMPLE_PSEUDO_CLASSES: $ReadOnlySet = new Set([ + ':first-child', + ':last-child', + ':only-child', + ':empty', + ':hover', + ':focus', + ':focus-visible', + ':focus-within', + ':active', + ':visited', + ':disabled', +]); + +const NTH_PSEUDO_CLASSES: $ReadOnlySet = new Set([ + ':nth-child', + ':nth-of-type', +]); + +const SELECTOR_PSEUDO_CLASSES: $ReadOnlySet = new Set([ + ':not', + ':is', + ':where', +]); + +function findCloseParen(input: string, openIndex: number): number { + let depth = 1; + for (let i = openIndex + 1; i < input.length; i++) { + const ch = input[i]; + if (ch === '(') { + depth++; + } else if (ch === ')') { + depth--; + if (depth === 0) { + return i; + } + } + } + return -1; +} + +function isValidPseudoClassSelector(selector: string): boolean { + if (!selector.startsWith(':')) { + return false; + } + + let rest = selector; + while (rest.length > 0) { + const match = /^:([a-z-]+)(\()?/.exec(rest); + if (match == null) { + return false; + } + + const name = `:${match[1]}`; + if (match[2] === '(') { + const closeIndex = findCloseParen(rest, name.length); + if (closeIndex === -1) { + return false; + } + const arg = rest.slice(name.length + 1, closeIndex).trim(); + if (arg.length === 0) { + return false; + } + if (SELECTOR_PSEUDO_CLASSES.has(name)) { + if (!isValidPseudoClassSelector(arg)) { + return false; + } + } else if (!NTH_PSEUDO_CLASSES.has(name)) { + return false; + } + rest = rest.slice(closeIndex + 1); + } else { + if (!SIMPLE_PSEUDO_CLASSES.has(name) && !NTH_PSEUDO_CLASSES.has(name)) { + return false; + } + rest = rest.slice(name.length); + } + } + + return true; +} + +const isPseudoClassSelector: RuleCheck = makeVariableCheckingRule( + (node: Node, _vars?: Variables): RuleResponse => { + if ( + node.type === 'Literal' && + typeof node.value === 'string' && + isValidPseudoClassSelector(node.value) + ) { + return undefined; + } + return { message: 'a valid pseudo-class selector' }; + }, +); + +export default isPseudoClassSelector; diff --git a/packages/@stylexjs/eslint-plugin/src/stylex-valid-styles.js b/packages/@stylexjs/eslint-plugin/src/stylex-valid-styles.js index ba1d3de35..66511d72f 100644 --- a/packages/@stylexjs/eslint-plugin/src/stylex-valid-styles.js +++ b/packages/@stylexjs/eslint-plugin/src/stylex-valid-styles.js @@ -49,8 +49,8 @@ import { CSSProperties, CSSPropertyReplacements, pseudoElements, - pseudoClassesAndAtRules, - allModifiers, + pseudoClasses, + atRules, all, } from './reference/cssProperties'; import { splitSpecificShorthands, CANNOT_FIX } from './utils/splitShorthands'; @@ -701,39 +701,52 @@ const stylexValidStyles = { message: 'Keys must be strings', } as Rule.ReportDescriptor); } - if (keyName.startsWith('@') || keyName.startsWith(':')) { + if (keyName.startsWith('::')) { if (level === 0) { - const ruleCheck = ( - allowOuterPseudoAndMedia ? allModifiers : pseudoElements - )(key, variables); - - if (ruleCheck !== undefined) { - if (keyName.startsWith('::')) { - return context.report({ - node: style.value, - loc: style.value.loc, - message: `Unknown pseudo element "${keyName}"`, - } as $ReadOnly); - } + if (pseudoElements(key, variables) !== undefined) { return context.report({ node: style.value, loc: style.value.loc, - message: allowOuterPseudoAndMedia - ? 'Nested styles can only be used for the pseudo selectors in the stylex allowlist and for @media queries' - : 'Pseudo Classes, Media Queries and other At Rules should be nested as conditions within style properties. Only Pseudo Elements (::after) are allowed at the top-level', + message: `Unknown pseudo element "${keyName}"`, } as $ReadOnly); } } else { - const ruleCheck = pseudoClassesAndAtRules(key, variables); + return context.report({ + node: style.value, + loc: style.value.loc, + message: + 'Invalid Pseudo class or At Rule used for conditional style value', + } as $ReadOnly); + } + } else if (keyName.startsWith(':') || keyName.startsWith('@')) { + const modifierRule = keyName.startsWith('@') + ? atRules + : pseudoClasses; - if (ruleCheck !== undefined) { + if (level === 0) { + if (!allowOuterPseudoAndMedia) { return context.report({ node: style.value, loc: style.value.loc, message: - 'Invalid Pseudo class or At Rule used for conditional style value', + 'Pseudo Classes, Media Queries and other At Rules should be nested as conditions within style properties. Only Pseudo Elements (::after) are allowed at the top-level', } as $ReadOnly); } + if (modifierRule(key, variables) !== undefined) { + return context.report({ + node: style.value, + loc: style.value.loc, + message: + 'Nested styles can only be used for the pseudo selectors in the stylex allowlist and for @media queries', + } as $ReadOnly); + } + } else if (modifierRule(key, variables) !== undefined) { + return context.report({ + node: style.value, + loc: style.value.loc, + message: + 'Invalid Pseudo class or At Rule used for conditional style value', + } as $ReadOnly); } }