import { SyntaxKind , ClassDeclaration , SourceFile , PropertyAssignment , StringLiteral , Identifier } from 'ts-simple-ast' ; import { AngularExternalResourcesAnalyzer } from '../external-resources/angular-external-resources.analyzer' ; import { AngularExternalTemplate } from '../external-resources/angular-external-resources.interfaces' ; /**
 * Angular External Templates File Analyzer
 */ export class AngularExternalTemplatesFileAnalyzer extends AngularExternalResourcesAnalyzer { param sourceFile returns public static getExternalTemplates ( sourceFile ) { return sourceFile . getClasses ( ) . reduce ( ( externalTemplateAssignment , classDeclaration ) : Array < PropertyAssignment > => { try { return [ ... externalTemplateAssignment , this . getComponentDecoratorPropertyOrThrow ( classDeclaration , 'templateUrl' ) ] ; } catch { return externalTemplateAssignment ; } } , [ ] ) . map ( ( externalTemplateAssignment ) : AngularExternalTemplate => { const externalTemplateKey = this . getExternalTemplateAssignmentKeyOrThrow ( externalTemplateAssignment ) ; const externalTemplateValue = this . getExternalTemplateAssignmentValueOrThrow ( externalTemplateAssignment ) ; const externalTemplatePath = this . getExternalResourcePath ( externalTemplateValue ) ; const resolvedExternalTemplatePath = this . resolveExternalResourcePath ( externalTemplatePath , sourceFile ) ; return { node : externalTemplateKey , template : { node : externalTemplateValue , path : resolvedExternalTemplatePath } } ; } ) ; } param externalTemplateAssignment returns private static getExternalTemplateAssignmentKeyOrThrow ( externalTemplateAssignment ) { return < Identifier > externalTemplateAssignment . getChildrenOfKind ( SyntaxKind . Identifier ) [ 0 ] || ( ( ) => { throw new Error ( 'External template does not have a key.' ) ; } ) ( ) ; } param externalTemplateAssignment returns private static getExternalTemplateAssignmentValueOrThrow ( externalTemplateAssignment ) { return < StringLiteral > externalTemplateAssignment . getChildrenOfKind ( SyntaxKind . StringLiteral ) [ 0 ] || ( ( ) => { throw new Error ( 'External template doesn ot have a vlaue.' ) ; } ) ( ) ; } }