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' ;
13
2
import { CategorizedClassDoc } from './dgeni-definitions' ;
14
3
15
4
/**
@@ -30,15 +19,15 @@ import {CategorizedClassDoc} from './dgeni-definitions';
30
19
export function getDirectiveMetadata ( classDoc : CategorizedClassDoc ) : Map < string , any > | null {
31
20
const declaration = classDoc . symbol . valueDeclaration ;
32
21
const decorators =
33
- declaration && isClassDeclaration ( declaration ) ? getDecorators ( declaration ) : null ;
22
+ declaration && ts . isClassDeclaration ( declaration ) ? ts . getDecorators ( declaration ) : null ;
34
23
35
24
if ( ! decorators ?. length ) {
36
25
return null ;
37
26
}
38
27
39
28
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 )
42
31
. find (
43
32
callExpression =>
44
33
callExpression . expression . getText ( ) === 'Component' ||
@@ -55,25 +44,41 @@ export function getDirectiveMetadata(classDoc: CategorizedClassDoc): Map<string,
55
44
return null ;
56
45
}
57
46
58
- const objectExpression = expression . arguments [ 0 ] as ObjectLiteralExpression ;
47
+ const objectExpression = expression . arguments [ 0 ] as ts . ObjectLiteralExpression ;
59
48
const resultMetadata = new Map < string , any > ( ) ;
60
49
61
- ( objectExpression . properties as NodeArray < PropertyAssignment > ) . forEach ( prop => {
50
+ ( objectExpression . properties as ts . NodeArray < ts . PropertyAssignment > ) . forEach ( prop => {
62
51
// 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
+ } ) ;
67
75
68
76
resultMetadata . set ( prop . name . getText ( ) , arrayData ) ;
69
77
}
70
78
71
79
// 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 ) ;
77
82
}
78
83
} ) ;
79
84
0 commit comments