import { API , FileInfo , Options , JSXElement , JSXAttribute , JSXSpreadAttribute } from 'jscodeshift' ; import { getImportRenameMap } from './getImportRenameMap' ; function filterDefined < T > ( input ) { return ! ! input ; } export default function transformer ( file , api , options ) { const j = api . jscodeshift ; const root = j ( file . source ) ; const { containsCanvasImports , importMap , styledMap } = getImportRenameMap ( j , root , '@workday/canvas-kit-react/card' ) ; if ( ! containsCanvasImports ) { return file . source ; } root . find ( j . JSXElement , ( value ) => value . openingElement . name . type === 'JSXIdentifier' && ( value . openingElement . name . name === importMap . Card || value . openingElement . name . name === styledMap . Card ) ) . forEach ( nodePath => { for ( const child of nodePath . value . children || [ ] ) { if ( child . type === 'JSXElement' && child . openingElement . name . type === 'JSXMemberExpression' && child . openingElement . name . object . type === 'JSXIdentifier' && child . openingElement . name . object . name === 'Card' && child . openingElement . name . property . type === 'JSXIdentifier' && child . openingElement . name . property . name === 'Body' ) { return ; } } const findAttribute = ( name ) => ( item ) => { if ( item . type === 'JSXAttribute' && item . name . type === 'JSXIdentifier' && item . name . name === name ) { return true ; } return false ; } ; const attributes = nodePath . value . openingElement . attributes ; if ( attributes ) { const header = attributes . find ( findAttribute ( 'heading' ) ) as JSXAttribute | undefined ; const id = attributes . find ( findAttribute ( 'headingId' ) ) as JSXAttribute | undefined ; const children = nodePath . value . children ; const depth = attributes . find ( findAttribute ( 'depth' ) ) as JSXAttribute | undefined ; nodePath . value . openingElement . attributes = attributes . filter ( item => { if ( item . type === 'JSXAttribute' && item . name . type === 'JSXIdentifier' && [ 'heading' , 'headingId' ] . includes ( item . name . name ) ) { return false ; } return true ; } ) ; if ( depth && depth . value && depth . value . type === 'JSXExpressionContainer' && depth . value . expression . type === 'MemberExpression' && depth . value . expression . object . type === 'Identifier' && depth . value . expression . property . type === 'NumericLiteral' ) { depth . value . expression = depth . value . expression . property ; } const CardHeadingJSX = j . jsxMemberExpression ( j . jsxIdentifier ( importMap . Card ) , j . jsxIdentifier ( 'Heading' ) ) ; const CardBodyJSX = j . jsxMemberExpression ( j . jsxIdentifier ( importMap . Card ) , j . jsxIdentifier ( 'Body' ) ) ; const convertAttributeToChildren = ( input ) => { switch ( input . value ?. type ) { case 'JSXExpressionContainer' : if ( input . value . expression . type === 'JSXElement' ) { return [ input . value . expression ] ; } return [ input . value ] ; default : return [ input . value ! ] ; } } ; nodePath . value . children = [ ! ! header ? j . jsxElement ( j . jsxOpeningElement ( CardHeadingJSX , id ? [ j . jsxAttribute ( j . jsxIdentifier ( 'id' ) , id . value ) ] : [ ] ) , j . jsxClosingElement ( CardHeadingJSX ) , convertAttributeToChildren ( header ) ) : undefined , j . jsxElement ( j . jsxOpeningElement ( CardBodyJSX ) , j . jsxClosingElement ( CardBodyJSX ) , children ) , ] . filter < JSXElement > ( filterDefined ) ; } } ) ; return root . toSource ( ) ; }