11import type { LintContext , HyperframeLintFinding } from "../context" ;
22import postcss from "postcss" ;
3+ import selectorParser from "postcss-selector-parser" ;
34import {
45 readAttr ,
56 readDecodedAttr ,
@@ -25,6 +26,87 @@ function selectorTargetsCompositionId(selector: string, compositionId: string):
2526 ) . test ( selector ) ;
2627}
2728
29+ function repeatedDescendantId ( selector : string ) : string | null {
30+ let repeated : string | null = null ;
31+
32+ const requiredPseudoIds = ( pseudo : selectorParser . Pseudo ) : Set < string > => {
33+ if ( ! [ ":is" , ":where" ] . includes ( pseudo . value . toLowerCase ( ) ) || pseudo . nodes . length === 0 ) {
34+ return new Set < string > ( ) ;
35+ }
36+
37+ const optionIdSets : Set < string > [ ] = [ ] ;
38+ for ( const option of pseudo . nodes ) {
39+ // Only promote ids from a single compound. For selector-list branches with
40+ // combinators, determining which compound is the subject requires fuller
41+ // selector semantics; skipping them avoids false positives.
42+ if ( option . nodes . some ( ( node ) => node . type === "combinator" ) ) return new Set < string > ( ) ;
43+ const optionIds = new Set < string > (
44+ option . nodes . filter ( ( node ) => node . type === "id" ) . map ( ( node ) => node . value ) ,
45+ ) ;
46+ optionIdSets . push ( optionIds ) ;
47+ }
48+ const [ firstOptionIds , ...remainingOptionIds ] = optionIdSets ;
49+ return new Set < string > (
50+ [ ...( firstOptionIds ?? [ ] ) ] . filter ( ( id ) =>
51+ remainingOptionIds . every ( ( optionIds ) => optionIds . has ( id ) ) ,
52+ ) ,
53+ ) ;
54+ } ;
55+
56+ try {
57+ selectorParser ( ( root ) => {
58+ root . each ( ( selectorNode ) => {
59+ const firstCompoundById = new Map < string , number > ( ) ;
60+ let compound = 0 ;
61+ selectorNode . each ( ( node ) => {
62+ if ( repeated ) return ;
63+ if ( node . type === "combinator" ) {
64+ compound += 1 ;
65+ return ;
66+ }
67+ const requiredIds =
68+ node . type === "id"
69+ ? [ node . value ]
70+ : node . type === "pseudo"
71+ ? [ ...requiredPseudoIds ( node ) ]
72+ : [ ] ;
73+ for ( const id of requiredIds ) {
74+ const firstCompound = firstCompoundById . get ( id ) ;
75+ if ( firstCompound !== undefined && firstCompound !== compound ) {
76+ repeated = id ;
77+ return ;
78+ }
79+ firstCompoundById . set ( id , compound ) ;
80+ }
81+ } ) ;
82+ } ) ;
83+ } ) . processSync ( selector ) ;
84+ } catch {
85+ return null ;
86+ }
87+ return repeated ;
88+ }
89+
90+ function resolvedRuleSelectors ( rule : postcss . Rule ) : string [ ] {
91+ let ancestor : postcss . AnyNode | undefined = rule . parent ;
92+ while ( ancestor && ancestor . type !== "rule" ) ancestor = ancestor . parent ;
93+ if ( ! ancestor || ancestor . type !== "rule" ) return rule . selectors ;
94+
95+ const parentSelectors = resolvedRuleSelectors ( ancestor ) ;
96+ return parentSelectors . flatMap ( ( parentSelector ) =>
97+ rule . selectors . map ( ( childSelector ) => {
98+ const nestingToken = / ( ^ | [ \s > + ~ , ( ] ) & / g;
99+ if ( nestingToken . test ( childSelector ) ) {
100+ return childSelector . replace (
101+ nestingToken ,
102+ ( _ , separator : string ) => separator + parentSelector ,
103+ ) ;
104+ }
105+ return `${ parentSelector } ${ childSelector } ` ;
106+ } ) ,
107+ ) ;
108+ }
109+
28110function isStudioTimelineElement ( tag : { raw : string ; name : string } ) : boolean {
29111 if ( [ "script" , "style" , "link" , "meta" , "template" , "noscript" ] . includes ( tag . name ) ) {
30112 return false ;
@@ -321,6 +403,35 @@ export const coreRules: Array<(ctx: LintContext) => HyperframeLintFinding[]> = [
321403 return findings ;
322404 } ,
323405
406+ // repeated_id_descendant_selector
407+ ( { styles } ) => {
408+ const findings : HyperframeLintFinding [ ] = [ ] ;
409+ const reported = new Set < string > ( ) ;
410+ for ( const style of styles ) {
411+ let root : postcss . Root ;
412+ try {
413+ root = postcss . parse ( style . content ) ;
414+ } catch {
415+ continue ;
416+ }
417+ root . walkRules ( ( rule ) => {
418+ for ( const selector of resolvedRuleSelectors ( rule ) ) {
419+ const repeatedId = repeatedDescendantId ( selector ) ;
420+ if ( ! repeatedId || reported . has ( repeatedId ) ) continue ;
421+ reported . add ( repeatedId ) ;
422+ findings . push ( {
423+ code : "repeated_id_descendant_selector" ,
424+ severity : "error" ,
425+ message : `Selector "${ selector } " requires #${ repeatedId } to be nested inside another #${ repeatedId } . IDs must be unique, so this selector cannot match a valid composition.` ,
426+ selector,
427+ fixHint : `Remove the duplicate ancestor: change \`#${ repeatedId } #${ repeatedId } \` to \`#${ repeatedId } \`.` ,
428+ } ) ;
429+ }
430+ } ) ;
431+ }
432+ return findings ;
433+ } ,
434+
324435 // invalid_inline_script_syntax (malformed close tag)
325436 ( { source } ) => {
326437 if ( ! INVALID_SCRIPT_CLOSE_PATTERN . test ( source ) ) return [ ] ;
0 commit comments