From 4b0ee8ad15efcb513ab5d9e38bf9b1e08857e798 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Tue, 1 Nov 2022 10:50:41 -0700 Subject: [PATCH] fix(@schematics/angular): guard schematics should include all guards (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 f837f6dab46510346964c2ad6be64b68fac32a41) --- .../guard/files/__name@dasherize__.guard.ts.template | 6 +++++- packages/schematics/angular/guard/index.ts | 5 ++++- packages/schematics/angular/guard/index_spec.ts | 10 ++++++++++ packages/schematics/angular/guard/schema.json | 2 +- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/packages/schematics/angular/guard/files/__name@dasherize__.guard.ts.template b/packages/schematics/angular/guard/files/__name@dasherize__.guard.ts.template index 3dc36a017893..8d83bc7498b4 100644 --- a/packages/schematics/angular/guard/files/__name@dasherize__.guard.ts.template +++ b/packages/schematics/angular/guard/files/__name@dasherize__.guard.ts.template @@ -23,7 +23,11 @@ export class <%= classify(name) %>Guard implements <%= implementations %> { nextState?: RouterStateSnapshot): Observable | Promise | boolean | UrlTree { return true; } - <% } %><% if (implements.includes('CanLoad')) { %>canLoad( + <% } %><% if (implements.includes('CanMatch')) { %>canMatch( + route: Route, + segments: UrlSegment[]): Observable | Promise | boolean | UrlTree { + return true; + }<% } %><% if (implements.includes('CanLoad')) { %>canLoad( route: Route, segments: UrlSegment[]): Observable | Promise | boolean | UrlTree { return true; diff --git a/packages/schematics/angular/guard/index.ts b/packages/schematics/angular/guard/index.ts index f8a35b9947bf..efb377216684 100644 --- a/packages/schematics/angular/guard/index.ts +++ b/packages/schematics/angular/guard/index.ts @@ -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) { diff --git a/packages/schematics/angular/guard/index_spec.ts b/packages/schematics/angular/guard/index_spec.ts index eba8e654982b..45326eba1862 100644 --- a/packages/schematics/angular/guard/index_spec.ts +++ b/packages/schematics/angular/guard/index_spec.ts @@ -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 }; diff --git a/packages/schematics/angular/guard/schema.json b/packages/schematics/angular/guard/schema.json index f66bdc9428cc..fda5ea7a43a2 100644 --- a/packages/schematics/angular/guard/schema.json +++ b/packages/schematics/angular/guard/schema.json @@ -48,7 +48,7 @@ "uniqueItems": true, "minItems": 1, "items": { - "enum": ["CanActivate", "CanActivateChild", "CanDeactivate", "CanLoad"], + "enum": ["CanActivate", "CanActivateChild", "CanDeactivate", "CanLoad", "CanMatch"], "type": "string" }, "default": ["CanActivate"],