Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: throw if parent and child routes have the same name #2267

Merged
merged 2 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions packages/router/__tests__/matcher/addingRemoving.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,52 @@ describe('Matcher: adding and removing records', () => {
})
})

it('throws if a parent and child have the same name', () => {
expect(() => {
createRouterMatcher(
[
{
path: '/',
component,
name: 'home',
children: [{ path: '/home', component, name: 'home' }],
},
],
{}
)
}).toThrowError(
'A route named "home" has been added as a child of a route with the same name'
)
})

it('throws if an ancestor and descendant have the same name', () => {
const name = Symbol('home')
const matcher = createRouterMatcher(
[
{
path: '/',
name,
children: [
{
path: 'home',
name: 'other',
component,
},
],
},
],
{}
)

const parent = matcher.getRecordMatcher('other')

expect(() => {
matcher.addRoute({ path: '', component, name }, parent)
}).toThrowError(
'A route named "Symbol(home)" has been added as a descendant of a route with the same name'
)
})

it('adds empty paths as children', () => {
const matcher = createRouterMatcher([], {})
matcher.addRoute({ path: '/', component, name: 'parent' })
Expand Down
21 changes: 20 additions & 1 deletion packages/router/src/matcher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,12 @@ export function createRouterMatcher(

// remove the route if named and only for the top record (avoid in nested calls)
// this works because the original record is the first one
if (isRootAdd && record.name && !isAliasRecord(matcher))
if (isRootAdd && record.name && !isAliasRecord(matcher)) {
if (__DEV__) {
checkSameNameAsAncestor(record, parent)
}
removeRoute(record.name)
}
}

// Avoid adding a record that doesn't display anything. This allows passing through records without a component to
Expand Down Expand Up @@ -499,6 +503,21 @@ function checkChildMissingNameWithEmptyPath(
}
}

function checkSameNameAsAncestor(
record: RouteRecordRaw,
parent?: RouteRecordMatcher
) {
for (let ancestor = parent; ancestor; ancestor = ancestor.parent) {
if (ancestor.record.name === record.name) {
throw new Error(
`A route named "${String(record.name)}" has been added as a ${
parent === ancestor ? 'child' : 'descendant'
} of a route with the same name. Route names must be unique and a nested route cannot use the same name as an ancestor.`
)
}
}
}

function checkMissingParamsInAbsolutePath(
record: RouteRecordMatcher,
parent: RouteRecordMatcher
Expand Down