@@ -43,6 +43,7 @@ import {
4343 WINDOW_TIMELINE_ASSIGN_PATTERN ,
4444 TIMELINE_REGISTRY_OBJECT_LITERAL_PATTERN ,
4545} from "../utils" ;
46+ import { collectAllDeclaredVariableIds } from "./composition" ;
4647
4748// ── GSAP-specific types ────────────────────────────────────────────────────
4849
@@ -195,6 +196,44 @@ function zeroValue(value: string | number | undefined): boolean {
195196 return Number ( value . trim ( ) ) === 0 ;
196197}
197198
199+ function isGsapColorProperty ( property : string ) : boolean {
200+ const normalized = property . toLowerCase ( ) ;
201+ return (
202+ ! normalized . startsWith ( "--" ) &&
203+ ( normalized . endsWith ( "color" ) || normalized === "fill" || normalized === "stroke" )
204+ ) ;
205+ }
206+
207+ function collectStaticCssVariableDefinitions (
208+ tags : readonly OpenTag [ ] ,
209+ styles : LintContext [ "styles" ] ,
210+ ) : Set < string > {
211+ const definitions = new Set < string > ( ) ;
212+ const sources = [
213+ ...styles . map ( ( style ) => style . content ) ,
214+ ...tags . map ( ( tag ) => readDecodedAttr ( tag . raw , "style" ) ?? "" ) ,
215+ ] ;
216+ const definitionPattern = / (?: ^ | [ ; { ] ) \s * ( - - [ A - Z a - z 0 - 9 _ - ] + ) \s * : / gm;
217+ for ( const source of sources ) {
218+ const withoutComments = source . replace ( / \/ \* [ \s \S ] * ?\* \/ / g, " " ) ;
219+ for ( const match of withoutComments . matchAll ( definitionPattern ) ) {
220+ if ( match [ 1 ] ) definitions . add ( match [ 1 ] ) ;
221+ }
222+ }
223+ for ( const id of collectAllDeclaredVariableIds ( tags ) ?? [ ] ) definitions . add ( `--${ id } ` ) ;
224+ return definitions ;
225+ }
226+
227+ function cssVariableReferencesWithoutFallback ( value : unknown ) : string [ ] {
228+ const text = unwrapRaw ( value ) ;
229+ if ( typeof text !== "string" ) return [ ] ;
230+ const variables : string [ ] = [ ] ;
231+ for ( const match of text . matchAll ( / v a r \( \s * ( - - [ A - Z a - z 0 - 9 _ - ] + ) \s * ( , ) ? / g) ) {
232+ if ( match [ 1 ] && ! match [ 2 ] ) variables . push ( match [ 1 ] ) ;
233+ }
234+ return variables ;
235+ }
236+
198237function isHiddenGsapState ( values : Record < string , string | number > ) : boolean {
199238 const visibility = stringValue ( values . visibility ) ?. toLowerCase ( ) ;
200239 const display = stringValue ( values . display ) ?. toLowerCase ( ) ;
@@ -1056,6 +1095,37 @@ function collectCssOpacityZeroSelectors(
10561095
10571096// fallow-ignore-next-line complexity
10581097export const gsapRules : LintRule < LintContext > [ ] = [
1098+ // gsap_undefined_css_variable
1099+ async ( { tags, styles, scripts } ) => {
1100+ const definedVariables = collectStaticCssVariableDefinitions ( tags , styles ) ;
1101+ const findings : HyperframeLintFinding [ ] = [ ] ;
1102+ const reported = new Set < string > ( ) ;
1103+ for ( const script of scripts ) {
1104+ for ( const win of await cachedExtractGsapWindows ( script . content ) ) {
1105+ for ( const values of [ win . fromPropertyValues , win . propertyValues ] ) {
1106+ for ( const [ property , value ] of Object . entries ( values ?? { } ) ) {
1107+ if ( ! isGsapColorProperty ( property ) ) continue ;
1108+ for ( const variable of cssVariableReferencesWithoutFallback ( value ) ) {
1109+ if ( definedVariables . has ( variable ) ) continue ;
1110+ const key = `${ win . targetSelector } |${ property } |${ variable } ` ;
1111+ if ( reported . has ( key ) ) continue ;
1112+ reported . add ( key ) ;
1113+ findings . push ( {
1114+ code : "gsap_undefined_css_variable" ,
1115+ severity : "warning" ,
1116+ message : `GSAP ${ property } on "${ win . targetSelector } " uses ${ variable } , but no static CSS or composition-variable declaration defines it. The computed color may become invalid or transparent.` ,
1117+ selector : win . targetSelector ,
1118+ fixHint : `Define ${ variable } in applicable CSS, declare "${ variable . slice ( 2 ) } " in data-composition-variables, or add a var() fallback such as var(${ variable } , #fff).` ,
1119+ snippet : truncateSnippet ( win . raw ) ,
1120+ } ) ;
1121+ }
1122+ }
1123+ }
1124+ }
1125+ }
1126+ return findings ;
1127+ } ,
1128+
10591129 // overlapping_gsap_tweens + gsap_animates_clip_element
10601130 // fallow-ignore-next-line complexity
10611131 async ( { source, tags, scripts, styles, rootCompositionId } ) => {
0 commit comments