import type * as ts from 'typescript' ; import { createNodeArray } from '../../../typescriptFactory/typescriptFactory' ; import { core } from '../../core/core' ; type Declaration = ts . InterfaceDeclaration | ts . ClassDeclaration | ts . TypeAliasDeclaration ; type ImportDeclaration = ts . ImportEqualsDeclaration | ts . ImportOrExportSpecifier | ts . ImportClause ; export namespace TypescriptHelper { export function IsLiteralOrPrimitive ( typeNode ) { return ( core . ts . isLiteralTypeNode ( typeNode ) || typeNode . kind === core . ts . SyntaxKind . StringKeyword || typeNode . kind === core . ts . SyntaxKind . BooleanKeyword || typeNode . kind === core . ts . SyntaxKind . NumberKeyword || typeNode . kind === core . ts . SyntaxKind . ArrayType ) ; } export function GetDeclarationFromNode ( node ) { const typeChecker = core . typeChecker ; const symbol = typeChecker . getSymbolAtLocation ( node ) ; if ( ! symbol ) { throw new Error ( `The type checker failed to look up a symbol for \`${ node . getText ( ) }'. 
        Perhaps, the checker was searching an outdated source.` ) ; } return GetDeclarationFromSymbol ( symbol ) ; } export function GetDeclarationFromSymbol ( symbol ) { const declarations = symbol . declarations ; if ( ! declarations ) { throw new Error ( `Failed to look up declarations for \`${ symbol . getName ( ) }'.` ) ; } const declaration = GetFirstValidDeclaration ( declarations ) ; if ( isImportExportDeclaration ( declaration ) ) { return GetDeclarationForImport ( declaration ) ; } return declaration ; } export function GetConcreteDeclarationFromSymbol ( symbol ) { const declarations = symbol . declarations ; if ( ! declarations ) { throw new Error ( `Failed to look up declarations for \`${ symbol . getName ( ) }'.` ) ; } const declaration = declarations [ 0 ] ; if ( isImportExportDeclaration ( declaration ) ) { return GetConcreteDeclarationForImport ( declaration ) ; } return declaration ; } export function GetDeclarationForImport ( node ) { const declarations = GetDeclarationsForImport ( node ) ; return GetFirstValidDeclaration ( declarations ) ; } export function GetConcreteDeclarationForImport ( node ) { const declarations = GetDeclarationsForImport ( node ) ; return declarations [ 0 ] ; } export function GetParameterOfNode ( node ) { const declaration = GetDeclarationFromNode ( node ) ; const { typeParameters = createNodeArray ( [ ] ) } = declaration as Declaration ; return typeParameters ; } export function GetTypeParameterOwnerMock ( declaration ) { const typeDeclaration = core . ts . getTypeParameterOwner ( declaration ) ; if ( typeDeclaration && ( typeDeclaration as Declaration ) . typeParameters ) { return typeDeclaration ; } for ( let current = declaration ; current ; current = current . parent ) { if ( current . kind === core . ts . SyntaxKind . TypeAliasDeclaration ) { return current as ts . Declaration ; } } } export function GetStringPropertyName ( propertyName ) { if ( ! core . ts . isComputedPropertyName ( propertyName ) ) { return propertyName . text ; } const symbol = core . typeChecker . getSymbolAtLocation ( propertyName ) ; if ( ! symbol ) { throw new Error ( `The type checker failed to look up symbol for property: ${ propertyName . getText ( ) }.` ) ; } return symbol . escapedName . toString ( ) ; } export function GetAliasedSymbolSafe ( alias ) { return isAlias ( alias ) ? core . typeChecker . getAliasedSymbol ( alias ) : alias ; } export function getSignatureOfCallExpression ( node ) { return core . typeChecker . getResolvedSignature ( node ) ; } export function hasTypeArguments ( node ) { return ( typeof node . typeArguments !== 'undefined' && ! ! node . typeArguments . length ) ; } function GetFirstValidDeclaration ( declarations ) { return ( declarations . find ( ( declaration ) => ! core . ts . isVariableDeclaration ( declaration ) && ! core . ts . isFunctionDeclaration ( declaration ) && ! core . ts . isModuleDeclaration ( declaration ) ) || declarations [ 0 ] ) ; } function isAlias ( symbol ) { return ! ! ( symbol . flags & core . ts . SymbolFlags . Alias || symbol . flags & core . ts . SymbolFlags . AliasExcludes ) ; } function isImportExportDeclaration ( declaration ) { return ( core . ts . isImportEqualsDeclaration ( declaration ) || core . ts . isImportOrExportSpecifier ( declaration ) || core . ts . isImportClause ( declaration ) ) ; } function GetDeclarationsForImport ( node ) { const typeChecker = core . typeChecker ; const symbol = node . name && typeChecker . getSymbolAtLocation ( node . name ) ; const originalSymbol = symbol && typeChecker . getAliasedSymbol ( symbol ) ; return originalSymbol ?. declarations ?? [ ] ; } }