Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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';
Expand Down
25 changes: 5 additions & 20 deletions packages/@stylexjs/eslint-plugin/src/reference/cssProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 };
115 changes: 115 additions & 0 deletions packages/@stylexjs/eslint-plugin/src/rules/isPseudoClassSelector.js
Original file line number Diff line number Diff line change
@@ -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<string> = new Set([
':first-child',
':last-child',
':only-child',
':empty',
':hover',
':focus',
':focus-visible',
':focus-within',
':active',
':visited',
':disabled',
]);

const NTH_PSEUDO_CLASSES: $ReadOnlySet<string> = new Set([
':nth-child',
':nth-of-type',
]);

const SELECTOR_PSEUDO_CLASSES: $ReadOnlySet<string> = 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;
55 changes: 34 additions & 21 deletions packages/@stylexjs/eslint-plugin/src/stylex-valid-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ import {
CSSProperties,
CSSPropertyReplacements,
pseudoElements,
pseudoClassesAndAtRules,
allModifiers,
pseudoClasses,
atRules,
all,
} from './reference/cssProperties';
import { splitSpecificShorthands, CANNOT_FIX } from './utils/splitShorthands';
Expand Down Expand Up @@ -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<Rule.ReportDescriptor>);
}
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<Rule.ReportDescriptor>);
}
} 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<Rule.ReportDescriptor>);
}
} 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<Rule.ReportDescriptor>);
}
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<Rule.ReportDescriptor>);
}
} 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<Rule.ReportDescriptor>);
}
}

Expand Down
Loading