File size: 8,828 Bytes
fea495a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | import {
AnyClassGroupIds,
AnyConfig,
AnyThemeGroupIds,
ClassGroup,
ClassValidator,
Config,
ThemeGetter,
ThemeObject,
} from './types'
import { concatArrays } from './utils'
export interface ClassPartObject {
nextPart: Map<string, ClassPartObject>
validators: ClassValidatorObject[] | null
classGroupId: AnyClassGroupIds | undefined // Always define optional props for consistent shape
}
interface ClassValidatorObject {
classGroupId: AnyClassGroupIds
validator: ClassValidator
}
// Factory function ensures consistent object shapes
const createClassValidatorObject = (
classGroupId: AnyClassGroupIds,
validator: ClassValidator,
): ClassValidatorObject => ({
classGroupId,
validator,
})
// Factory ensures consistent ClassPartObject shape
const createClassPartObject = (
nextPart: Map<string, ClassPartObject> = new Map(),
validators: ClassValidatorObject[] | null = null,
classGroupId?: AnyClassGroupIds,
): ClassPartObject => ({
nextPart,
validators,
classGroupId,
})
const CLASS_PART_SEPARATOR = '-'
const EMPTY_CONFLICTS: readonly AnyClassGroupIds[] = []
// I use two dots here because one dot is used as prefix for class groups in plugins
const ARBITRARY_PROPERTY_PREFIX = 'arbitrary..'
export const createClassGroupUtils = (config: AnyConfig) => {
const classMap = createClassMap(config)
const { conflictingClassGroups, conflictingClassGroupModifiers } = config
const getClassGroupId = (className: string) => {
if (className.startsWith('[') && className.endsWith(']')) {
return getGroupIdForArbitraryProperty(className)
}
const classParts = className.split(CLASS_PART_SEPARATOR)
// Classes like `-inset-1` produce an empty string as first classPart. We assume that classes for negative values are used correctly and skip it.
const startIndex = classParts[0] === '' && classParts.length > 1 ? 1 : 0
return getGroupRecursive(classParts, startIndex, classMap)
}
const getConflictingClassGroupIds = (
classGroupId: AnyClassGroupIds,
hasPostfixModifier: boolean,
): readonly AnyClassGroupIds[] => {
if (hasPostfixModifier) {
const modifierConflicts = conflictingClassGroupModifiers[classGroupId]
const baseConflicts = conflictingClassGroups[classGroupId]
if (modifierConflicts) {
if (baseConflicts) {
// Merge base conflicts with modifier conflicts
return concatArrays(baseConflicts, modifierConflicts)
}
// Only modifier conflicts
return modifierConflicts
}
// Fall back to without postfix if no modifier conflicts
return baseConflicts || EMPTY_CONFLICTS
}
return conflictingClassGroups[classGroupId] || EMPTY_CONFLICTS
}
return {
getClassGroupId,
getConflictingClassGroupIds,
}
}
const getGroupRecursive = (
classParts: string[],
startIndex: number,
classPartObject: ClassPartObject,
): AnyClassGroupIds | undefined => {
const classPathsLength = classParts.length - startIndex
if (classPathsLength === 0) {
return classPartObject.classGroupId
}
const currentClassPart = classParts[startIndex]!
const nextClassPartObject = classPartObject.nextPart.get(currentClassPart)
if (nextClassPartObject) {
const result = getGroupRecursive(classParts, startIndex + 1, nextClassPartObject)
if (result) return result
}
const validators = classPartObject.validators
if (validators === null) {
return undefined
}
// Build classRest string efficiently by joining from startIndex onwards
const classRest =
startIndex === 0
? classParts.join(CLASS_PART_SEPARATOR)
: classParts.slice(startIndex).join(CLASS_PART_SEPARATOR)
const validatorsLength = validators.length
for (let i = 0; i < validatorsLength; i++) {
const validatorObj = validators[i]!
if (validatorObj.validator(classRest)) {
return validatorObj.classGroupId
}
}
return undefined
}
/**
* Get the class group ID for an arbitrary property.
*
* @param className - The class name to get the group ID for. Is expected to be string starting with `[` and ending with `]`.
*/
const getGroupIdForArbitraryProperty = (className: string): AnyClassGroupIds | undefined =>
className.slice(1, -1).indexOf(':') === -1
? undefined
: (() => {
const content = className.slice(1, -1)
const colonIndex = content.indexOf(':')
const property = content.slice(0, colonIndex)
return property ? ARBITRARY_PROPERTY_PREFIX + property : undefined
})()
/**
* Exported for testing only
*/
export const createClassMap = (config: Config<AnyClassGroupIds, AnyThemeGroupIds>) => {
const { theme, classGroups } = config
return processClassGroups(classGroups, theme)
}
// Split into separate functions to maintain monomorphic call sites
const processClassGroups = (
classGroups: Record<AnyClassGroupIds, ClassGroup<AnyThemeGroupIds>>,
theme: ThemeObject<AnyThemeGroupIds>,
): ClassPartObject => {
const classMap = createClassPartObject()
for (const classGroupId in classGroups) {
const group = classGroups[classGroupId]!
processClassesRecursively(group, classMap, classGroupId, theme)
}
return classMap
}
const processClassesRecursively = (
classGroup: ClassGroup<AnyThemeGroupIds>,
classPartObject: ClassPartObject,
classGroupId: AnyClassGroupIds,
theme: ThemeObject<AnyThemeGroupIds>,
) => {
const len = classGroup.length
for (let i = 0; i < len; i++) {
const classDefinition = classGroup[i]!
processClassDefinition(classDefinition, classPartObject, classGroupId, theme)
}
}
// Split into separate functions for each type to maintain monomorphic call sites
const processClassDefinition = (
classDefinition: ClassGroup<AnyThemeGroupIds>[number],
classPartObject: ClassPartObject,
classGroupId: AnyClassGroupIds,
theme: ThemeObject<AnyThemeGroupIds>,
) => {
if (typeof classDefinition === 'string') {
processStringDefinition(classDefinition, classPartObject, classGroupId)
return
}
if (typeof classDefinition === 'function') {
processFunctionDefinition(classDefinition, classPartObject, classGroupId, theme)
return
}
processObjectDefinition(
classDefinition as Record<string, ClassGroup<AnyThemeGroupIds>>,
classPartObject,
classGroupId,
theme,
)
}
const processStringDefinition = (
classDefinition: string,
classPartObject: ClassPartObject,
classGroupId: AnyClassGroupIds,
) => {
const classPartObjectToEdit =
classDefinition === '' ? classPartObject : getPart(classPartObject, classDefinition)
classPartObjectToEdit.classGroupId = classGroupId
}
const processFunctionDefinition = (
classDefinition: Function,
classPartObject: ClassPartObject,
classGroupId: AnyClassGroupIds,
theme: ThemeObject<AnyThemeGroupIds>,
) => {
if (isThemeGetter(classDefinition)) {
processClassesRecursively(classDefinition(theme), classPartObject, classGroupId, theme)
return
}
if (classPartObject.validators === null) {
classPartObject.validators = []
}
classPartObject.validators.push(
createClassValidatorObject(classGroupId, classDefinition as ClassValidator),
)
}
const processObjectDefinition = (
classDefinition: Record<string, ClassGroup<AnyThemeGroupIds>>,
classPartObject: ClassPartObject,
classGroupId: AnyClassGroupIds,
theme: ThemeObject<AnyThemeGroupIds>,
) => {
const entries = Object.entries(classDefinition)
const len = entries.length
for (let i = 0; i < len; i++) {
const [key, value] = entries[i]!
processClassesRecursively(value, getPart(classPartObject, key), classGroupId, theme)
}
}
const getPart = (classPartObject: ClassPartObject, path: string): ClassPartObject => {
let current = classPartObject
const parts = path.split(CLASS_PART_SEPARATOR)
const len = parts.length
for (let i = 0; i < len; i++) {
const part = parts[i]!
let next = current.nextPart.get(part)
if (!next) {
next = createClassPartObject()
current.nextPart.set(part, next)
}
current = next
}
return current
}
// Type guard maintains monomorphic check
const isThemeGetter = (func: Function): func is ThemeGetter =>
'isThemeGetter' in func && (func as ThemeGetter).isThemeGetter === true
|