import { ImportLookup , ImportLookupResult } from './analyzerFileInfo' ; import { Declaration , DeclarationType } from './declaration' ; import { Symbol } from './symbol' ; export interface ResolvedAliasInfo { declaration : Declaration | undefined ; isPrivate : boolean ; privatePyTypedImported ? : string ; privatePyTypedImporter ? : string ; } export function resolveAliasDeclaration ( importLookup , declaration , resolveLocalNames , allowExternallyHiddenAccess ) { let curDeclaration = declaration ; const alreadyVisited = [ ] ; let isPrivate = false ; let isPrivatePyTypedImport = false ; let privatePyTypedImported ; let privatePyTypedImporter ; while ( true ) { if ( curDeclaration . type !== DeclarationType . Alias || ! curDeclaration . symbolName ) { return { declaration : curDeclaration , isPrivate , privatePyTypedImported , privatePyTypedImporter , } ; } if ( ! resolveLocalNames && curDeclaration . usesLocalName ) { return { declaration : curDeclaration , isPrivate , privatePyTypedImported , privatePyTypedImporter , } ; } let lookupResult ; if ( curDeclaration . path && curDeclaration . loadSymbolsFromPath ) { lookupResult = importLookup ( curDeclaration . path ) ; } const symbol = lookupResult ? lookupResult . symbolTable . get ( curDeclaration . symbolName ) : undefined ; if ( ! symbol ) { if ( curDeclaration . submoduleFallback ) { return resolveAliasDeclaration ( importLookup , curDeclaration . submoduleFallback , resolveLocalNames , allowExternallyHiddenAccess ) ; } if ( curDeclaration . isNativeLib ) { return { declaration : undefined , isPrivate , } ; } return undefined ; } if ( symbol . isPrivateMember ( ) ) { isPrivate = true ; } if ( symbol . isExternallyHidden ( ) && ! allowExternallyHiddenAccess ) { return undefined ; } let declarations = symbol . getTypedDeclarations ( ) ; if ( declarations . length === 0 ) { declarations = symbol . getDeclarations ( ) ; if ( declarations . length === 0 ) { return undefined ; } } const unvisitedDecls = declarations . filter ( ( decl ) => ! alreadyVisited . includes ( decl ) ) ; if ( unvisitedDecls . length > 0 ) { curDeclaration = unvisitedDecls [ unvisitedDecls . length - 1 ] ; } else { curDeclaration = declarations [ declarations . length - 1 ] ; } if ( isPrivatePyTypedImport ) { privatePyTypedImported = privatePyTypedImported ?? curDeclaration ?. moduleName ; } if ( symbol . isPrivatePyTypedImport ( ) ) { isPrivatePyTypedImport = true ; } if ( isPrivatePyTypedImport ) { privatePyTypedImporter = privatePyTypedImporter ?? curDeclaration ?. moduleName ; } if ( alreadyVisited . find ( ( decl ) => decl === curDeclaration ) ) { if ( curDeclaration . path === declaration . path && curDeclaration . type === DeclarationType . Alias && curDeclaration . submoduleFallback ) { return resolveAliasDeclaration ( importLookup , curDeclaration . submoduleFallback , resolveLocalNames , allowExternallyHiddenAccess ) ; } return { declaration , isPrivate , privatePyTypedImported , privatePyTypedImporter , } ; } alreadyVisited . push ( curDeclaration ) ; } }