Skip to content

Commit 086f30c

Browse files
alan-agius4Keen Yee Liau
authored and
Keen Yee Liau
committed
fix(@schematics/angular): ignore nested arrays in route declarations
`findNodes` will continue looking for arrays recursively unless we specify that we specify that we want only the first array. Routes array can contain nested arrays such as when specifying guards. Fixes #15016
1 parent a22354b commit 086f30c

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

packages/schematics/angular/utility/ast-utils.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -622,8 +622,8 @@ export function addRouteDeclarationToModule(
622622

623623
// Check if the route declarations array is
624624
// an inlined argument of RouterModule or a standalone variable
625-
if (routesArg.kind === ts.SyntaxKind.ArrayLiteralExpression) {
626-
routesArr = routesArg as ts.ArrayLiteralExpression;
625+
if (ts.isArrayLiteralExpression(routesArg)) {
626+
routesArr = routesArg;
627627
} else {
628628
const routesVarName = routesArg.getText();
629629
let routesVar;
@@ -642,8 +642,8 @@ export function addRouteDeclarationToModule(
642642
`to router module at line ${line} in ${fileToAdd}`,
643643
);
644644
}
645-
const arrExpr = findNodes(routesVar, ts.SyntaxKind.ArrayLiteralExpression).pop();
646-
routesArr = arrExpr as ts.ArrayLiteralExpression;
645+
646+
routesArr = findNodes(routesVar, ts.SyntaxKind.ArrayLiteralExpression, 1)[0] as ts.ArrayLiteralExpression;
647647
}
648648

649649
const occurencesCount = routesArr.elements.length;

packages/schematics/angular/utility/ast-utils_spec.ts

+35
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,41 @@ describe('ast utils', () => {
444444
);
445445
});
446446

447+
it('should add a route to the routes to the correct array when having guards', () => {
448+
const moduleContent = `
449+
import { BrowserModule } from '@angular/platform-browser';
450+
import { NgModule } from '@angular/core';
451+
import { AppComponent } from './app.component';
452+
453+
const routes = [
454+
{ path: 'foo', component: FooComponent, canLoad: [Guard] }
455+
];
456+
457+
@NgModule({
458+
declarations: [
459+
AppComponent
460+
],
461+
imports: [
462+
BrowserModule,
463+
RouterModule.forRoot(routes)
464+
],
465+
bootstrap: [AppComponent]
466+
})
467+
export class AppModule { }
468+
`;
469+
470+
const source = getTsSource(modulePath, moduleContent);
471+
const changes = addRouteDeclarationToModule(
472+
source,
473+
'./src/app', `{ path: 'bar', component: BarComponent }`,
474+
);
475+
const output = applyChanges(modulePath, moduleContent, [changes]);
476+
expect(output).toMatch(
477+
// tslint:disable-next-line:max-line-length
478+
/const routes = \[\r?\n?\s*{ path: 'foo', component: FooComponent, canLoad: \[Guard\] },\r?\n?\s*{ path: 'bar', component: BarComponent }\r?\n?\s*\]/,
479+
);
480+
});
481+
447482
it('should add a route to the routes argument of RouteModule', () => {
448483
const moduleContent = `
449484
import { BrowserModule } from '@angular/platform-browser';

0 commit comments

Comments
 (0)