File tree Expand file tree Collapse file tree 2 files changed +66
-1
lines changed Expand file tree Collapse file tree 2 files changed +66
-1
lines changed Original file line number Diff line number Diff line change @@ -404,6 +404,52 @@ describe('Matcher: adding and removing records', () => {
404404 } )
405405 } )
406406
407+ it ( 'throws if a parent and child have the same name' , ( ) => {
408+ expect ( ( ) => {
409+ createRouterMatcher (
410+ [
411+ {
412+ path : '/' ,
413+ component,
414+ name : 'home' ,
415+ children : [ { path : '/home' , component, name : 'home' } ] ,
416+ } ,
417+ ] ,
418+ { }
419+ )
420+ } ) . toThrowError (
421+ 'A route named "home" has been added as a child of a route with the same name'
422+ )
423+ } )
424+
425+ it ( 'throws if an ancestor and descendant have the same name' , ( ) => {
426+ const name = Symbol ( 'home' )
427+ const matcher = createRouterMatcher (
428+ [
429+ {
430+ path : '/' ,
431+ name,
432+ children : [
433+ {
434+ path : 'home' ,
435+ name : 'other' ,
436+ component,
437+ } ,
438+ ] ,
439+ } ,
440+ ] ,
441+ { }
442+ )
443+
444+ const parent = matcher . getRecordMatcher ( 'other' )
445+
446+ expect ( ( ) => {
447+ matcher . addRoute ( { path : '' , component, name } , parent )
448+ } ) . toThrowError (
449+ 'A route named "Symbol(home)" has been added as a descendant of a route with the same name'
450+ )
451+ } )
452+
407453 it ( 'adds empty paths as children' , ( ) => {
408454 const matcher = createRouterMatcher ( [ ] , { } )
409455 matcher . addRoute ( { path : '/' , component, name : 'parent' } )
Original file line number Diff line number Diff line change @@ -159,8 +159,12 @@ export function createRouterMatcher(
159159
160160 // remove the route if named and only for the top record (avoid in nested calls)
161161 // this works because the original record is the first one
162- if ( isRootAdd && record . name && ! isAliasRecord ( matcher ) )
162+ if ( isRootAdd && record . name && ! isAliasRecord ( matcher ) ) {
163+ if ( __DEV__ ) {
164+ checkSameNameAsAncestor ( record , parent )
165+ }
163166 removeRoute ( record . name )
167+ }
164168 }
165169
166170 // Avoid adding a record that doesn't display anything. This allows passing through records without a component to
@@ -529,6 +533,21 @@ function checkChildMissingNameWithEmptyPath(
529533 }
530534}
531535
536+ function checkSameNameAsAncestor (
537+ record : RouteRecordRaw ,
538+ parent ?: RouteRecordMatcher
539+ ) {
540+ for ( let ancestor = parent ; ancestor ; ancestor = ancestor . parent ) {
541+ if ( ancestor . record . name === record . name ) {
542+ throw new Error (
543+ `A route named "${ String ( record . name ) } " has been added as a ${
544+ parent === ancestor ? 'child' : 'descendant'
545+ } of a route with the same name. Route names must be unique and a nested route cannot use the same name as an ancestor.`
546+ )
547+ }
548+ }
549+ }
550+
532551function checkMissingParamsInAbsolutePath (
533552 record : RouteRecordMatcher ,
534553 parent : RouteRecordMatcher
You can’t perform that action at this time.
0 commit comments