Skip to content

Commit cd20e84

Browse files
committed
feat(@schematics/angular): default interface for guard
Currently, if the user hits `<Enter>` before selecting an interface to implement, the CLI generates a broken guard that implements no interface. With this commit, the CLI generates a `CanActivate` guard by default.
1 parent 32449fc commit cd20e84

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

packages/schematics/angular/guard/index.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import { strings } from '@angular-devkit/core';
99
import {
1010
Rule,
11-
SchematicsException,
1211
Tree,
1312
apply,
1413
applyTemplates,
@@ -37,13 +36,14 @@ export default function (options: GuardOptions): Rule {
3736

3837
let implementations = '';
3938
let implementationImports = '';
40-
if (options.implements.length > 0) {
41-
implementations = options.implements.join(', ');
42-
implementationImports = `${implementations}, `;
43-
// As long as we aren't in IE... ;)
44-
if (options.implements.includes('CanLoad')) {
45-
implementationImports = `${implementationImports}Route, UrlSegment, `;
46-
}
39+
if (options.implements.length === 0) {
40+
options.implements.push('CanActivate');
41+
}
42+
implementations = options.implements.join(', ');
43+
implementationImports = `${implementations}, `;
44+
// As long as we aren't in IE... ;)
45+
if (options.implements.includes('CanLoad')) {
46+
implementationImports = `${implementationImports}Route, UrlSegment, `;
4747
}
4848

4949
const parsedPath = parseName(options.path, options.name);

packages/schematics/angular/guard/index_spec.ts

+13
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,17 @@ describe('Guard Schematic', () => {
105105
expect(fileString).toContain(functionName);
106106
});
107107
});
108+
109+
it('should use CanActivate if no implements value', async () => {
110+
const options = { ...defaultOptions, implements: []};
111+
const tree = await schematicRunner.runSchematicAsync('guard', options, appTree)
112+
.toPromise();
113+
const fileString = tree.readContent('/projects/bar/src/app/foo.guard.ts');
114+
expect(fileString).toContain('CanActivate');
115+
expect(fileString).toContain('canActivate');
116+
expect(fileString).not.toContain('CanActivateChild');
117+
expect(fileString).not.toContain('canActivateChild');
118+
expect(fileString).not.toContain('CanLoad');
119+
expect(fileString).not.toContain('canLoad');
120+
});
108121
});

0 commit comments

Comments
 (0)