import * as ts from "typescript" ; import { I18nDef } from "../../src/i18n-polyfill" ; export abstract class AbstractAstParser { protected _sourceFile : ts . SourceFile ; protected _createSourceFile ( path , contents ) { return ts . createSourceFile ( path , contents , ts . ScriptTarget . ES5 , false ) ; } /**
     * Get strings from function call's first argument
     */ protected _getCallArgStrings ( callNode ) { if ( ! callNode . arguments . length ) { return [ ] ; } const firstArg = callNode . arguments [ 0 ] ; switch ( firstArg . kind ) { case ts . SyntaxKind . StringLiteral : case ts . SyntaxKind . FirstTemplateToken : return [ ( firstArg as ts . StringLiteral ) . text ] ; case ts . SyntaxKind . ArrayLiteralExpression : return ( firstArg as ts . ArrayLiteralExpression ) . elements . map ( ( element ) => element . text ) ; case ts . SyntaxKind . ObjectLiteralExpression : const i18nDef = { value : "" } ; ( firstArg as ts . ObjectLiteralExpression ) . properties . forEach ( ( prop ) => { i18nDef [ ( prop . name as ts . Identifier ) . text ] = ( prop . initializer as ts . StringLiteral ) . text ; } ) ; if ( ! i18nDef . value ) { throw new Error ( `An I18nDef requires a value property on '${ this . syntaxKindToName ( firstArg . kind ) }' for ${ firstArg }` ) ; } return [ i18nDef ] ; case ts . SyntaxKind . Identifier : console . log ( "WARNING: We cannot extract variable values passed to TranslateService (yet)" ) ; break ; default : console . log ( `SKIP: Unknown argument type: '${ this . syntaxKindToName ( firstArg . kind ) }'` , firstArg ) ; } return [ ] ; } /**
     * Find all child nodes of a kind
     */ protected _findNodes ( node , kind ) { const childrenNodes = node . getChildren ( this . _sourceFile ) ; const initialValue = node . kind === kind ? [ node ] : [ ] ; return childrenNodes . reduce ( ( result , childNode ) => { return result . concat ( this . _findNodes ( childNode , kind ) ) ; } , initialValue ) ; } protected syntaxKindToName ( kind ) { return ts . SyntaxKind [ kind ] ; } }