Skip to content

fix(@schematics/angular): ignore nested arrays in route declarations #15022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/schematics/angular/utility/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,8 +622,8 @@ export function addRouteDeclarationToModule(

// Check if the route declarations array is
// an inlined argument of RouterModule or a standalone variable
if (routesArg.kind === ts.SyntaxKind.ArrayLiteralExpression) {
routesArr = routesArg as ts.ArrayLiteralExpression;
if (ts.isArrayLiteralExpression(routesArg)) {
routesArr = routesArg;
} else {
const routesVarName = routesArg.getText();
let routesVar;
Expand All @@ -642,8 +642,8 @@ export function addRouteDeclarationToModule(
`to router module at line ${line} in ${fileToAdd}`,
);
}
const arrExpr = findNodes(routesVar, ts.SyntaxKind.ArrayLiteralExpression).pop();
routesArr = arrExpr as ts.ArrayLiteralExpression;

routesArr = findNodes(routesVar, ts.SyntaxKind.ArrayLiteralExpression, 1)[0] as ts.ArrayLiteralExpression;
}

const occurencesCount = routesArr.elements.length;
Expand Down
35 changes: 35 additions & 0 deletions packages/schematics/angular/utility/ast-utils_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,41 @@ describe('ast utils', () => {
);
});

it('should add a route to the routes to the correct array when having guards', () => {
const moduleContent = `
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';

const routes = [
{ path: 'foo', component: FooComponent, canLoad: [Guard] }
];

@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
RouterModule.forRoot(routes)
],
bootstrap: [AppComponent]
})
export class AppModule { }
`;

const source = getTsSource(modulePath, moduleContent);
const changes = addRouteDeclarationToModule(
source,
'./src/app', `{ path: 'bar', component: BarComponent }`,
);
const output = applyChanges(modulePath, moduleContent, [changes]);
expect(output).toMatch(
// tslint:disable-next-line:max-line-length
/const routes = \[\r?\n?\s*{ path: 'foo', component: FooComponent, canLoad: \[Guard\] },\r?\n?\s*{ path: 'bar', component: BarComponent }\r?\n?\s*\]/,
);
});

it('should add a route to the routes argument of RouteModule', () => {
const moduleContent = `
import { BrowserModule } from '@angular/platform-browser';
Expand Down