Skip to content

Commit 1bb8c9a

Browse files
cexbrayatKeen Yee Liau
authored and
Keen Yee Liau
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 5aa3c74 commit 1bb8c9a

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

packages/schematics/angular/guard/index.ts

+8-12
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
import { applyLintFix } from '../utility/lint-fix';
2323
import { parseName } from '../utility/parse-name';
2424
import { createDefaultPath } from '../utility/workspace';
25-
import { Schema as GuardOptions } from './schema';
25+
import { Implement as GuardInterface, Schema as GuardOptions } from './schema';
2626

2727

2828
export default function (options: GuardOptions): Rule {
@@ -31,19 +31,15 @@ export default function (options: GuardOptions): Rule {
3131
options.path = await createDefaultPath(host, options.project as string);
3232
}
3333

34-
if (options.implements === undefined) {
35-
options.implements = [];
34+
if (!options.implements) {
35+
throw new SchematicsException('Option "implements" is required.');
3636
}
3737

38-
let implementations = '';
39-
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-
}
38+
const implementations = options.implements.join(', ');
39+
let implementationImports = `${implementations}, `;
40+
// As long as we aren't in IE... ;)
41+
if (options.implements.includes(GuardInterface.CanLoad)) {
42+
implementationImports = `${implementationImports}Route, UrlSegment, `;
4743
}
4844

4945
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: undefined };
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)