feat(router): enforce convention of CamelCase names in route aliases

Closes #4083
This commit is contained in:
Brian Ford 2015-09-14 16:01:09 -07:00
parent cb4a9a3c04
commit 5298eb0709
3 changed files with 34 additions and 0 deletions

View File

@ -44,6 +44,12 @@ export class RouteRecognizer {
config(config: RouteDefinition): boolean { config(config: RouteDefinition): boolean {
var handler; 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) { if (config instanceof AuxRoute) {
handler = new SyncRouteHandler(config.component, config.data); handler = new SyncRouteHandler(config.component, config.data);
let path = config.path.startsWith('/') ? config.path.substring(1) : config.path; let path = config.path.startsWith('/') ? config.path.substring(1) : config.path;

View File

@ -144,6 +144,20 @@ export function main() {
async.done(); async.done();
return null; 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 { class WrongConfigCmp {
} }
@Component({selector: 'app-cmp'})
@View({template: `root { <router-outlet></router-outlet> }`, directives: ROUTER_DIRECTIVES})
@RouteConfig([{path: '/child', component: HelloCmp, as: 'child'}])
class BadAliasCmp {
}
@Component({selector: 'app-cmp'}) @Component({selector: 'app-cmp'})
@View({template: `root { <router-outlet></router-outlet> }`, directives: ROUTER_DIRECTIVES}) @View({template: `root { <router-outlet></router-outlet> }`, directives: ROUTER_DIRECTIVES})
@RouteConfig([ @RouteConfig([

View File

@ -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', () => { describe('params', () => {
it('should recognize parameters within the URL path', () => { it('should recognize parameters within the URL path', () => {
recognizer.config(new Route({path: 'profile/:name', component: DummyCmpA, as: 'User'})); recognizer.config(new Route({path: 'profile/:name', component: DummyCmpA, as: 'User'}));