@@ -189,7 +189,6 @@ export function getSourceNodes(sourceFile: ts.SourceFile): ts.Node[] {
189189
190190export function findNode ( node : ts . Node , kind : ts . SyntaxKind , text : string ) : ts . Node | null {
191191 if ( node . kind === kind && node . getText ( ) === text ) {
192- // throw new Error(node.getText());
193192 return node ;
194193 }
195194
@@ -367,29 +366,28 @@ export function addSymbolToNgModuleMetadata(
367366 importPath : string | null = null ,
368367) : Change [ ] {
369368 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 ] ;
371370
372371 // Find the decorator declaration.
373- if ( ! node ) {
372+ if ( ! node || ! ts . isObjectLiteralExpression ( node ) ) {
374373 return [ ] ;
375374 }
376375
377376 // Get all the children property assignment of object literals.
378- const matchingProperties = getMetadataField ( node as ts . ObjectLiteralExpression , metadataField ) ;
377+ const matchingProperties = getMetadataField ( node , metadataField ) ;
379378
380379 if ( matchingProperties . length == 0 ) {
381380 // We haven't found the field in the metadata declaration. Insert a new field.
382- const expr = node as ts . ObjectLiteralExpression ;
383381 let position : number ;
384382 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 ;
387385 toInsert = `\n ${ metadataField } : [\n${ tags . indentBy ( 4 ) `${ symbolName } ` } \n ]\n` ;
388386 } 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 ( ) ;
391389 // Get the indentation of the last element, if any.
392- const text = node . getFullText ( source ) ;
390+ const text = childNode . getFullText ( source ) ;
393391 const matches = text . match ( / ^ ( \r ? \n ) ( \s * ) / ) ;
394392 if ( matches ) {
395393 toInsert =
@@ -408,47 +406,48 @@ export function addSymbolToNgModuleMetadata(
408406 return [ new InsertChange ( ngModulePath , position , toInsert ) ] ;
409407 }
410408 }
411- const assignment = matchingProperties [ 0 ] as ts . PropertyAssignment ;
409+ const assignment = matchingProperties [ 0 ] ;
412410
413411 // 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+ ) {
415416 return [ ] ;
416417 }
417418
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 ;
425422
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 ( ) } ` ) ;
429425 if ( symbolsArray . includes ( tags . oneLine `${ symbolName } ` ) ) {
430426 return [ ] ;
431427 }
432428
433- node = node [ node . length - 1 ] ;
429+ expresssion = elements [ elements . length - 1 ] ;
430+ } else {
431+ expresssion = assignmentInit ;
434432 }
435433
436434 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 ) ) {
439437 // We found the field but it's empty. Insert it just before the `]`.
440438 position -- ;
441439 toInsert = `\n${ tags . indentBy ( 4 ) `${ symbolName } ` } \n ` ;
442440 } else {
443441 // Get the indentation of the last element, if any.
444- const text = node . getFullText ( source ) ;
442+ const text = expresssion . getFullText ( source ) ;
445443 const matches = text . match ( / ^ ( \r ? \n ) ( \s * ) / ) ;
446444 if ( matches ) {
447445 toInsert = `,${ matches [ 1 ] } ${ tags . indentBy ( matches [ 2 ] . length ) `${ symbolName } ` } ` ;
448446 } else {
449447 toInsert = `, ${ symbolName } ` ;
450448 }
451449 }
450+
452451 if ( importPath !== null ) {
453452 return [
454453 new InsertChange ( ngModulePath , position , toInsert ) ,
@@ -604,9 +603,12 @@ export function getEnvironmentExportName(source: ts.SourceFile): string | null {
604603 */
605604export function getRouterModuleDeclaration ( source : ts . SourceFile ) : ts . Expression | undefined {
606605 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+ }
609610
611+ const matchingProperties = getMetadataField ( node , 'imports' ) ;
610612 if ( ! matchingProperties ) {
611613 return ;
612614 }
@@ -634,7 +636,10 @@ export function addRouteDeclarationToModule(
634636) : Change {
635637 const routerModuleExpr = getRouterModuleDeclaration ( source ) ;
636638 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+ ) ;
638643 }
639644 const scopeConfigMethodArgs = ( routerModuleExpr as ts . CallExpression ) . arguments ;
640645 if ( ! scopeConfigMethodArgs . length ) {
0 commit comments