11import { segment } from "./utils/segment" ;
22import { findRoot } from "./find-root" ;
3- import { functionalPlugins , namedPlugins , type Variant } from "./plugins" ;
3+ import { functionalPlugins , type Modifier , namedPlugins , type Variant } from "./plugins" ;
44import { parseVariant } from "./parse-variant" ;
55import { inferDataType } from "./utils/infer-data-type.ts" ;
6- import { getValue } from "./utils/value.ts" ;
6+ import { getValue , type Value } from "./utils/value.ts" ;
77import { PluginNotFoundException } from "./exceptions/plugin-not-found-exception.ts" ;
88import { decodeArbitraryValue } from "./utils/decodeArbitraryValue.ts" ;
99import type { CustomThemeConfig , ScreensConfig } from "tailwindcss/types/config" ;
@@ -15,7 +15,19 @@ export type State = {
1515 negative : boolean
1616}
1717
18- export const parse = ( input : string , config ?: CustomThemeConfig ) => {
18+ export type AST = {
19+ root : string
20+ kind : "named" | "functional"
21+ property : string
22+ value : Value
23+ variants : Variant [ ]
24+ modifiers : Modifier [ ] ,
25+ important : boolean
26+ negative : boolean ,
27+ arbitrary : boolean
28+ }
29+
30+ export const parse = ( input : string , config ?: CustomThemeConfig ) : AST => {
1931 const theme = getTailwindTheme ( config )
2032 let state : State = {
2133 important : false ,
@@ -28,8 +40,8 @@ export const parse = (input: string, config?: CustomThemeConfig) => {
2840
2941 for ( let i = variants . length - 1 ; i >= 0 ; -- i ) {
3042 let parsedVariant = parseVariant ( variants [ i ] , theme . screens as ScreensConfig )
31- if ( parsedVariant === null ) return null
32- parsedCandidateVariants . push ( parsedVariant )
43+ if ( parsedVariant !== null )
44+ parsedCandidateVariants . push ( parsedVariant )
3345 }
3446
3547 if ( base [ 0 ] === '!' ) {
@@ -46,16 +58,19 @@ export const parse = (input: string, config?: CustomThemeConfig) => {
4658 if ( namedPlugin ) {
4759 return {
4860 root : base ,
49- kind : "static " ,
61+ kind : "named " ,
5062 property : namedPlugin ! . ns ,
5163 value : {
5264 class : namedPlugin . class ,
5365 raw : base ,
66+ kind : "named" ,
5467 value : namedPlugin . value ,
5568 } ,
56- modifier : parsedCandidateVariants ,
69+ variants : parsedCandidateVariants ,
70+ modifiers : [ ] ,
5771 important : state . important ,
58- negative : state . negative
72+ negative : state . negative ,
73+ arbitrary : false
5974 }
6075 }
6176
@@ -77,14 +92,16 @@ export const parse = (input: string, config?: CustomThemeConfig) => {
7792
7893 return {
7994 root : root ,
95+ kind : "functional" ,
8096 property : associatedPluginByType ! . ns ,
8197 value : {
8298 value : arbitraryValue ,
8399 class : associatedPluginByType ! . class ,
84100 raw : value ,
85- type : unitType
101+ kind : unitType || "named"
86102 } ,
87- modifier : parsedCandidateVariants ,
103+ variants : parsedCandidateVariants ,
104+ modifiers : [ ] ,
88105 arbitrary : true ,
89106 important : state . important ,
90107 negative : state . negative
@@ -95,27 +112,26 @@ export const parse = (input: string, config?: CustomThemeConfig) => {
95112 let isValueColor = isColor ( value , theme )
96113
97114 //we need to remove modifier from value
115+ const modifiers : Modifier [ ] = [ ]
98116 if ( value && isValueColor ) {
99117 let [ valueWithoutModifier , modifierSegment = null ] = segment ( value , '/' )
100118 value = valueWithoutModifier
101119 if ( modifierSegment ) {
102120 if ( modifierSegment [ 0 ] === '[' && modifierSegment [ modifierSegment . length - 1 ] === ']' ) {
103- parsedCandidateVariants . push ( {
121+ modifiers . push ( {
104122 kind : 'arbitrary' ,
105- type : "opacity" ,
106123 value : decodeArbitraryValue ( modifierSegment . slice ( 1 , - 1 ) ) ,
107124 } )
108125 } else {
109- parsedCandidateVariants . push ( {
126+ modifiers . push ( {
110127 kind : 'named' ,
111- type : "opacity" ,
112128 value : modifierSegment ,
113129 } )
114130 }
115131 }
116132 }
117133
118- if ( ! value ) {
134+ if ( ! value ) {
119135 value = 'DEFAULT'
120136 }
121137 //check value against each scale of available plugins
@@ -126,10 +142,13 @@ export const parse = (input: string, config?: CustomThemeConfig) => {
126142
127143 return {
128144 root : root ,
145+ kind : "functional" ,
129146 property : matchedPlugin . ns ,
130147 value : getValue ( value , matchedPlugin , theme [ matchedPlugin . scaleKey ] ) ,
131- modifier : parsedCandidateVariants ,
148+ variants : parsedCandidateVariants ,
149+ modifiers : modifiers ,
132150 important : state . important ,
133- negative : state . negative
151+ negative : state . negative ,
152+ arbitrary : false ,
134153 }
135154}
0 commit comments