import * as ts from 'typescript' ; import { FromWhat } from './common' ; import { Imports } from '../types' ; import { namespaceBlacklist } from './namespaceBlacklist' ; type ImportedNamespace = { file : string ; namespaces : string [ ] ; } ; const getPossibleImportedNamespaces = ( imports ) : ImportedNamespace [ ] => { const keys = Object . keys ( imports ) ; const imported = [ ] ; keys . forEach ( ( fromFile ) => { const namespaces = imports [ fromFile ] . map ( ( i ) => `${ i }.` ) ; imported . push ( { file : fromFile , namespaces : namespaces , } ) ; } ) ; return imported ; } ; const mayContainImportsFromNamespace = ( node , imports ) : boolean => { const nodeText = node . getText ( ) ; return imports . some ( ( possible ) => { return possible . namespaces . some ( ( ns ) => nodeText . includes ( ns ) ) ; } ) ; } ; export const addImportsFromNamespace = ( node , imports , addImport ) : void => { const possibles = getPossibleImportedNamespaces ( imports ) ; if ( ! mayContainImportsFromNamespace ( node , possibles ) ) { return ; } const findImportUsagesWithin = ( node ) : void => { const nodeText = node . getText ( ) ; possibles . forEach ( ( p ) => { p . namespaces . forEach ( ( ns ) => { if ( nodeText . startsWith ( ns ) ) { addImport ( { from : p . file , what : [ nodeText ] , } ) ; } } ) ; } ) ; } ; const recurseIntoChildren = ( next ) : void => { findImportUsagesWithin ( next ) ; next . getChildren ( ) . filter ( ( c ) => ! namespaceBlacklist . includes ( c . kind ) ) . forEach ( recurseIntoChildren ) ; } ; recurseIntoChildren ( node ) ; } ;