1
1
import * as ts from 'typescript' ;
2
2
import * as fs from 'fs' ;
3
- import { Symbols } from '@angular/tsc-wrapped/src/symbols' ;
3
+ import { Symbols } from '@angular/tsc-wrapped/src/symbols' ;
4
4
import {
5
5
isMetadataImportedSymbolReferenceExpression ,
6
6
isMetadataModuleReferenceExpression
7
7
} from '@angular/tsc-wrapped' ;
8
- import { Change , InsertChange , NoopChange , MultiChange } from './change' ;
9
- import { findNodes } from './node' ;
10
- import { insertImport } from './route-utils' ;
8
+ import { Change , InsertChange , NoopChange , MultiChange } from './change' ;
9
+ import { findNodes } from './node' ;
10
+ import { insertImport } from './route-utils' ;
11
11
12
- import { Observable } from 'rxjs/Observable' ;
13
- import { ReplaySubject } from 'rxjs/ReplaySubject' ;
12
+ import { Observable } from 'rxjs/Observable' ;
13
+ import { ReplaySubject } from 'rxjs/ReplaySubject' ;
14
14
import 'rxjs/add/observable/empty' ;
15
15
import 'rxjs/add/observable/of' ;
16
16
import 'rxjs/add/operator/do' ;
@@ -112,26 +112,26 @@ export function getDecoratorMetadata(source: ts.SourceFile, identifier: string,
112
112
return getSourceNodes ( source )
113
113
. filter ( node => {
114
114
return node . kind == ts . SyntaxKind . Decorator
115
- && ( < ts . Decorator > node ) . expression . kind == ts . SyntaxKind . CallExpression ;
115
+ && ( node as ts . Decorator ) . expression . kind == ts . SyntaxKind . CallExpression ;
116
116
} )
117
- . map ( node => < ts . CallExpression > ( < ts . Decorator > node ) . expression )
117
+ . map ( node => ( node as ts . Decorator ) . expression as ts . CallExpression )
118
118
. filter ( expr => {
119
119
if ( expr . expression . kind == ts . SyntaxKind . Identifier ) {
120
- const id = < ts . Identifier > expr . expression ;
120
+ const id = expr . expression as ts . Identifier ;
121
121
const metaData = symbols . resolve ( id . getFullText ( source ) ) ;
122
122
if ( isMetadataImportedSymbolReferenceExpression ( metaData ) ) {
123
123
return metaData . name == identifier && metaData . module == module ;
124
124
}
125
125
} else if ( expr . expression . kind == ts . SyntaxKind . PropertyAccessExpression ) {
126
126
// This covers foo.NgModule when importing * as foo.
127
- const paExpr = < ts . PropertyAccessExpression > expr . expression ;
127
+ const paExpr = expr . expression as ts . PropertyAccessExpression ;
128
128
// If the left expression is not an identifier, just give up at that point.
129
129
if ( paExpr . expression . kind !== ts . SyntaxKind . Identifier ) {
130
130
return false ;
131
131
}
132
132
133
133
const id = paExpr . name ;
134
- const moduleId = < ts . Identifier > paExpr . expression ;
134
+ const moduleId = paExpr . expression as ts . Identifier ;
135
135
const moduleMetaData = symbols . resolve ( moduleId . getFullText ( source ) ) ;
136
136
if ( isMetadataModuleReferenceExpression ( moduleMetaData ) ) {
137
137
return moduleMetaData . module == module && id . getFullText ( source ) == identifier ;
@@ -141,7 +141,7 @@ export function getDecoratorMetadata(source: ts.SourceFile, identifier: string,
141
141
} )
142
142
. filter ( expr => expr . arguments [ 0 ]
143
143
&& expr . arguments [ 0 ] . kind == ts . SyntaxKind . ObjectLiteralExpression )
144
- . map ( expr => < ts . ObjectLiteralExpression > expr . arguments [ 0 ] ) ;
144
+ . map ( expr => expr . arguments [ 0 ] as ts . ObjectLiteralExpression ) ;
145
145
}
146
146
147
147
@@ -184,14 +184,14 @@ function _addSymbolToNgModuleMetadata(ngModulePath: string, metadataField: strin
184
184
return metadata . toPromise ( ) ;
185
185
}
186
186
187
- const assignment = < ts . PropertyAssignment > matchingProperties [ 0 ] ;
187
+ const assignment = matchingProperties [ 0 ] as ts . PropertyAssignment ;
188
188
189
189
// If it's not an array, nothing we can do really.
190
190
if ( assignment . initializer . kind !== ts . SyntaxKind . ArrayLiteralExpression ) {
191
191
return null ;
192
192
}
193
193
194
- const arrLiteral = < ts . ArrayLiteralExpression > assignment . initializer ;
194
+ const arrLiteral = assignment . initializer as ts . ArrayLiteralExpression ;
195
195
if ( arrLiteral . elements . length == 0 ) {
196
196
// Forward the property.
197
197
return arrLiteral ;
@@ -200,11 +200,17 @@ function _addSymbolToNgModuleMetadata(ngModulePath: string, metadataField: strin
200
200
} )
201
201
. then ( ( node : ts . Node ) => {
202
202
if ( ! node ) {
203
- /* eslint-disable no-console */
204
203
console . log ( 'No app module found. Please add your new class to your component.' ) ;
205
204
return new NoopChange ( ) ;
206
205
}
206
+
207
207
if ( Array . isArray ( node ) ) {
208
+ const nodeArray = node as any as Array < ts . Node > ;
209
+ const symbolsArray = nodeArray . map ( node => node . getText ( ) ) ;
210
+ if ( symbolsArray . includes ( symbolName ) ) {
211
+ return new NoopChange ( ) ;
212
+ }
213
+
208
214
node = node [ node . length - 1 ] ;
209
215
}
210
216
@@ -213,7 +219,7 @@ function _addSymbolToNgModuleMetadata(ngModulePath: string, metadataField: strin
213
219
if ( node . kind == ts . SyntaxKind . ObjectLiteralExpression ) {
214
220
// We haven't found the field in the metadata declaration. Insert a new
215
221
// field.
216
- let expr = < ts . ObjectLiteralExpression > node ;
222
+ let expr = node as ts . ObjectLiteralExpression ;
217
223
if ( expr . properties . length == 0 ) {
218
224
position = expr . getEnd ( ) - 1 ;
219
225
toInsert = ` ${ metadataField } : [${ symbolName } ]\n` ;
@@ -281,7 +287,7 @@ export function addProviderToModule(modulePath: string, classifiedName: string,
281
287
* Custom function to insert an export into NgModule. It also imports it.
282
288
*/
283
289
export function addExportToModule ( modulePath : string , classifiedName : string ,
284
- importPath : string ) : Promise < Change > {
290
+ importPath : string ) : Promise < Change > {
285
291
return _addSymbolToNgModuleMetadata ( modulePath , 'exports' , classifiedName , importPath ) ;
286
292
}
287
293
0 commit comments