Skip to content

Commit 4f2a20d

Browse files
timdeschryverdgp1130
authored andcommitted
fix(@schematics/angular): add new route before wildcard route
(cherry picked from commit 3784859)
1 parent b8a2215 commit 4f2a20d

File tree

2 files changed

+63
-11
lines changed

2 files changed

+63
-11
lines changed

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

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -649,20 +649,38 @@ export function addRouteDeclarationToModule(
649649
routesArr = findNodes(routesVar, ts.SyntaxKind.ArrayLiteralExpression, 1)[0] as ts.ArrayLiteralExpression;
650650
}
651651

652-
const occurencesCount = routesArr.elements.length;
652+
const occurrencesCount = routesArr.elements.length;
653653
const text = routesArr.getFullText(source);
654654

655655
let route: string = routeLiteral;
656-
if (occurencesCount > 0) {
657-
const identation = text.match(/\r?\n(\r?)\s*/) || [];
658-
route = `,${identation[0] || ' '}${routeLiteral}`;
656+
let insertPos = routesArr.elements.pos;
657+
658+
if (occurrencesCount > 0) {
659+
const lastRouteLiteral = [...routesArr.elements].pop() as ts.Expression;
660+
const lastRouteIsWildcard = ts.isObjectLiteralExpression(lastRouteLiteral)
661+
&& lastRouteLiteral
662+
.properties
663+
.some(n => (
664+
ts.isPropertyAssignment(n)
665+
&& ts.isIdentifier(n.name)
666+
&& n.name.text === 'path'
667+
&& ts.isStringLiteral(n.initializer)
668+
&& n.initializer.text === '**'
669+
));
670+
671+
const indentation = text.match(/\r?\n(\r?)\s*/) || [];
672+
const routeText = `${indentation[0] || ' '}${routeLiteral}`;
673+
674+
// Add the new route before the wildcard route
675+
// otherwise we'll always redirect to the wildcard route
676+
if (lastRouteIsWildcard) {
677+
insertPos = lastRouteLiteral.pos;
678+
route = `${routeText},`;
679+
} else {
680+
insertPos = lastRouteLiteral.end;
681+
route = `,${routeText}`;
682+
}
659683
}
660684

661-
return insertAfterLastOccurrence(
662-
routesArr.elements as unknown as ts.Node[],
663-
route,
664-
fileToAdd,
665-
routesArr.elements.pos,
666-
ts.SyntaxKind.ObjectLiteralExpression,
667-
);
685+
return new InsertChange(fileToAdd, insertPos, route);
668686
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,40 @@ describe('ast utils', () => {
440440
);
441441
});
442442

443+
it('should add a route before the last wildcard path', () => {
444+
const moduleContent = `
445+
import { BrowserModule } from '@angular/platform-browser';
446+
import { NgModule } from '@angular/core';
447+
import { AppComponent } from './app.component';
448+
const routes = [
449+
{ path: '**', component: FooComponent }
450+
];
451+
@NgModule({
452+
declarations: [
453+
AppComponent
454+
],
455+
imports: [
456+
BrowserModule,
457+
RouterModule.forRoot(routes)
458+
],
459+
bootstrap: [AppComponent]
460+
})
461+
export class AppModule { }
462+
`;
463+
464+
const source = getTsSource(modulePath, moduleContent);
465+
const changes = addRouteDeclarationToModule(
466+
source,
467+
'./src/app',
468+
`{ path: 'bar', component: BarComponent }`,
469+
);
470+
const output = applyChanges(modulePath, moduleContent, [changes]);
471+
472+
expect(output).toMatch(
473+
/const routes = \[\r?\n?\s*{ path: 'bar', component: BarComponent },\r?\n?\s*{ path: '\*\*', component: FooComponent }\r?\n?\s*\]/,
474+
);
475+
});
476+
443477
it('should add a route to the routes to the correct array when having guards', () => {
444478
const moduleContent = `
445479
import { BrowserModule } from '@angular/platform-browser';

0 commit comments

Comments
 (0)