@@ -61,35 +61,57 @@ function _angularImportsFromNode(node: ts.ImportDeclaration, sourceFile: ts.Sour
61
61
function _ctorParameterFromTypeReference ( paramNode : ts . ParameterDeclaration ,
62
62
angularImports : string [ ] ,
63
63
refactor : TypeScriptFileRefactor ) {
64
- if ( paramNode . type . kind == ts . SyntaxKind . TypeReference ) {
65
- const type = paramNode . type as ts . TypeReferenceNode ;
66
- const decorators = refactor . findAstNodes ( paramNode , ts . SyntaxKind . Decorator ) as ts . Decorator [ ] ;
67
- const decoratorStr = decorators
68
- . map ( decorator => {
69
- const fnName =
70
- ( refactor . findFirstAstNode ( decorator , ts . SyntaxKind . CallExpression ) as ts . CallExpression )
71
- . expression . getText ( refactor . sourceFile ) ;
72
-
73
- if ( angularImports . indexOf ( fnName ) === - 1 ) {
74
- return null ;
64
+ let typeName = 'undefined' ;
65
+
66
+ if ( paramNode . type ) {
67
+ switch ( paramNode . type . kind ) {
68
+ case ts . SyntaxKind . TypeReference :
69
+ const type = paramNode . type as ts . TypeReferenceNode ;
70
+ if ( type . typeName ) {
71
+ typeName = type . typeName . getText ( refactor . sourceFile ) ;
75
72
} else {
76
- return fnName ;
73
+ typeName = type . getText ( refactor . sourceFile ) ;
77
74
}
78
- } )
79
- . filter ( x => ! ! x )
80
- . map ( name => `{ type: ${ name } }` )
81
- . join ( ', ' ) ;
82
-
83
- if ( type . typeName . kind == ts . SyntaxKind . Identifier ) {
84
- const typeName = type . typeName as ts . Identifier ;
85
- if ( decorators . length > 0 ) {
86
- return `{ type: ${ typeName . text } , decorators: [${ decoratorStr } ] }` ;
87
- }
88
- return `{ type: ${ typeName . text } }` ;
75
+ break ;
76
+ case ts . SyntaxKind . AnyKeyword :
77
+ typeName = 'undefined' ;
78
+ break ;
79
+ default :
80
+ typeName = 'null' ;
89
81
}
90
82
}
91
83
92
- return 'null' ;
84
+ const decorators = refactor . findAstNodes ( paramNode , ts . SyntaxKind . Decorator ) as ts . Decorator [ ] ;
85
+ const decoratorStr = decorators
86
+ . map ( decorator => {
87
+ const call =
88
+ refactor . findFirstAstNode ( decorator , ts . SyntaxKind . CallExpression ) as ts . CallExpression ;
89
+
90
+ if ( ! call ) {
91
+ return null ;
92
+ }
93
+
94
+ const fnName = call . expression . getText ( refactor . sourceFile ) ;
95
+ const args = call . arguments . map ( x => x . getText ( refactor . sourceFile ) ) . join ( ', ' ) ;
96
+ if ( angularImports . indexOf ( fnName ) === - 1 ) {
97
+ return null ;
98
+ } else {
99
+ return [ fnName , args ] ;
100
+ }
101
+ } )
102
+ . filter ( x => ! ! x )
103
+ . map ( ( [ name , args ] : string [ ] ) => {
104
+ if ( args ) {
105
+ return `{ type: ${ name } , args: [${ args } ] }` ;
106
+ }
107
+ return `{ type: ${ name } }` ;
108
+ } )
109
+ . join ( ', ' ) ;
110
+
111
+ if ( decorators . length > 0 ) {
112
+ return `{ type: ${ typeName } , decorators: [${ decoratorStr } ] }` ;
113
+ }
114
+ return `{ type: ${ typeName } }` ;
93
115
}
94
116
95
117
@@ -106,12 +128,7 @@ function _addCtorParameters(classNode: ts.ClassDeclaration,
106
128
}
107
129
108
130
const params = Array . from ( ctor . parameters ) . map ( paramNode => {
109
- switch ( paramNode . type . kind ) {
110
- case ts . SyntaxKind . TypeReference :
111
- return _ctorParameterFromTypeReference ( paramNode , angularImports , refactor ) ;
112
- default :
113
- return 'null' ;
114
- }
131
+ return _ctorParameterFromTypeReference ( paramNode , angularImports , refactor ) ;
115
132
} ) ;
116
133
117
134
const ctorParametersDecl = `static ctorParameters() { return [ ${ params . join ( ', ' ) } ]; }` ;
0 commit comments