1
1
import * as ts from 'typescript' ;
2
2
import * as fs from 'fs' ;
3
- import { Change , InsertChange , NoopChange , MultiChange } from './change' ;
4
- import { findNodes } from './node' ;
5
- import { insertImport } from './route-utils' ;
6
-
7
- import { Observable } from 'rxjs/Observable' ;
8
- import { ReplaySubject } from 'rxjs/ReplaySubject' ;
3
+ import { Change , InsertChange , NoopChange , MultiChange } from './change' ;
4
+ import { findNodes } from './node' ;
5
+ import { insertImport } from './route-utils' ;
6
+ import { Observable } from 'rxjs/Observable' ;
7
+ import { ReplaySubject } from 'rxjs/ReplaySubject' ;
9
8
import 'rxjs/add/observable/empty' ;
10
9
import 'rxjs/add/observable/of' ;
11
10
import 'rxjs/add/operator/do' ;
@@ -162,17 +161,17 @@ export function getDecoratorMetadata(source: ts.SourceFile, identifier: string,
162
161
return getSourceNodes ( source )
163
162
. filter ( node => {
164
163
return node . kind == ts . SyntaxKind . Decorator
165
- && ( < ts . Decorator > node ) . expression . kind == ts . SyntaxKind . CallExpression ;
164
+ && ( node as ts . Decorator ) . expression . kind == ts . SyntaxKind . CallExpression ;
166
165
} )
167
- . map ( node => < ts . CallExpression > ( < ts . Decorator > node ) . expression )
166
+ . map ( node => ( node as ts . Decorator ) . expression as ts . CallExpression )
168
167
. filter ( expr => {
169
168
if ( expr . expression . kind == ts . SyntaxKind . Identifier ) {
170
- const id = < ts . Identifier > expr . expression ;
169
+ const id = expr . expression as ts . Identifier ;
171
170
return id . getFullText ( source ) == identifier
172
171
&& angularImports [ id . getFullText ( source ) ] === module ;
173
172
} else if ( expr . expression . kind == ts . SyntaxKind . PropertyAccessExpression ) {
174
173
// This covers foo.NgModule when importing * as foo.
175
- const paExpr = < ts . PropertyAccessExpression > expr . expression ;
174
+ const paExpr = expr . expression as ts . PropertyAccessExpression ;
176
175
// If the left expression is not an identifier, just give up at that point.
177
176
if ( paExpr . expression . kind !== ts . SyntaxKind . Identifier ) {
178
177
return false ;
@@ -186,7 +185,7 @@ export function getDecoratorMetadata(source: ts.SourceFile, identifier: string,
186
185
} )
187
186
. filter ( expr => expr . arguments [ 0 ]
188
187
&& expr . arguments [ 0 ] . kind == ts . SyntaxKind . ObjectLiteralExpression )
189
- . map ( expr => < ts . ObjectLiteralExpression > expr . arguments [ 0 ] ) ;
188
+ . map ( expr => expr . arguments [ 0 ] as ts . ObjectLiteralExpression ) ;
190
189
}
191
190
192
191
@@ -229,14 +228,14 @@ function _addSymbolToNgModuleMetadata(ngModulePath: string, metadataField: strin
229
228
return metadata . toPromise ( ) ;
230
229
}
231
230
232
- const assignment = < ts . PropertyAssignment > matchingProperties [ 0 ] ;
231
+ const assignment = matchingProperties [ 0 ] as ts . PropertyAssignment ;
233
232
234
233
// If it's not an array, nothing we can do really.
235
234
if ( assignment . initializer . kind !== ts . SyntaxKind . ArrayLiteralExpression ) {
236
235
return null ;
237
236
}
238
237
239
- const arrLiteral = < ts . ArrayLiteralExpression > assignment . initializer ;
238
+ const arrLiteral = assignment . initializer as ts . ArrayLiteralExpression ;
240
239
if ( arrLiteral . elements . length == 0 ) {
241
240
// Forward the property.
242
241
return arrLiteral ;
@@ -245,11 +244,17 @@ function _addSymbolToNgModuleMetadata(ngModulePath: string, metadataField: strin
245
244
} )
246
245
. then ( ( node : ts . Node ) => {
247
246
if ( ! node ) {
248
- /* eslint-disable no-console */
249
247
console . log ( 'No app module found. Please add your new class to your component.' ) ;
250
248
return new NoopChange ( ) ;
251
249
}
250
+
252
251
if ( Array . isArray ( node ) ) {
252
+ const nodeArray = node as any as Array < ts . Node > ;
253
+ const symbolsArray = nodeArray . map ( node => node . getText ( ) ) ;
254
+ if ( symbolsArray . includes ( symbolName ) ) {
255
+ return new NoopChange ( ) ;
256
+ }
257
+
253
258
node = node [ node . length - 1 ] ;
254
259
}
255
260
@@ -258,7 +263,7 @@ function _addSymbolToNgModuleMetadata(ngModulePath: string, metadataField: strin
258
263
if ( node . kind == ts . SyntaxKind . ObjectLiteralExpression ) {
259
264
// We haven't found the field in the metadata declaration. Insert a new
260
265
// field.
261
- let expr = < ts . ObjectLiteralExpression > node ;
266
+ let expr = node as ts . ObjectLiteralExpression ;
262
267
if ( expr . properties . length == 0 ) {
263
268
position = expr . getEnd ( ) - 1 ;
264
269
toInsert = ` ${ metadataField } : [${ symbolName } ]\n` ;
@@ -326,7 +331,7 @@ export function addProviderToModule(modulePath: string, classifiedName: string,
326
331
* Custom function to insert an export into NgModule. It also imports it.
327
332
*/
328
333
export function addExportToModule ( modulePath : string , classifiedName : string ,
329
- importPath : string ) : Promise < Change > {
334
+ importPath : string ) : Promise < Change > {
330
335
return _addSymbolToNgModuleMetadata ( modulePath , 'exports' , classifiedName , importPath ) ;
331
336
}
332
337
0 commit comments