import { ExpressionStatement } from 'ast-types' ; import { CodeModExports } from '../codeModTypes' ; const codeMod = ( ( fileInfo , api , options ) => { const j = api . jscodeshift ; const ast = fileInfo . ast ; const target = options . target ; const path = target . thisOrClosest ( j . VariableDeclaration ) . firstPath ( ) ! ; if ( path . node . kind === 'const' ) { path . node . kind = 'let' ; } const assignments = [ ] ; path . node . declarations . forEach ( ( d ) => { if ( j . VariableDeclarator . check ( d ) && d . init ) { let id = d . id ; if ( j . Identifier . check ( id ) ) { id = j . identifier ( id . name ) ; } assignments . push ( j . expressionStatement ( j . assignmentExpression ( '=' , id , d . init ) ) ) ; d . init = null ; } } ) ; assignments . reverse ( ) . forEach ( ( a ) => { path . insertAfter ( a ) ; } ) ; const resultText = ast . toSource ( ) ; return resultText ; } ) as CodeModExports ; codeMod . canRun = ( fileInfo , api , options ) => { const j = api . jscodeshift ; const ast = fileInfo . ast ; const target = options . target ; let testPath = target . firstPath ( ) ; while ( testPath && testPath . parent ) { if ( j . VariableDeclarator . check ( testPath . parent . node ) && testPath . parent . node . init === testPath . node ) { return false ; } if ( j . VariableDeclaration . check ( testPath . node ) ) { break ; } testPath = testPath . parent ; } const path = target . thisOrClosest ( j . VariableDeclaration ) . firstPath ( ) ; return Boolean ( path && ! j . ExportNamedDeclaration . check ( path . parent . node ) && path . node . declarations . some ( ( d ) => Boolean ( j . VariableDeclarator . check ( d ) && d . init ) ) ) ; } ; codeMod . scope = 'cursor' ; codeMod . title = 'Split into declaration and initialization' ; codeMod . description = '' ; codeMod . detail = '' ; module . exports = codeMod ;