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' ;
@@ -81,7 +81,7 @@ function nodesByPosition(first: ts.Node, second: ts.Node): number {
81
81
* @throw Error if toInsert is first occurence but fall back is not set
82
82
*/
83
83
export function insertAfterLastOccurrence ( nodes : ts . Node [ ] , toInsert : string ,
84
- file : string , fallbackPos ?: number , syntaxKind ?: ts . SyntaxKind ) : Change {
84
+ file : string , fallbackPos ?: number , syntaxKind ?: ts . SyntaxKind ) : Change {
85
85
let lastItem = nodes . sort ( nodesByPosition ) . pop ( ) ;
86
86
if ( syntaxKind ) {
87
87
lastItem = findNodes ( lastItem , syntaxKind ) . sort ( nodesByPosition ) . pop ( ) ;
@@ -106,13 +106,13 @@ export function getContentOfKeyLiteral(source: ts.SourceFile, node: ts.Node): st
106
106
107
107
108
108
export function getDecoratorMetadata ( source : ts . SourceFile , identifier : string ,
109
- module : string ) : Observable < ts . Node > {
109
+ module : string ) : Observable < ts . Node > {
110
110
const symbols = new Symbols ( source as any ) ;
111
111
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
+ && ( < ts . Decorator > node ) . expression . kind == ts . SyntaxKind . CallExpression ;
116
116
} )
117
117
. map ( node => < ts . CallExpression > ( < ts . Decorator > node ) . expression )
118
118
. filter ( expr => {
@@ -140,13 +140,13 @@ export function getDecoratorMetadata(source: ts.SourceFile, identifier: string,
140
140
return false ;
141
141
} )
142
142
. filter ( expr => expr . arguments [ 0 ]
143
- && expr . arguments [ 0 ] . kind == ts . SyntaxKind . ObjectLiteralExpression )
143
+ && expr . arguments [ 0 ] . kind == ts . SyntaxKind . ObjectLiteralExpression )
144
144
. map ( expr => < ts . ObjectLiteralExpression > expr . arguments [ 0 ] ) ;
145
145
}
146
146
147
147
148
148
function _addSymbolToNgModuleMetadata ( ngModulePath : string , metadataField : string ,
149
- symbolName : string , importPath : string ) {
149
+ symbolName : string , importPath : string ) {
150
150
const source : ts . SourceFile = getSource ( ngModulePath ) ;
151
151
let metadata = getDecoratorMetadata ( source , 'NgModule' , '@angular/core' ) ;
152
152
@@ -200,10 +200,18 @@ 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
+ 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
+ }
214
+
207
215
if ( Array . isArray ( node ) ) {
208
216
node = node [ node . length - 1 ] ;
209
217
}
@@ -213,7 +221,7 @@ function _addSymbolToNgModuleMetadata(ngModulePath: string, metadataField: strin
213
221
if ( node . kind == ts . SyntaxKind . ObjectLiteralExpression ) {
214
222
// We haven't found the field in the metadata declaration. Insert a new
215
223
// field.
216
- let expr = < ts . ObjectLiteralExpression > node ;
224
+ let expr = node as ts . ObjectLiteralExpression ;
217
225
if ( expr . properties . length == 0 ) {
218
226
position = expr . getEnd ( ) - 1 ;
219
227
toInsert = ` ${ metadataField } : [${ symbolName } ]\n` ;
@@ -254,7 +262,7 @@ function _addSymbolToNgModuleMetadata(ngModulePath: string, metadataField: strin
254
262
* into NgModule declarations. It also imports the component.
255
263
*/
256
264
export function addDeclarationToModule ( modulePath : string , classifiedName : string ,
257
- importPath : string ) : Promise < Change > {
265
+ importPath : string ) : Promise < Change > {
258
266
259
267
return _addSymbolToNgModuleMetadata ( modulePath , 'declarations' , classifiedName , importPath ) ;
260
268
}
@@ -264,7 +272,7 @@ export function addDeclarationToModule(modulePath: string, classifiedName: strin
264
272
* into NgModule declarations. It also imports the component.
265
273
*/
266
274
export function addImportToModule ( modulePath : string , classifiedName : string ,
267
- importPath : string ) : Promise < Change > {
275
+ importPath : string ) : Promise < Change > {
268
276
269
277
return _addSymbolToNgModuleMetadata ( modulePath , 'imports' , classifiedName , importPath ) ;
270
278
}
@@ -273,15 +281,15 @@ export function addImportToModule(modulePath: string, classifiedName: string,
273
281
* Custom function to insert a provider into NgModule. It also imports it.
274
282
*/
275
283
export function addProviderToModule ( modulePath : string , classifiedName : string ,
276
- importPath : string ) : Promise < Change > {
284
+ importPath : string ) : Promise < Change > {
277
285
return _addSymbolToNgModuleMetadata ( modulePath , 'providers' , classifiedName , importPath ) ;
278
286
}
279
287
280
288
/**
281
289
* Custom function to insert an export into NgModule. It also imports it.
282
290
*/
283
291
export function addExportToModule ( modulePath : string , classifiedName : string ,
284
- importPath : string ) : Promise < Change > {
292
+ importPath : string ) : Promise < Change > {
285
293
return _addSymbolToNgModuleMetadata ( modulePath , 'exports' , classifiedName , importPath ) ;
286
294
}
287
295
0 commit comments