import vm from 'vm' ; import generate from '@babel/generator' ; import { NodePath } from '@babel/traverse' ; import * as t from '@babel/types' ; import type { TamaguiConfig } from '@tamagui/core' ; import { FAILED_EVAL } from './createExtractor' ; import { evaluateAstNode } from './evaluateAstNode' ; import { isValidThemeHook } from './extractHelpers' ; export function createEvaluator ( { tamaguiConfig , staticNamespace , sourcePath , traversePath , shouldPrintDebug , } ) { const evalFn = ( n ) => { if ( t . isMemberExpression ( n ) && t . isIdentifier ( n . property ) && isValidThemeHook ( traversePath , n , sourcePath ) ) { const key = n . property . name ; if ( shouldPrintDebug ) { console . log ( '    > found theme prop' , key ) ; } console . log ( 'SHOULD FIND THEME (NESTED NOW)' , key ) ; return `var(--${ key })` ; } if ( t . isIdentifier ( n ) && staticNamespace . hasOwnProperty ( n . name ) ) { return staticNamespace [ n . name ] ; } const evalContext = vm . createContext ( staticNamespace ) ; const code = `(${ generate ( n as any ) . code })` ; return vm . runInContext ( code , evalContext ) ; } ; return ( n ) => { return evaluateAstNode ( n , evalFn ) ; } ; } export function createSafeEvaluator ( attemptEval ) { return ( n ) => { try { return attemptEval ( n ) ; } catch ( err ) { return FAILED_EVAL ; } } ; }