Skip to content

Commit c33cc0a

Browse files
atscottdgp1130
authored andcommitted
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 f837f6d)
1 parent 504ecef commit c33cc0a

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

packages/schematics/angular/guard/files/__name@dasherize__.guard.ts.template

+5-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ export class <%= classify(name) %>Guard implements <%= implementations %> {
2323
nextState?: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
2424
return true;
2525
}
26-
<% } %><% if (implements.includes('CanLoad')) { %>canLoad(
26+
<% } %><% if (implements.includes('CanMatch')) { %>canMatch(
27+
route: Route,
28+
segments: UrlSegment[]): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
29+
return true;
30+
}<% } %><% if (implements.includes('CanLoad')) { %>canLoad(
2731
route: Route,
2832
segments: UrlSegment[]): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
2933
return true;

packages/schematics/angular/guard/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ export default function (options: GuardOptions): Rule {
2121
const commonRouterNameImports = ['ActivatedRouteSnapshot', 'RouterStateSnapshot'];
2222
const routerNamedImports: string[] = [...options.implements, 'UrlTree'];
2323

24-
if (options.implements.includes(GuardInterface.CanLoad)) {
24+
if (
25+
options.implements.includes(GuardInterface.CanLoad) ||
26+
options.implements.includes(GuardInterface.CanMatch)
27+
) {
2528
routerNamedImports.push('Route', 'UrlSegment');
2629

2730
if (options.implements.length > 1) {

packages/schematics/angular/guard/index_spec.ts

+10
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,16 @@ describe('Guard Schematic', () => {
126126
expect(fileString).toContain(expectedImports);
127127
});
128128

129+
it('should add correct imports based on CanMatch implementation', async () => {
130+
const implementationOptions = ['CanMatch'];
131+
const options = { ...defaultOptions, implements: implementationOptions };
132+
const tree = await schematicRunner.runSchematicAsync('guard', options, appTree).toPromise();
133+
const fileString = tree.readContent('/projects/bar/src/app/foo.guard.ts');
134+
const expectedImports = `import { CanMatch, Route, UrlSegment, UrlTree } from '@angular/router';`;
135+
136+
expect(fileString).toContain(expectedImports);
137+
});
138+
129139
it('should add correct imports based on CanActivate implementation', async () => {
130140
const implementationOptions = ['CanActivate'];
131141
const options = { ...defaultOptions, implements: implementationOptions };

packages/schematics/angular/guard/schema.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"uniqueItems": true,
4848
"minItems": 1,
4949
"items": {
50-
"enum": ["CanActivate", "CanActivateChild", "CanDeactivate", "CanLoad"],
50+
"enum": ["CanActivate", "CanActivateChild", "CanDeactivate", "CanLoad", "CanMatch"],
5151
"type": "string"
5252
},
5353
"default": ["CanActivate"],

0 commit comments

Comments
 (0)