import { API , FileInfo , Options , JSXIdentifier } from 'jscodeshift' ; import { getImportRenameMap } from './getImportRenameMap' ; const inputsMap = { '@workday/canvas-kit-react/checkbox' : [ 'Checkbox' ] , '@workday/canvas-kit-react/color-picker' : [ 'ColorInput' , 'ColorPreview' ] , '@workday/canvas-kit-react/radio' : [ 'Radio' ] , '@workday/canvas-kit-react/switch' : [ 'Switch' ] , '@workday/canvas-kit-react/text-area' : [ 'TextArea' ] , '@workday/canvas-kit-react/text-input' : [ 'TextInput' ] , } ; export default function transformer ( file , api , options ) { const j = api . jscodeshift ; const root = j ( file . source ) ; const defaultImports = [ ] ; root . find ( j . ImportDefaultSpecifier ) . forEach ( nodePath => { const packageName = nodePath . parent . node . source . value ; const localName = nodePath . value . local ?. name ; if ( packageName in inputsMap && localName ) { defaultImports . push ( localName ) ; } } ) ; let runningSource = file . source ; for ( const inputPackageName of Object . keys ( inputsMap ) ) { const currentRoot = j ( runningSource ) ; const { containsCanvasImports , importMap , styledMap } = getImportRenameMap ( j , currentRoot , inputPackageName ) ; if ( containsCanvasImports ) { const inputNames = inputsMap [ inputPackageName ] ; currentRoot . find ( j . JSXIdentifier , ( value ) => value . name === 'inputRef' ) . replaceWith ( nodePath => { const elementName = nodePath . parent ?. parent ?. value ?. name ?. name ; if ( defaultImports . includes ( elementName ) || inputNames . map ( n => importMap [ n ] ) . includes ( elementName ) || inputNames . map ( n => styledMap [ n ] ) . includes ( elementName ) ) { return j . jsxIdentifier ( 'ref' ) ; } return nodePath . value ; } ) ; runningSource = currentRoot . toSource ( ) ; } } return runningSource ; }