1- import {
2- ArrayLiteralExpression ,
3- CallExpression ,
4- isCallExpression ,
5- NodeArray ,
6- ObjectLiteralExpression ,
7- PropertyAssignment ,
8- StringLiteral ,
9- SyntaxKind ,
10- getDecorators ,
11- isClassDeclaration ,
12- } from 'typescript' ;
1+ import ts from 'typescript' ;
132import { CategorizedClassDoc } from './dgeni-definitions' ;
143
154/**
@@ -30,15 +19,15 @@ import {CategorizedClassDoc} from './dgeni-definitions';
3019export function getDirectiveMetadata ( classDoc : CategorizedClassDoc ) : Map < string , any > | null {
3120 const declaration = classDoc . symbol . valueDeclaration ;
3221 const decorators =
33- declaration && isClassDeclaration ( declaration ) ? getDecorators ( declaration ) : null ;
22+ declaration && ts . isClassDeclaration ( declaration ) ? ts . getDecorators ( declaration ) : null ;
3423
3524 if ( ! decorators ?. length ) {
3625 return null ;
3726 }
3827
3928 const expression = decorators
40- . filter ( decorator => decorator . expression && isCallExpression ( decorator . expression ) )
41- . map ( decorator => decorator . expression as CallExpression )
29+ . filter ( decorator => decorator . expression && ts . isCallExpression ( decorator . expression ) )
30+ . map ( decorator => decorator . expression as ts . CallExpression )
4231 . find (
4332 callExpression =>
4433 callExpression . expression . getText ( ) === 'Component' ||
@@ -55,25 +44,41 @@ export function getDirectiveMetadata(classDoc: CategorizedClassDoc): Map<string,
5544 return null ;
5645 }
5746
58- const objectExpression = expression . arguments [ 0 ] as ObjectLiteralExpression ;
47+ const objectExpression = expression . arguments [ 0 ] as ts . ObjectLiteralExpression ;
5948 const resultMetadata = new Map < string , any > ( ) ;
6049
61- ( objectExpression . properties as NodeArray < PropertyAssignment > ) . forEach ( prop => {
50+ ( objectExpression . properties as ts . NodeArray < ts . PropertyAssignment > ) . forEach ( prop => {
6251 // Support ArrayLiteralExpression assignments in the directive metadata.
63- if ( prop . initializer . kind === SyntaxKind . ArrayLiteralExpression ) {
64- const arrayData = ( prop . initializer as ArrayLiteralExpression ) . elements . map (
65- literal => ( literal as StringLiteral ) . text ,
66- ) ;
52+ if ( ts . isArrayLiteralExpression ( prop . initializer ) ) {
53+ const arrayData = prop . initializer . elements . map ( literal => {
54+ if ( ts . isStringLiteralLike ( literal ) ) {
55+ return literal . text ;
56+ }
57+
58+ if ( ts . isObjectLiteralExpression ( literal ) ) {
59+ return literal . properties . reduce (
60+ ( result , prop ) => {
61+ if ( ts . isPropertyAssignment ( prop ) ) {
62+ result [ prop . name . getText ( ) ] = ts . isStringLiteralLike ( prop . initializer )
63+ ? prop . initializer . text
64+ : prop . initializer . getText ( ) ;
65+ }
66+
67+ return result ;
68+ } ,
69+ { } as Record < string , string > ,
70+ ) ;
71+ }
72+
73+ return literal . getText ( ) ;
74+ } ) ;
6775
6876 resultMetadata . set ( prop . name . getText ( ) , arrayData ) ;
6977 }
7078
7179 // Support normal StringLiteral and NoSubstitutionTemplateLiteral assignments
72- if (
73- prop . initializer . kind === SyntaxKind . StringLiteral ||
74- prop . initializer . kind === SyntaxKind . NoSubstitutionTemplateLiteral
75- ) {
76- resultMetadata . set ( prop . name . getText ( ) , ( prop . initializer as StringLiteral ) . text ) ;
80+ if ( ts . isStringLiteralLike ( prop . initializer ) ) {
81+ resultMetadata . set ( prop . name . getText ( ) , prop . initializer . text ) ;
7782 }
7883 } ) ;
7984
0 commit comments