import * as t from '@babel/types' ; import Store from '@remax/build-store' ; import { TEMPLATE_ID , REACT_KEY , LEAF , ENTRY } from '../constants' ; param { string } attributeName param { string } dataPath param { ( t . StringLiteral | t . JSXExpressionContainer ) } value returns function createAttributeValueTemplate ( attributeName , dataPath , value ? ) { let template = '' ; if ( t . isLiteral ( value ) ) { template = value . value ; } if ( t . isJSXExpressionContainer ( value ) ) { if ( t . isLiteral ( value . expression ) ) { template = ( value . expression as t . StringLiteral ) . value ; } else { template = `{{${ dataPath }.props['${ attributeName }']}}` ; } } if ( value === null || value === undefined ) { template = `{{${ dataPath }.props['${ attributeName }']}}` ; } return `"${ template }"` ; } export param { string } componentType param { string } dataPath param { ( Array < t . JSXAttribute | t . JSXSpreadAttribute > ) } attributes returns export function createAttributesTemplate ( componentType , dataPath , attributes ) { const SEPARATOR = '\n  ' ; const hostComponent = Store . registeredHostComponents . get ( componentType ) ; const template = ( attributes as t . JSXAttribute [ ] ) . filter ( attr => ! [ TEMPLATE_ID , REACT_KEY , LEAF , ENTRY ] . find ( k => k === attr . name . name ) ) . map ( attr => { const name = attr . name ; let attrName = '' ; if ( t . isJSXIdentifier ( name ) ) { attrName = name . name ; } if ( t . isJSXNamespacedName ( name ) ) { attrName = name . namespace . name + ':' + name . name . name ; } const prop = hostComponent ?. alias ?. [ attrName ] ?? attrName ; return [ prop , createAttributeValueTemplate ( prop , dataPath , attr . value as any ) ] ; } ) ; return template . map ( ( [ prop , value ] ) => `${ prop }=${ value }` ) . join ( SEPARATOR ) ; }