From 5298eb0709dd85081372e89c6f81bebdfaaefd94 Mon Sep 17 00:00:00 2001 From: Brian Ford Date: Mon, 14 Sep 2015 16:01:09 -0700 Subject: [PATCH] feat(router): enforce convention of CamelCase names in route aliases Closes #4083 --- .../angular2/src/router/route_recognizer.ts | 6 ++++++ .../angular2/test/router/route_config_spec.ts | 20 +++++++++++++++++++ .../test/router/route_recognizer_spec.ts | 8 ++++++++ 3 files changed, 34 insertions(+) diff --git a/modules/angular2/src/router/route_recognizer.ts b/modules/angular2/src/router/route_recognizer.ts index d7e709c1fa..d63bbf8079 100644 --- a/modules/angular2/src/router/route_recognizer.ts +++ b/modules/angular2/src/router/route_recognizer.ts @@ -44,6 +44,12 @@ export class RouteRecognizer { config(config: RouteDefinition): boolean { var handler; + if (isPresent(config.as) && config.as[0].toUpperCase() != config.as[0]) { + var suggestedAlias = config.as[0].toUpperCase() + config.as.substring(1); + throw new BaseException( + `Route '${config.path}' with alias '${config.as}' does not begin with an uppercase letter. Route aliases should be CamelCase like '${suggestedAlias}'.`); + } + if (config instanceof AuxRoute) { handler = new SyncRouteHandler(config.component, config.data); let path = config.path.startsWith('/') ? config.path.substring(1) : config.path; diff --git a/modules/angular2/test/router/route_config_spec.ts b/modules/angular2/test/router/route_config_spec.ts index 5a5c3af8cf..063de85727 100644 --- a/modules/angular2/test/router/route_config_spec.ts +++ b/modules/angular2/test/router/route_config_spec.ts @@ -144,6 +144,20 @@ export function main() { async.done(); return null; })})); + + it('should throw if a config has an invalid alias name', + inject( + [AsyncTestCompleter], + (async) => { + bootstrap(BadAliasCmp, + [bind(ROUTER_PRIMARY_COMPONENT).toValue(BadAliasCmp), testBindings]) + .catch((e) => { + expect(e.originalException) + .toContainError( + `Route '/child' with alias 'child' does not begin with an uppercase letter. Route aliases should be CamelCase like 'Child'.`); + async.done(); + return null; + })})); }); } @@ -201,6 +215,12 @@ class HierarchyAppCmp { class WrongConfigCmp { } +@Component({selector: 'app-cmp'}) +@View({template: `root { }`, directives: ROUTER_DIRECTIVES}) +@RouteConfig([{path: '/child', component: HelloCmp, as: 'child'}]) +class BadAliasCmp { +} + @Component({selector: 'app-cmp'}) @View({template: `root { }`, directives: ROUTER_DIRECTIVES}) @RouteConfig([ diff --git a/modules/angular2/test/router/route_recognizer_spec.ts b/modules/angular2/test/router/route_recognizer_spec.ts index fff6eb63e9..6ba8ea21c6 100644 --- a/modules/angular2/test/router/route_recognizer_spec.ts +++ b/modules/angular2/test/router/route_recognizer_spec.ts @@ -127,6 +127,14 @@ export function main() { }); + it('should throw if the route alias is not CamelCase', () => { + expect(() => recognizer.config( + new Route({path: 'app/user/:name', component: DummyCmpA, as: 'user'}))) + .toThrowError( + `Route 'app/user/:name' with alias 'user' does not begin with an uppercase letter. Route aliases should be CamelCase like 'User'.`); + }); + + describe('params', () => { it('should recognize parameters within the URL path', () => { recognizer.config(new Route({path: 'profile/:name', component: DummyCmpA, as: 'User'}));