Skip to content

Commit 9b3d508

Browse files
committed
fix(@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 forces a choice in interactive mode and generates a `CanActivate` guard by default.
1 parent 32449fc commit 9b3d508

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

packages/schematics/angular/guard/index.ts

+9-9
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,
@@ -22,7 +21,7 @@ import {
2221
import { applyLintFix } from '../utility/lint-fix';
2322
import { parseName } from '../utility/parse-name';
2423
import { createDefaultPath } from '../utility/workspace';
25-
import { Schema as GuardOptions } from './schema';
24+
import { Implement as GuardInterface, Schema as GuardOptions } from './schema';
2625

2726

2827
export default function (options: GuardOptions): Rule {
@@ -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(GuardInterface.CanActivate);
41+
}
42+
implementations = options.implements.join(', ');
43+
implementationImports = `${implementations}, `;
44+
// As long as we aren't in IE... ;)
45+
if (options.implements.includes(GuardInterface.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
});

packages/schematics/angular/guard/schema.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,16 @@
5555
"description": "Specifies which interfaces to implement.",
5656
"uniqueItems": true,
5757
"items": {
58-
"type": "string"
58+
"enum": [
59+
"CanActivate",
60+
"CanActivateChild",
61+
"CanLoad"
62+
]
5963
},
64+
"default": [
65+
"CanActivate"
66+
],
67+
"minItems": 1,
6068
"x-prompt": {
6169
"message": "Which interfaces would you like to implement?",
6270
"type": "list",

0 commit comments

Comments
 (0)