import * as t from '@babel/types' ; import { ClassNameObject } from '../types' ; export function buildClassName ( classNameObjects ) { return classNameObjects . reduce < t . Expression | null > ( ( acc , val ) => { if ( acc == null ) { if ( t . isConditionalExpression ( val ) || t . isStringLiteral ( val ) || t . isNumericLiteral ( val ) ) { return val ; } return t . logicalExpression ( '||' , val , t . stringLiteral ( '' ) ) ; } let inner ; if ( t . isStringLiteral ( val ) ) { if ( t . isStringLiteral ( acc ) ) { return t . stringLiteral ( `${ acc . value } ${ val . value }` ) ; } inner = t . stringLiteral ( ` ${ val . value }` ) ; } else if ( t . isLiteral ( val ) ) { inner = t . binaryExpression ( '+' , t . stringLiteral ( ' ' ) , val ) ; } else if ( t . isConditionalExpression ( val ) || t . isBinaryExpression ( val ) ) { if ( t . isStringLiteral ( acc ) ) { return t . binaryExpression ( '+' , t . stringLiteral ( `${ acc . value } ` ) , val ) ; } inner = t . binaryExpression ( '+' , t . stringLiteral ( ' ' ) , val ) ; } else if ( t . isIdentifier ( val ) || t . isMemberExpression ( val ) ) { inner = t . conditionalExpression ( val , t . binaryExpression ( '+' , t . stringLiteral ( ' ' ) , val ) , t . stringLiteral ( '' ) ) ; } else { if ( t . isStringLiteral ( acc ) ) { return t . binaryExpression ( '+' , t . stringLiteral ( `${ acc . value } ` ) , t . logicalExpression ( '||' , val , t . stringLiteral ( '' ) ) ) ; } inner = t . binaryExpression ( '+' , t . stringLiteral ( ' ' ) , t . logicalExpression ( '||' , val , t . stringLiteral ( '' ) ) ) ; } return t . binaryExpression ( '+' , acc , inner ) ; } , null ) ; }