11import { defineRule } from "@oxlint/plugins" ;
22
33const COMBINATOR_PATTERN = / [ \s > + ~ ] / u;
4- // A class, id, attribute, or tag at the start of the compound narrows it.
4+ // A class, id, attribute, or leading tag narrows a compound. Negated ones don't,
5+ // so `:not(...)` is removed before this check.
56const NARROWING_PATTERN = / [ . # [ ] | ^ [ a - z ] / iu;
67const ROOT_COMPOUND_PATTERN = / ^ (?: h t m l | b o d y | : r o o t ) (? ! [ \w - ] ) / iu;
78
8- /** Top-level `[...]` groups in a class token, i.e. Tailwind arbitrary variants and values . */
9- function bracketGroups ( token : string ) : string [ ] {
9+ /** Tailwind arbitrary variants in a class token: top-level `[...]` groups followed by `:` . */
10+ function variantGroups ( token : string ) : string [ ] {
1011 const groups : string [ ] = [ ] ;
1112 let depth = 0 ;
1213 let start = - 1 ;
@@ -17,7 +18,7 @@ function bracketGroups(token: string): string[] {
1718 depth ++ ;
1819 } else if ( char === "]" && depth > 0 ) {
1920 depth -- ;
20- if ( depth === 0 ) groups . push ( token . slice ( start , index ) ) ;
21+ if ( depth === 0 && token [ index + 1 ] === ":" ) groups . push ( token . slice ( start , index ) ) ;
2122 }
2223 }
2324 return groups ;
@@ -45,18 +46,39 @@ function hasCompounds(selector: string): string[] {
4546 return compounds ;
4647}
4748
49+ /** `compound` without any `:not(...)`, including one left open around the `:has()`. */
50+ function withoutNegations ( compound : string ) : string {
51+ let result = "" ;
52+ let index = 0 ;
53+ while ( index < compound . length ) {
54+ if ( ! compound . startsWith ( ":not(" , index ) ) {
55+ result += compound [ index ] ;
56+ index ++ ;
57+ continue ;
58+ }
59+ let depth = 0 ;
60+ for ( index += ":not" . length ; index < compound . length ; index ++ ) {
61+ if ( compound [ index ] === "(" ) depth ++ ;
62+ else if ( compound [ index ] === ")" && -- depth === 0 ) break ;
63+ }
64+ index ++ ;
65+ }
66+ return result ;
67+ }
68+
4869/** Arbitrary variants in `text` whose `:has()` is unanchored or anchored to the document root. */
4970function findUnscopedHasVariants ( text : string ) : string [ ] {
50- if ( ! text . includes ( ":has(" ) ) return [ ] ;
71+ // Selectors are ASCII case-insensitive.
72+ if ( ! text . toLowerCase ( ) . includes ( ":has(" ) ) return [ ] ;
5173 const offenders : string [ ] = [ ] ;
5274 for ( const token of text . split ( / \s + / u) ) {
53- for ( const group of bracketGroups ( token ) ) {
54- if ( ! group . includes ( ":has(" ) ) continue ;
75+ for ( const group of variantGroups ( token ) ) {
5576 // Tailwind writes spaces as "_", and "&" is the element carrying the class.
56- const selector = group . replaceAll ( "_" , " " ) . replaceAll ( "&" , ".self" ) ;
57- const unscoped = hasCompounds ( selector ) . some (
58- ( compound ) => ! NARROWING_PATTERN . test ( compound ) || ROOT_COMPOUND_PATTERN . test ( compound ) ,
59- ) ;
77+ const selector = group . toLowerCase ( ) . replaceAll ( "_" , " " ) . replaceAll ( "&" , ".self" ) ;
78+ const unscoped = hasCompounds ( selector ) . some ( ( compound ) => {
79+ const anchor = withoutNegations ( compound ) ;
80+ return ! NARROWING_PATTERN . test ( anchor ) || ROOT_COMPOUND_PATTERN . test ( anchor ) ;
81+ } ) ;
6082 if ( unscoped ) offenders . push ( `[${ group } ]` ) ;
6183 }
6284 }
0 commit comments