Skip to content

Commit

Permalink
fix(@schematics/angular): guard schematics should include all guards …
Browse files Browse the repository at this point in the history
…(CanMatch)

The `CanMatch` guard was added in v14.1 but not added
to the list of possible interfaces to implement.
This commit adds `CanMatch` to the list of possible
interfaces to implement. It has the same type imports
as the CanLoad interface.

(cherry picked from commit f837f6d)
  • Loading branch information
atscott authored and dgp1130 committed Nov 2, 2022
1 parent 7a40f87 commit 4b0ee8a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ export class <%= classify(name) %>Guard implements <%= implementations %> {
nextState?: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
return true;
}
<% } %><% if (implements.includes('CanLoad')) { %>canLoad(
<% } %><% if (implements.includes('CanMatch')) { %>canMatch(
route: Route,
segments: UrlSegment[]): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
return true;
}<% } %><% if (implements.includes('CanLoad')) { %>canLoad(
route: Route,
segments: UrlSegment[]): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
return true;
Expand Down
5 changes: 4 additions & 1 deletion packages/schematics/angular/guard/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ export default function (options: GuardOptions): Rule {
const commonRouterNameImports = ['ActivatedRouteSnapshot', 'RouterStateSnapshot'];
const routerNamedImports: string[] = [...options.implements, 'UrlTree'];

if (options.implements.includes(GuardInterface.CanLoad)) {
if (
options.implements.includes(GuardInterface.CanLoad) ||
options.implements.includes(GuardInterface.CanMatch)
) {
routerNamedImports.push('Route', 'UrlSegment');

if (options.implements.length > 1) {
Expand Down
10 changes: 10 additions & 0 deletions packages/schematics/angular/guard/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ describe('Guard Schematic', () => {
expect(fileString).toContain(expectedImports);
});

it('should add correct imports based on CanMatch implementation', async () => {
const implementationOptions = ['CanMatch'];
const options = { ...defaultOptions, implements: implementationOptions };
const tree = await schematicRunner.runSchematicAsync('guard', options, appTree).toPromise();
const fileString = tree.readContent('/projects/bar/src/app/foo.guard.ts');
const expectedImports = `import { CanMatch, Route, UrlSegment, UrlTree } from '@angular/router';`;

expect(fileString).toContain(expectedImports);
});

it('should add correct imports based on CanActivate implementation', async () => {
const implementationOptions = ['CanActivate'];
const options = { ...defaultOptions, implements: implementationOptions };
Expand Down
2 changes: 1 addition & 1 deletion packages/schematics/angular/guard/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"uniqueItems": true,
"minItems": 1,
"items": {
"enum": ["CanActivate", "CanActivateChild", "CanDeactivate", "CanLoad"],
"enum": ["CanActivate", "CanActivateChild", "CanDeactivate", "CanLoad", "CanMatch"],
"type": "string"
},
"default": ["CanActivate"],
Expand Down

0 comments on commit 4b0ee8a

Please sign in to comment.