import ts from 'typescript' ; import { body , syntaxKind } from './base' ; import * as node_ from './node' ; import * as utils from './utils' ; import * as variable from './variable' ; export type StatementedNode = ts . Block | ts . SourceFile | ts . FunctionDeclaration | ts . ModuleDeclaration | ts . FunctionLikeDeclaration | ts . CaseClause | ts . DefaultClause ; export function getStatements ( node ) { if ( ts . isSourceFile ( node ) || ts . isCaseClause ( node ) || ts . isDefaultClause ( node ) ) { return node . statements ; } if ( ts . isBlock ( node ) ) { return node . statements ; } let moduleBody = body . getBody ( node ) ; while ( moduleBody !== undefined && ( moduleBody as any ) . statements == undefined ) { moduleBody = body . getBody ( moduleBody as any ) ; } if ( moduleBody === undefined ) { throw new Error ( `Could not find statements for ${ syntaxKind . getSyntaxKindName ( node . kind ) }` ) ; } return ( moduleBody as any ) . statements ; } export function getClasses ( node ) { return getStatements ( node ) . filter ( ts . isClassDeclaration ) ; } export function getClass ( node , name ) { return getClasses ( node ) . find ( ( value ) => node_ . getName ( value ) === name ) ; } export function getClassOrThrow ( node , name ) { return utils . throwIfNullOrUndefined ( getClass ( node , name ) , 'class' ) ; } export function getInterfaces ( node ) { return getStatements ( node ) . filter ( ts . isInterfaceDeclaration ) ; } export function getInterface ( node , name ) { return getInterfaces ( node ) . find ( ( value ) => node_ . getName ( value ) === name ) ; } export function getInterfaceOrThrow ( node , name ) { return utils . throwIfNullOrUndefined ( getInterface ( node , name ) , 'interface' ) ; } export function getNamespaces ( node ) { return getStatements ( node ) . filter ( ts . isModuleDeclaration ) ; } export function getNamespace ( node , name ) { return getNamespaces ( node ) . find ( ( value ) => node_ . getName ( value ) === name ) ; } export function getNamespaceOrThrow ( node , name ) { return utils . throwIfNullOrUndefined ( getNamespace ( node , name ) , 'namespace' ) ; } export function getVariableStatements ( node ) { return getStatements ( node ) . filter ( ts . isVariableStatement ) ; } export function getVariableDeclarations ( node ) { return getVariableStatements ( node ) . reduce < readonly ts . VariableDeclaration [ ] > ( ( acc , value ) => acc . concat ( variable . getDeclarations ( variable . getDeclarationList ( value ) ) ) , [ ] ) ; } export function getVariableDeclaration ( node , name ) { return getVariableDeclarations ( node ) . find ( ( value ) => node_ . getName ( value ) === name ) ; } export function getVariableDeclarationOrThrow ( node , name ) { return utils . throwIfNullOrUndefined ( getVariableDeclaration ( node , name ) , 'variable declaration' ) ; } export function getTypeAliases ( node ) { return getStatements ( node ) . filter ( ts . isTypeAliasDeclaration ) ; } export function getTypeAlias ( node , name ) { return getTypeAliases ( node ) . find ( ( value ) => node_ . getName ( value ) === name ) ; } export function getTypeAliasOrThrow ( node , name ) { return utils . throwIfNullOrUndefined ( getTypeAlias ( node , name ) , 'type alias' ) ; } export function getFunctions ( node ) { return getStatements ( node ) . filter ( ts . isFunctionDeclaration ) ; } export function getFunction ( node , name ) { return getFunctions ( node ) . find ( ( value ) => node_ . getName ( value ) === name ) ; } export function getFunctionOrThrow ( node , name ) { return utils . throwIfNullOrUndefined ( getFunction ( node , name ) , 'function' ) ; } export function getEnums ( node ) { return getStatements ( node ) . filter ( ts . isEnumDeclaration ) ; } export function getEnum ( node , name ) { return getEnums ( node ) . find ( ( value ) => node_ . getName ( value ) === name ) ; } export function getEnumOrThrow ( node , name ) { return utils . throwIfNullOrUndefined ( getEnum ( node , name ) , 'enum' ) ; } export function getLabel ( node ) { return utils . getValueOrUndefined ( node . label ) ; } export function getClauses ( node ) { return node . clauses ; } export function getOnlyVariableDeclaration ( node ) { return utils . getValueOrUndefined ( node . variableDeclaration ) ; } export function getBlock ( node ) { return node . block ; } export function getStatement ( node ) { return node . statement ; } export function getInitializer ( node ) ; export function getInitializer ( node ) ; export function getInitializer ( node ) { return utils . getValueOrUndefined ( node . initializer ) ; } export function getCondition ( node ) { return utils . getValueOrUndefined ( node . condition ) ; } export function getIncrementor ( node ) { return utils . getValueOrUndefined ( node . incrementor ) ; } export function getThenStatement ( node ) { return node . thenStatement ; } export function getElseStatement ( node ) { return utils . getValueOrUndefined ( node . elseStatement ) ; } export function getCaseBlock ( node ) { return node . caseBlock ; } export function getCatchClause ( node ) { return utils . getValueOrUndefined ( node . catchClause ) ; } export function getTryBlock ( node ) { return node . tryBlock ; } export function getFinallyBlock ( node ) { return utils . getValueOrUndefined ( node . finallyBlock ) ; }