import { AstNode , BinaryExpression , Expression , FunctionDeclaration , IfStatement , Node , NodePath , Printable , StringLiteral , TemplateElement , UnaryExpression , VariableDeclaration , } from 'ast-types' ; import { Collection , JsCodeShift } from 'jscodeshift' ; import { CodeModExports } from '../codeModTypes' ; import * as astHelpers from '../utils/astHelpers' ; import { extractSelectionAnchor , SELECTION_ANCHOR } from '../utils/extractSelectionAnchor' ; const codeMod = ( ( fileInfo , api , options ) => { const j = api . jscodeshift ; const ast = fileInfo . ast ; const target = options . target ; const path = target . firstPath < StringLiteral > ( ) ! ; const node = path . node ; const raw = ( ( node as any ) . extra as any ) . raw as string ; const quoteChar = raw [ 0 ] ; const offset = options . selection . active - node . start ; let leftValue = raw . substring ( 0 , offset ) ; let rightValue = raw . substring ( offset ) ; if ( leftValue . endsWith ( '\\' ) ) { const m = rightValue . match ( /^[\r\n]+/ ) ; if ( m ) { leftValue = leftValue . substring ( 0 , leftValue . length - 1 ) ; rightValue = rightValue . substring ( m [ 0 ] . length ) ; } else { leftValue = leftValue . substring ( 0 , leftValue . length - 1 ) ; rightValue = '\\' + rightValue ; } } const leftNode = j . identifier ( leftValue + quoteChar ) ; const rightNode = j . identifier ( SELECTION_ANCHOR + quoteChar + rightValue ) ; let replacementTarget = path as NodePath < AstNode > ; while ( replacementTarget . parent && j . BinaryExpression . check ( replacementTarget . parent . node ) && replacementTarget . parent . node . operator === '+' && replacementTarget . parent . node . right === replacementTarget . node ) { replacementTarget = replacementTarget . parent ; } if ( replacementTarget === path ) { path . replace ( j . binaryExpression ( '+' , leftNode , rightNode ) ) ; } else { ( path . parent . node as BinaryExpression ) . right = leftNode ; replacementTarget . replace ( j . binaryExpression ( '+' , replacementTarget . node as Expression , rightNode ) ) ; } return extractSelectionAnchor ( ast . toSource ( ) ) ; } ) as CodeModExports ; codeMod . canRun = ( fileInfo , api , options ) => { const j = api . jscodeshift ; const ast = fileInfo . ast ; const path = options . target . firstPath ( ) ; if ( ! path || ! astHelpers . isStringExpression ( j , path ) ) { return false ; } return options . selection . active > path . node . start && options . selection . active < path . node . end ; } ; codeMod . scope = 'cursor' ; codeMod . title = 'Split string under cursor' ; codeMod . description = '' ; codeMod . detail = '' ; module . exports = codeMod ;