X Tutup
Skip to content

Commit 5298eb0

Browse files
committed
feat(router): enforce convention of CamelCase names in route aliases
Closes #4083
1 parent cb4a9a3 commit 5298eb0

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

modules/angular2/src/router/route_recognizer.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ export class RouteRecognizer {
4444
config(config: RouteDefinition): boolean {
4545
var handler;
4646

47+
if (isPresent(config.as) && config.as[0].toUpperCase() != config.as[0]) {
48+
var suggestedAlias = config.as[0].toUpperCase() + config.as.substring(1);
49+
throw new BaseException(
50+
`Route '${config.path}' with alias '${config.as}' does not begin with an uppercase letter. Route aliases should be CamelCase like '${suggestedAlias}'.`);
51+
}
52+
4753
if (config instanceof AuxRoute) {
4854
handler = new SyncRouteHandler(config.component, config.data);
4955
let path = config.path.startsWith('/') ? config.path.substring(1) : config.path;

modules/angular2/test/router/route_config_spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,20 @@ export function main() {
144144
async.done();
145145
return null;
146146
})}));
147+
148+
it('should throw if a config has an invalid alias name',
149+
inject(
150+
[AsyncTestCompleter],
151+
(async) => {
152+
bootstrap(BadAliasCmp,
153+
[bind(ROUTER_PRIMARY_COMPONENT).toValue(BadAliasCmp), testBindings])
154+
.catch((e) => {
155+
expect(e.originalException)
156+
.toContainError(
157+
`Route '/child' with alias 'child' does not begin with an uppercase letter. Route aliases should be CamelCase like 'Child'.`);
158+
async.done();
159+
return null;
160+
})}));
147161
});
148162
}
149163

@@ -201,6 +215,12 @@ class HierarchyAppCmp {
201215
class WrongConfigCmp {
202216
}
203217

218+
@Component({selector: 'app-cmp'})
219+
@View({template: `root { <router-outlet></router-outlet> }`, directives: ROUTER_DIRECTIVES})
220+
@RouteConfig([{path: '/child', component: HelloCmp, as: 'child'}])
221+
class BadAliasCmp {
222+
}
223+
204224
@Component({selector: 'app-cmp'})
205225
@View({template: `root { <router-outlet></router-outlet> }`, directives: ROUTER_DIRECTIVES})
206226
@RouteConfig([

modules/angular2/test/router/route_recognizer_spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ export function main() {
127127
});
128128

129129

130+
it('should throw if the route alias is not CamelCase', () => {
131+
expect(() => recognizer.config(
132+
new Route({path: 'app/user/:name', component: DummyCmpA, as: 'user'})))
133+
.toThrowError(
134+
`Route 'app/user/:name' with alias 'user' does not begin with an uppercase letter. Route aliases should be CamelCase like 'User'.`);
135+
});
136+
137+
130138
describe('params', () => {
131139
it('should recognize parameters within the URL path', () => {
132140
recognizer.config(new Route({path: 'profile/:name', component: DummyCmpA, as: 'User'}));

0 commit comments

Comments
 (0)
X Tutup