import { AstNode , Expression , Node , NodePath , Statement , StringLiteral , UnaryExpression , } from 'ast-types' ; import { Collection , JsCodeShift } from 'jscodeshift' ; export function negateExpression ( j , expr ) { if ( j . match < UnaryExpression > ( expr , { type : 'UnaryExpression' , operator : '!' } ) ) { return expr . argument ; } const operatorMap = { '<' : '>=' , '>' : '<=' , '>=' : '<' , '<=' : '>' , '!=' : '==' , '==' : '!=' , '!==' : '===' , '===' : '!==' , } ; if ( j . BinaryExpression . check ( expr ) && operatorMap [ expr . operator ] ) { expr . operator = operatorMap [ expr . operator ] ; return expr ; } return j . unaryExpression ( '!' , expr ) ; } /**
 * Checks if the asked node is a StringLiteral AND also checks
 * if it's a proper string inside a JS expression, not inside imports, TS string Enums etc.
 */ export function isStringExpression ( j , path ) { if ( ! path || ! j . StringLiteral . check ( path . node ) ) { return false ; } const parentNode = path . parent && path . parent . node ; if ( j . ImportDeclaration . check ( parentNode ) || j . JSXAttribute . check ( parentNode ) || j . TSEnumMember . check ( parentNode ) || ( ( j . ObjectProperty . check ( parentNode ) || j . ObjectMethod . check ( parentNode ) ) && parentNode . key === path . node ) ) { return false ; } return true ; } param j param n export function getSingleStatement ( j , n ) { if ( j . BlockStatement . check ( n ) && n . body . length === 1 ) { return getSingleStatement ( j , n . body [ 0 ] ) ; } if ( j . Statement . check ( n ) ) { return n ; } return null ; } param j param p export function getNextStatementInBlock ( j , p ) { if ( ! p . parent || ! j . BlockStatement . check ( p . parent . node ) ) { return null ; } const blockBody = p . parent . node . body ; const index = blockBody . indexOf ( p . node ) ; if ( index !== - 1 && index + 1 < blockBody . length ) { return blockBody [ index + 1 ] ; } return null ; } export function getParentBlockStatament ( j , p ) { if ( ! p . parent || ! j . BlockStatement . check ( p . parent . node ) ) { return null ; } const blockBody = p . parent . node . body ; const index = blockBody . indexOf ( p . node ) ; if ( index !== - 1 && index + 1 < blockBody . length ) { return blockBody [ index + 1 ] ; } return null ; }