import { VirtualScope , VScopeContentType , VirtualScopeType } from "." ; import * as ESTree from "estree" ; import { Scope , ScopeManager , Variable , ModuleScope } from "../.." ; export class ExportDefaultVirtualScope implements VirtualScope { public readonly children : VirtualScope [ ] = [ ] ; public constructor ( public readonly contentType , public readonly declaration , public isChildrenDependent = true ) { } public get type ( ) { return VirtualScopeType . Default ; } public findAllReferencesToVirtualScope ( visitedSet , scopeManager , virtualScopeMap ) { const moduleScope = this . getModuleScope ( scopeManager ) ; if ( this . contentType === VScopeContentType . Reference ) { const id = this . declaration as ESTree . Identifier ; const idName = id . name ; const variable = moduleScope . set . get ( idName ) ! ; const vs = virtualScopeMap . get ( variable ) ! ; this . children . push ( vs ) ; } else { const scopes = scopeManager ! . __nodeToScope . get ( this . declaration ) ! ; this . traverseScopes ( scopes , moduleScope , visitedSet , virtualScopeMap ) ; } } private getModuleScope ( scopeManager ) { return scopeManager ! . scopes [ 1 ] as ModuleScope ; } private traverseScopes ( scopes , moduleScope , visitedSet , virtualScopeMap ) { scopes . forEach ( scope => { visitedSet . add ( scope ) ; /**
             * recursive function to traverse all scopes belong
             * to this
             */ const traverse = ( scope ) => { scope . references . forEach ( ref => { if ( ref . resolved && ref . resolved . scope . type === "module" ) { const idName = ref . identifier . name ; this . addToVs ( idName , moduleScope , virtualScopeMap ) ; } } ) ; scope . childScopes . forEach ( traverse ) ; } ; traverse ( scope ) ; } ) ; } private addToVs ( idName , moduleScope , virtualScopeMap ) { const refVar = moduleScope . set . get ( idName ) ! ; const virtualScope = virtualScopeMap . get ( refVar ) ! ; this . children . push ( virtualScope ) ; } }