Skip to content

Commit

Permalink
fix(warn): should not warn missing optional params in aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Jan 15, 2021
1 parent bfc0489 commit 92f8901
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
21 changes: 18 additions & 3 deletions __tests__/matcher/addingRemoving.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,25 @@ describe('Matcher: adding and removing records', () => {
describe('warnings', () => {
mockWarn()

// TODO: add warnings for invalid records
it.skip('warns if alias is missing a required param', () => {
it('warns if alias is missing a required param', () => {
createRouterMatcher([{ path: '/:id', alias: '/no-id', component }], {})
expect('TODO').toHaveBeenWarned()
expect('same param named "id"').toHaveBeenWarned()
})

it('does not warn for optional param on alias', () => {
createRouterMatcher(
[{ path: '/:id', alias: '/:id-:suffix?', component }],
{}
)
expect('same param named').not.toHaveBeenWarned()
})

it('does not warn for optional param on main record', () => {
createRouterMatcher(
[{ alias: '/:id', path: '/:id-:suffix?', component }],
{}
)
expect('same param named').not.toHaveBeenWarned()
})
})
})
10 changes: 8 additions & 2 deletions src/matcher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,15 +425,21 @@ function isSameParam(a: ParamKey, b: ParamKey): boolean {
)
}

/**
* Check if a path and its alias have the same required params
*
* @param a - original record
* @param b - alias record
*/
function checkSameParams(a: RouteRecordMatcher, b: RouteRecordMatcher) {
for (let key of a.keys) {
if (!b.keys.find(isSameParam.bind(null, key)))
if (!key.optional && !b.keys.find(isSameParam.bind(null, key)))
return warn(
`Alias "${b.record.path}" and the original record: "${a.record.path}" should have the exact same param named "${key.name}"`
)
}
for (let key of b.keys) {
if (!a.keys.find(isSameParam.bind(null, key)))
if (!key.optional && !a.keys.find(isSameParam.bind(null, key)))
return warn(
`Alias "${b.record.path}" and the original record: "${a.record.path}" should have the exact same param named "${key.name}"`
)
Expand Down

0 comments on commit 92f8901

Please sign in to comment.