Skip to content

Commit d05f78b

Browse files
atscottclydin
authored andcommitted
refactor(@schematics/angular): Use MaybeAsync and GuardResult types to reduce boilerplate
This refactor uses new types in the Router to reduce boilerplate.
1 parent 1db81fb commit d05f78b

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
11
import { Injectable } from '@angular/core';
22
import { <%= routerImports %> } from '@angular/router';
3-
import { Observable } from 'rxjs';
43

54
@Injectable({
65
providedIn: 'root'
76
})
87
export class <%= classify(name) %>Guard implements <%= implementations %> {
98
<% if (implements.includes('CanActivate')) { %>canActivate(
109
route: ActivatedRouteSnapshot,
11-
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
10+
state: RouterStateSnapshot): MaybeAsync<GuardResult> {
1211
return true;
1312
}
1413
<% } %><% if (implements.includes('CanActivateChild')) { %>canActivateChild(
1514
childRoute: ActivatedRouteSnapshot,
16-
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
15+
state: RouterStateSnapshot): MaybeAsync<GuardResult> {
1716
return true;
1817
}
1918
<% } %><% if (implements.includes('CanDeactivate')) { %>canDeactivate(
2019
component: unknown,
2120
currentRoute: ActivatedRouteSnapshot,
2221
currentState: RouterStateSnapshot,
23-
nextState?: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
22+
nextState?: RouterStateSnapshot): MaybeAsync<GuardResult> {
2423
return true;
2524
}
2625
<% } %><% if (implements.includes('CanMatch')) { %>canMatch(
2726
route: Route,
28-
segments: UrlSegment[]): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
27+
segments: UrlSegment[]): MaybeAsync<GuardResult> {
2928
return true;
3029
}<% } %>
3130
}

packages/schematics/angular/guard/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export default function (options: GuardOptions): Rule {
3131
.map((implement) => (implement === 'CanDeactivate' ? 'CanDeactivate<unknown>' : implement))
3232
.join(', ');
3333
const commonRouterNameImports = ['ActivatedRouteSnapshot', 'RouterStateSnapshot'];
34-
const routerNamedImports: string[] = [...options.implements, 'UrlTree'];
34+
const routerNamedImports: string[] = [...options.implements, 'MaybeAsync', 'GuardResult'];
3535

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

packages/schematics/angular/guard/index_spec.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ describe('Guard Schematic', () => {
143143
const options = { ...defaultOptions, implements: implementationOptions, functional: false };
144144
const tree = await schematicRunner.runSchematic('guard', options, appTree);
145145
const fileString = tree.readContent('/projects/bar/src/app/foo.guard.ts');
146-
const expectedImports = `import { CanMatch, Route, UrlSegment, UrlTree } from '@angular/router';`;
146+
const expectedImports = `import { CanMatch, GuardResult, MaybeAsync, Route, UrlSegment } from '@angular/router';`;
147147

148148
expect(fileString).toContain(expectedImports);
149149
});
@@ -153,7 +153,9 @@ describe('Guard Schematic', () => {
153153
const options = { ...defaultOptions, implements: implementationOptions, functional: false };
154154
const tree = await schematicRunner.runSchematic('guard', options, appTree);
155155
const fileString = tree.readContent('/projects/bar/src/app/foo.guard.ts');
156-
const expectedImports = `import { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot, UrlTree } from '@angular/router';`;
156+
const expectedImports =
157+
`import { ActivatedRouteSnapshot, CanActivate, GuardResult, ` +
158+
`MaybeAsync, RouterStateSnapshot } from '@angular/router';`;
157159

158160
expect(fileString).toContain(expectedImports);
159161
});
@@ -173,9 +175,8 @@ describe('Guard Schematic', () => {
173175
const tree = await schematicRunner.runSchematic('guard', options, appTree);
174176
const fileString = tree.readContent('/projects/bar/src/app/foo.guard.ts');
175177
const expectedImports =
176-
`import ` +
177-
`{ ActivatedRouteSnapshot, CanActivate, CanActivateChild, CanMatch, Route, RouterStateSnapshot, UrlSegment, UrlTree } ` +
178-
`from '@angular/router';`;
178+
`import { ActivatedRouteSnapshot, CanActivate, CanActivateChild, CanMatch, GuardResult, ` +
179+
`MaybeAsync, Route, RouterStateSnapshot, UrlSegment } from '@angular/router';`;
179180

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

0 commit comments

Comments
 (0)