import { Expression } from "@babel/types" ; import { parseAsEstreeExpression } from "./parse" ; import { precook , PrecookOptions } from "./precook" ; export type PreevaluateOptions = Omit < PrecookOptions , "expressionOnly" > ; export interface PreevaluateResult { expression : Expression ; attemptToVisitGlobals : Set < string > ; source : string ; prefix : string ; suffix : string ; } export function preevaluate ( raw , options ? ) { const fixes = [ ] ; const source = raw . replace ( /^\s*<%~?\s|\s%>\s*$/g , ( m ) => { fixes . push ( m ) ; return "" ; } ) ; const expression = parseAsEstreeExpression ( source ) ; const attemptToVisitGlobals = precook ( expression , { ... options , expressionOnly : true , } ) ; return { expression , attemptToVisitGlobals , source , prefix : fixes [ 0 ] , suffix : fixes [ 1 ] , } ; } export function isEvaluable ( raw ) { return /^\s*<%~?\s/ . test ( raw ) && /\s%>\s*$/ . test ( raw ) ; } export function shouldAllowRecursiveEvaluations ( raw ) { return /^\s*<%~\s/ . test ( raw ) ; }