Skip to content

Commit

Permalink
feat(warn): detect missing param in nested absolute paths
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed May 1, 2020
1 parent 186e275 commit f5b5949
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
22 changes: 22 additions & 0 deletions __tests__/warnings.spec.ts
Expand Up @@ -49,6 +49,28 @@ describe('warnings', () => {
).toHaveBeenWarned()
})

it('warns if a child with absolute path is missing a parent param', async () => {
createRouter({
history: createMemoryHistory(),
routes: [
{
path: '/:a',
component,
children: [
{
path: ':b',
component,
children: [{ path: '/:a/b', component }],
},
],
},
],
})
expect(
`Absolute path "/:a/b" should have the exact same param named "b" as its parent "/:a/:b".`
).toHaveBeenWarned()
})

it('warns if an alias has a param with the same name but different', async () => {
createRouter({
history: createMemoryHistory(),
Expand Down
15 changes: 15 additions & 0 deletions src/matcher/index.ts
Expand Up @@ -102,6 +102,9 @@ export function createRouterMatcher(
// create the object before hand so it can be passed to children
matcher = createRouteRecordMatcher(normalizedRecord, parent, options)

if (__DEV__ && parent && path[0] === '/')
checkMissingParamsInAbsolutePath(matcher, parent)

// if we are an alias we must tell the original record that we exist
// so we can be removed
if (originalRecord) {
Expand Down Expand Up @@ -390,4 +393,16 @@ function checkSameParams(a: RouteRecordMatcher, b: RouteRecordMatcher) {
}
}

function checkMissingParamsInAbsolutePath(
record: RouteRecordMatcher,
parent: RouteRecordMatcher
) {
for (let key of parent.keys) {
if (!record.keys.find(isSameParam.bind(null, key)))
return warn(
`Absolute path "${record.record.path}" should have the exact same param named "${key.name}" as its parent "${parent.record.path}".`
)
}
}

export { PathParserOptions, _PathParserOptions }

0 comments on commit f5b5949

Please sign in to comment.