Skip to content

Commit bfc843b

Browse files
committed
fix: properly check for duplicated route names and paths
1 parent a8398a8 commit bfc843b

2 files changed

Lines changed: 18 additions & 8 deletions

File tree

modules/RouteNode.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,20 @@ export default class RouteNode {
6969
originalRoute = route;
7070
route = new RouteNode(route.name, route.path, route.children);
7171
}
72-
// Check duplicated routes
73-
if (this.children.map(child => child.name).indexOf(route.name) !== -1) {
74-
throw new Error(`Alias "${route.name}" is already defined in route node`);
75-
}
76-
// Check duplicated paths
77-
if (this.children.map(child => child.path).indexOf(route.path) !== -1) {
78-
throw new Error(`Path "${route.path}" is already defined in route node`);
79-
}
8072

8173
let names = route.name.split('.');
8274

8375
if (names.length === 1) {
76+
// Check duplicated routes
77+
if (this.children.map(child => child.name).indexOf(route.name) !== -1) {
78+
throw new Error(`Alias "${route.name}" is already defined in route node`);
79+
}
80+
81+
// Check duplicated paths
82+
if (this.children.map(child => child.path).indexOf(route.path) !== -1) {
83+
throw new Error(`Path "${route.path}" is already defined in route node`);
84+
}
85+
8486
this.children.push(route);
8587
// Push greedy spats to the bottom of the pile
8688
this.children.sort((left, right) => {

test/main.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ describe('RouteNode', function () {
9595
(function () {
9696
root.add({name: 'profile', path: '/home'})
9797
}).should.throw('Path "/home" is already defined in route node');
98+
99+
(function () {
100+
root.add({name: 'home.profile', path: '/home'})
101+
}).should.not.throw();
102+
103+
(function () {
104+
root.add({name: 'home.profile', path: '/profile'})
105+
}).should.throw();
98106
});
99107

100108
it('should throw an error when trying to add a route which parent doesn\'t exist', function () {

0 commit comments

Comments
 (0)