@@ -189,7 +189,6 @@ export function getSourceNodes(sourceFile: ts.SourceFile): ts.Node[] {
189
189
190
190
export function findNode ( node : ts . Node , kind : ts . SyntaxKind , text : string ) : ts . Node | null {
191
191
if ( node . kind === kind && node . getText ( ) === text ) {
192
- // throw new Error(node.getText());
193
192
return node ;
194
193
}
195
194
@@ -367,29 +366,28 @@ export function addSymbolToNgModuleMetadata(
367
366
importPath : string | null = null ,
368
367
) : Change [ ] {
369
368
const nodes = getDecoratorMetadata ( source , 'NgModule' , '@angular/core' ) ;
370
- let node : any = nodes [ 0 ] ; // eslint-disable-line @typescript-eslint/no-explicit-any
369
+ const node = nodes [ 0 ] ;
371
370
372
371
// Find the decorator declaration.
373
- if ( ! node ) {
372
+ if ( ! node || ! ts . isObjectLiteralExpression ( node ) ) {
374
373
return [ ] ;
375
374
}
376
375
377
376
// Get all the children property assignment of object literals.
378
- const matchingProperties = getMetadataField ( node as ts . ObjectLiteralExpression , metadataField ) ;
377
+ const matchingProperties = getMetadataField ( node , metadataField ) ;
379
378
380
379
if ( matchingProperties . length == 0 ) {
381
380
// We haven't found the field in the metadata declaration. Insert a new field.
382
- const expr = node as ts . ObjectLiteralExpression ;
383
381
let position : number ;
384
382
let toInsert : string ;
385
- if ( expr . properties . length == 0 ) {
386
- position = expr . getEnd ( ) - 1 ;
383
+ if ( node . properties . length == 0 ) {
384
+ position = node . getEnd ( ) - 1 ;
387
385
toInsert = `\n ${ metadataField } : [\n${ tags . indentBy ( 4 ) `${ symbolName } ` } \n ]\n` ;
388
386
} else {
389
- node = expr . properties [ expr . properties . length - 1 ] ;
390
- position = node . getEnd ( ) ;
387
+ const childNode = node . properties [ node . properties . length - 1 ] ;
388
+ position = childNode . getEnd ( ) ;
391
389
// Get the indentation of the last element, if any.
392
- const text = node . getFullText ( source ) ;
390
+ const text = childNode . getFullText ( source ) ;
393
391
const matches = text . match ( / ^ ( \r ? \n ) ( \s * ) / ) ;
394
392
if ( matches ) {
395
393
toInsert =
@@ -408,47 +406,48 @@ export function addSymbolToNgModuleMetadata(
408
406
return [ new InsertChange ( ngModulePath , position , toInsert ) ] ;
409
407
}
410
408
}
411
- const assignment = matchingProperties [ 0 ] as ts . PropertyAssignment ;
409
+ const assignment = matchingProperties [ 0 ] ;
412
410
413
411
// If it's not an array, nothing we can do really.
414
- if ( assignment . initializer . kind !== ts . SyntaxKind . ArrayLiteralExpression ) {
412
+ if (
413
+ ! ts . isPropertyAssignment ( assignment ) ||
414
+ ! ts . isArrayLiteralExpression ( assignment . initializer )
415
+ ) {
415
416
return [ ] ;
416
417
}
417
418
418
- const arrLiteral = assignment . initializer as ts . ArrayLiteralExpression ;
419
- if ( arrLiteral . elements . length == 0 ) {
420
- // Forward the property.
421
- node = arrLiteral ;
422
- } else {
423
- node = arrLiteral . elements ;
424
- }
419
+ let expresssion : ts . Expression | ts . ArrayLiteralExpression ;
420
+ const assignmentInit = assignment . initializer ;
421
+ const elements = assignmentInit . elements ;
425
422
426
- if ( Array . isArray ( node ) ) {
427
- const nodeArray = ( node as { } ) as Array < ts . Node > ;
428
- const symbolsArray = nodeArray . map ( ( node ) => tags . oneLine `${ node . getText ( ) } ` ) ;
423
+ if ( elements . length ) {
424
+ const symbolsArray = elements . map ( ( node ) => tags . oneLine `${ node . getText ( ) } ` ) ;
429
425
if ( symbolsArray . includes ( tags . oneLine `${ symbolName } ` ) ) {
430
426
return [ ] ;
431
427
}
432
428
433
- node = node [ node . length - 1 ] ;
429
+ expresssion = elements [ elements . length - 1 ] ;
430
+ } else {
431
+ expresssion = assignmentInit ;
434
432
}
435
433
436
434
let toInsert : string ;
437
- let position = node . getEnd ( ) ;
438
- if ( node . kind == ts . SyntaxKind . ArrayLiteralExpression ) {
435
+ let position = expresssion . getEnd ( ) ;
436
+ if ( ts . isArrayLiteralExpression ( expresssion ) ) {
439
437
// We found the field but it's empty. Insert it just before the `]`.
440
438
position -- ;
441
439
toInsert = `\n${ tags . indentBy ( 4 ) `${ symbolName } ` } \n ` ;
442
440
} else {
443
441
// Get the indentation of the last element, if any.
444
- const text = node . getFullText ( source ) ;
442
+ const text = expresssion . getFullText ( source ) ;
445
443
const matches = text . match ( / ^ ( \r ? \n ) ( \s * ) / ) ;
446
444
if ( matches ) {
447
445
toInsert = `,${ matches [ 1 ] } ${ tags . indentBy ( matches [ 2 ] . length ) `${ symbolName } ` } ` ;
448
446
} else {
449
447
toInsert = `, ${ symbolName } ` ;
450
448
}
451
449
}
450
+
452
451
if ( importPath !== null ) {
453
452
return [
454
453
new InsertChange ( ngModulePath , position , toInsert ) ,
@@ -604,9 +603,12 @@ export function getEnvironmentExportName(source: ts.SourceFile): string | null {
604
603
*/
605
604
export function getRouterModuleDeclaration ( source : ts . SourceFile ) : ts . Expression | undefined {
606
605
const result = getDecoratorMetadata ( source , 'NgModule' , '@angular/core' ) ;
607
- const node = result [ 0 ] as ts . ObjectLiteralExpression ;
608
- const matchingProperties = getMetadataField ( node , 'imports' ) ;
606
+ const node = result [ 0 ] ;
607
+ if ( ! node || ! ts . isObjectLiteralExpression ( node ) ) {
608
+ return undefined ;
609
+ }
609
610
611
+ const matchingProperties = getMetadataField ( node , 'imports' ) ;
610
612
if ( ! matchingProperties ) {
611
613
return ;
612
614
}
@@ -634,7 +636,10 @@ export function addRouteDeclarationToModule(
634
636
) : Change {
635
637
const routerModuleExpr = getRouterModuleDeclaration ( source ) ;
636
638
if ( ! routerModuleExpr ) {
637
- throw new Error ( `Couldn't find a route declaration in ${ fileToAdd } .` ) ;
639
+ throw new Error (
640
+ `Couldn't find a route declaration in ${ fileToAdd } .\n` +
641
+ `Use the '--module' option to specify a different routing module.` ,
642
+ ) ;
638
643
}
639
644
const scopeConfigMethodArgs = ( routerModuleExpr as ts . CallExpression ) . arguments ;
640
645
if ( ! scopeConfigMethodArgs . length ) {
0 commit comments