import { nanoid } from "nanoid" ; import produce from "immer" ; import * as AST from "../ast" ; import { getNodeById , getNodesByIds } from "../visit" ; import { replaceFromLists } from "./replace" ; export const updateLookAroundAssertion = ( ast , selectedIds , kind , negate ) => { let nextSelectedIds = selectedIds ; const nextAst = produce ( ast , ( draft ) => { const { node , nodeList , index } = getNodeById ( draft , selectedIds [ 0 ] ) ; if ( node . type === "lookAroundAssertion" ) { const { id , type , children } = node ; const lookAroundAssertionNode = { id , type , children , kind , negate , } ; nodeList [ index ] = lookAroundAssertionNode ; } } ) ; return { nextAst , nextSelectedIds } ; } ; export const lookAroundAssertionIt = ( ast , selectedIds , kind ) => { const id = nanoid ( ) ; const nextSelectedIds = [ id ] ; const nextAst = produce ( ast , ( draft ) => { const { nodes , nodeList } = getNodesByIds ( draft , selectedIds ) ; const lookAroundAssertionNode = { id , type : "lookAroundAssertion" , kind , negate : false , children : nodes , } ; replaceFromLists ( nodeList , nodes , [ lookAroundAssertionNode ] ) ; } ) ; return { nextAst , nextSelectedIds } ; } ; export const unLookAroundAssertion = ( ast , selectedIds ) => { let nextSelectedIds = selectedIds ; const nextAst = produce ( ast , ( draft ) => { const { node , nodeList } = getNodeById ( draft , selectedIds [ 0 ] ) ; if ( node . type === "lookAroundAssertion" ) { const { children } = node ; replaceFromLists ( nodeList , [ node ] , children ) ; nextSelectedIds = children . map ( ( { id } ) => id ) ; } } ) ; return { nextAst , nextSelectedIds } ; } ;