import { ConditionalExpression , Expression , FunctionDeclaration , Printable , UnaryExpression , } from 'ast-types' ; import { Collection , JsCodeShift } from 'jscodeshift' ; import { CodeModExports } from '../codeModTypes' ; import { negateExpression } from '../utils/astHelpers' ; const codeMod = ( ( fileInfo , api , options ) => { const j = api . jscodeshift ; const ast = fileInfo . ast ; const target = options . target ; const path = target . thisOrClosest ( j . ConditionalExpression ) . firstPath ( ) ! ; if ( j . BooleanLiteral . check ( path . node . test ) ) { const testValue = path . node . test . value ; let replacement ; if ( testValue ) { replacement = path . node . consequent ; } else { replacement = path . node . alternate ; } path . replace ( replacement ) ; } else if ( j . BooleanLiteral . check ( path . node . consequent ) && j . BooleanLiteral . check ( path . node . alternate ) ) { const conValue = path . node . consequent . value ; const altValue = path . node . alternate . value ; let replacement ; if ( conValue ) { if ( altValue ) { replacement = j . booleanLiteral ( true ) ; } else { replacement = j . unaryExpression ( '!' , j . unaryExpression ( '!' , path . node . test ) ) ; } } else { if ( altValue ) { replacement = j . unaryExpression ( '!' , path . node . test ) ; } else { replacement = j . booleanLiteral ( false ) ; } } path . replace ( replacement ) ; } else { const replacement = j . logicalExpression ( '||' , path . node . test , path . node . alternate ) ; path . replace ( replacement ) ; } 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 ; const node = target . thisOrClosest ( j . ConditionalExpression ) . firstNode ( ) ; if ( ! j . ConditionalExpression . check ( node ) ) { return false ; } const constantTest = j . BooleanLiteral . check ( node . test ) ; const booleanBranches = j . BooleanLiteral . check ( node . consequent ) && j . BooleanLiteral . check ( node . alternate ) ; const testStr = fileInfo . source . substring ( node . test . start , node . test . end ) . trim ( ) ; const conStr = fileInfo . source . substring ( node . consequent . start , node . consequent . end ) . trim ( ) ; const sameTestCon = testStr === conStr ; return constantTest || booleanBranches || sameTestCon ; } ; codeMod . scope = 'cursor' ; codeMod . title = 'Simplify ?:' ; codeMod . description = '' ; codeMod . detail = '' ; module . exports = codeMod ;