import { ParseNode } from '../parser/parseNodes' ; import { getScope } from './analyzerNodeInfo' ; import { getEvaluationScopeNode } from './parseTreeUtils' ; import { Scope , ScopeType } from './scope' ; export function getBuiltInScope ( currentScope ) { let builtInScope = currentScope ; while ( builtInScope . type !== ScopeType . Builtin ) { builtInScope = builtInScope . parent ! ; } return builtInScope ; } export function getScopeForNode ( node ) { const scopeNode = getEvaluationScopeNode ( node ) ; return getScope ( scopeNode ) ; } export function getScopeHierarchy ( node , stopScope ? ) { const scopeHierarchy = [ ] ; let curNode = node ; while ( curNode ) { const curScope = getScopeForNode ( curNode ) ; if ( ! curScope ) { return undefined ; } if ( scopeHierarchy . length === 0 || scopeHierarchy [ scopeHierarchy . length - 1 ] !== curScope ) { scopeHierarchy . push ( curScope ) ; } if ( curScope === stopScope ) { return scopeHierarchy ; } curNode = curNode . parent ; } return stopScope ? undefined : scopeHierarchy ; } export function findTopNodeInScope ( node , scope ) { let curNode = node ; let prevNode ; let foundScope = false ; while ( curNode ) { if ( getScope ( curNode ) === scope ) { foundScope = true ; } else if ( foundScope ) { return prevNode ; } prevNode = curNode ; curNode = curNode . parent ; } return undefined ; }