import { basename, dirname, extname, join } from "node:path"; import type b from "@babel/core"; import hash from "@emotion/hash"; import { isPlainObject } from "lodash"; import invariant from "tiny-invariant"; import { type NodePath, type types as t } from "@babel/core"; import { type SourceLocation, type StyleMapEntry } from "../shared"; import { type ResolveTailwindOptions, getClassName } from "../index"; import { handleMacro } from "./macro"; export function evaluateArgs(path: NodePath) { const { confident, value } = path.evaluate(); invariant(confident, "Argument cannot be statically evaluated"); if (typeof value === "string") { return trim(value); } if (isPlainObject(value)) { return flatMapEntries(value, (classes, modifier) => { if (modifier === "data" && isPlainObject(classes)) { return flatMapEntries(classes as Record, (cls, key) => typeof cls === "string" ? trimPrefix(cls, `${modifier}-[${key}]:`) : flatMapEntries(cls as Record, (cls, attrValue) => trimPrefix(cls, `${modifier}-[${key}=${attrValue}]:`) ) ); } invariant( typeof classes === "string", `Value for "${modifier}" should be a string` ); return trimPrefix(classes, modifier + ":"); }); } throw new Error("Invalid argument type"); } export const trim = (value: string) => value.replace(/\s+/g, " ").trim().split(" ").filter(Boolean); export const trimPrefix = (cls: string, prefix = "") => trim(cls).map(value => prefix + value); const flatMapEntries = ( map: Record, fn: (value: V, key: K) => R[] ): R[] => Object.entries(map).flatMap(([key, value]) => fn(value as V, key as K));