import * as ts from "typescript" ; import { error } from "./diagnostics" ; export function isVarConst ( node ) { return ! ! ( ts . getCombinedNodeFlags ( node ) & ts . NodeFlags . Const ) ; } export function getPropertyIndex ( name , type , checker ) { const properties = checker . getPropertiesOfType ( type ) ; const index = properties . findIndex ( property => property . name === name ) ; if ( index < 0 ) { return error ( `Type '${ checker . typeToString ( type ) }' has no property '${ name }'` ) ; } return index ; } export function getTypeArguments ( type ) { if ( type . flags & ts . TypeFlags . Object ) { if ( ( type as ts . ObjectType ) . objectFlags & ts . ObjectFlags . Reference ) { return ( type as ts . TypeReference ) . typeArguments || [ ] ; } } return [ ] ; } export function addTypeArguments ( type , typeArguments ) { if ( type . flags & ts . TypeFlags . Object ) { if ( ( type as ts . ObjectType ) . objectFlags & ts . ObjectFlags . Reference ) { const typeReference = type as ts . TypeReference ; return { ... typeReference , typeArguments } ; } } return error ( "Invalid type" ) ; } export function isMethodReference ( expression , checker ) { return ( ts . isPropertyAccessExpression ( expression ) && ( checker . getTypeAtLocation ( expression ) . symbol . flags & ts . SymbolFlags . Method ) !== 0 ) ; } export function isObject ( type ) { return ! ! ( type . flags & ts . TypeFlags . Object ) ; } export function isArray ( type ) { return type . symbol && type . symbol . name === "Array" ; } export function isString ( type ) { return ! ! ( type . flags & ( ts . TypeFlags . String | ts . TypeFlags . StringLiteral ) ) ; } export function getTypeBaseName ( type , checker ) { return type . symbol ? type . symbol . name : checker . typeToString ( checker . getBaseTypeOfLiteralType ( type ) ) ; } export function isProperty ( symbol ) { return ! ! ( symbol . flags & ts . SymbolFlags . Property ) ; }