import type { NodePath } from '@babel/core' import { types as t } from '@babel/core' import type { Ctx } from 'rntwsc/devtools/babel-plugin-tw/lib/context' import type { ArrWp, Literal, MapWp, WithPath, } from 'rntwsc/devtools/babel-plugin-tw/lib/path-to-js' import { pathToArray, pathToLiteral, pathToObject, pathToObjectString, pathToString, } from 'rntwsc/devtools/babel-plugin-tw/lib/path-to-js' export type ClassNames = MapWp export type Attr = MapWp export type Attrs = MapWp export type Variant = MapWp export type CompoundVariant = Variant & Pick export type Options = { className?: WithPath classNames?: WithPath attributes?: WithPath compoundVariants?: WithPath> } export const pathToCvaOptions = ( ctx: Ctx, path: NodePath, ) => { const argumentsPath = path.get('arguments') if (argumentsPath?.length !== 1) { throw ctx.err(path, 'expect exactly one argument') } const argPath = argumentsPath[0] as NodePath const cva = pathToObject( ctx, argPath, undefined, (k, kPath, innerPath) => { if (k === 'className') { return pathToString(ctx, innerPath, kPath) } if (k === 'classNames') { return pathToObjectString(ctx, innerPath, kPath) } if (k === 'attributes') { return pathToObject( ctx, innerPath, kPath, (k2, kPath2, innerPath2) => pathToObject( ctx, innerPath2, kPath2, (k3, kPath3, innerPath3) => t.isStringLiteral(innerPath3) ? pathToString(ctx, innerPath3, kPath3) : pathToObjectString(ctx, innerPath3, kPath3), ), ) } if (k === 'compoundVariants') { return pathToArray(ctx, innerPath, kPath, (i, innerPath2) => pathToObject( ctx, innerPath2, undefined, (k3, kPath3, innerPath3) => { if (k3 === 'className') { return pathToString(ctx, innerPath3, kPath3) } if (k3 === 'classNames') { return pathToObjectString(ctx, innerPath3, kPath3) } return pathToLiteral(ctx, innerPath3, kPath3) }, ), ) } throw ctx.err(innerPath, `unknown key ${k}`) }, ) return cva.value as Options }