From 77dc1ab675d6dfc59d0c5a47c81401b059fd8e4d Mon Sep 17 00:00:00 2001 From: Dzmitry Shylovich Date: Fri, 11 Nov 2016 01:55:10 +0300 Subject: [PATCH] fix(router): throw an error when encounter undefined route (#12389) --- modules/@angular/router/src/config.ts | 18 +++++++++++++++++- modules/@angular/router/src/router_module.ts | 1 - modules/@angular/router/test/config.spec.ts | 6 ++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/modules/@angular/router/src/config.ts b/modules/@angular/router/src/config.ts index 06ea315763..1c1df4b5db 100644 --- a/modules/@angular/router/src/config.ts +++ b/modules/@angular/router/src/config.ts @@ -350,10 +350,26 @@ export interface Route { } export function validateConfig(config: Routes): void { - config.forEach(validateNode); + // forEach doesn't iterate undefined values + for (let i = 0; i < config.length; i++) { + validateNode(config[i]); + } } function validateNode(route: Route): void { + if (!route) { + throw new Error(` + Invalid route configuration: Encountered undefined route. + The reason might be an extra comma. + + Example: + const routes: Routes = [ + { path: '', redirectTo: '/dashboard', pathMatch: 'full' }, + { path: 'dashboard', component: DashboardComponent },, << two commas + { path: 'detail/:id', component: HeroDetailComponent } + ]; + `); + } if (Array.isArray(route)) { throw new Error(`Invalid route configuration: Array cannot be specified`); } diff --git a/modules/@angular/router/src/router_module.ts b/modules/@angular/router/src/router_module.ts index 1c8d45e725..9144c820d6 100644 --- a/modules/@angular/router/src/router_module.ts +++ b/modules/@angular/router/src/router_module.ts @@ -8,7 +8,6 @@ import {APP_BASE_HREF, HashLocationStrategy, Location, LocationStrategy, PathLocationStrategy, PlatformLocation} from '@angular/common'; import {ANALYZE_FOR_ENTRY_COMPONENTS, APP_BOOTSTRAP_LISTENER, ApplicationRef, Compiler, Inject, Injector, ModuleWithProviders, NgModule, NgModuleFactoryLoader, OpaqueToken, Optional, Provider, SkipSelf, SystemJsNgModuleLoader} from '@angular/core'; - import {Route, Routes} from './config'; import {RouterLink, RouterLinkWithHref} from './directives/router_link'; import {RouterLinkActive} from './directives/router_link_active'; diff --git a/modules/@angular/router/test/config.spec.ts b/modules/@angular/router/test/config.spec.ts index c77c50bdc7..7774b7d72b 100644 --- a/modules/@angular/router/test/config.spec.ts +++ b/modules/@angular/router/test/config.spec.ts @@ -15,6 +15,12 @@ describe('config', () => { validateConfig([{path: 'a', redirectTo: 'b'}, {path: 'b', component: ComponentA}]); }); + it('should throw for undefined route', () => { + expect(() => { + validateConfig([{path: 'a', component: ComponentA}, , {path: 'b', component: ComponentB}]); + }).toThrowError(); + }); + it('should throw when Array is passed', () => { expect(() => { validateConfig([