Skip to content

Commit

Permalink
feat(warn): help migrating catch all routes
Browse files Browse the repository at this point in the history
  • Loading branch information
miyaoka authored and posva committed Oct 2, 2020
1 parent bae61ce commit 14e1eb9
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
18 changes: 18 additions & 0 deletions __tests__/matcher/addingRemoving.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@ describe('Matcher: adding and removing records', () => {
})
})

it('throws when adding *', () => {
const matcher = createRouterMatcher([], {})
expect(() => {
matcher.addRoute({ path: '*', component })
}).toThrowError('Catch all')
})

it('does not throw when adding * in children', () => {
const matcher = createRouterMatcher([], {})
expect(() => {
matcher.addRoute({
path: '/something',
component,
children: [{ path: '*', component }],
})
}).not.toThrow()
})

it('adds children', () => {
const matcher = createRouterMatcher([], {})
matcher.addRoute({ path: '/parent', component, name: 'home' })
Expand Down
4 changes: 4 additions & 0 deletions __tests__/matcher/pathParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ describe('Path parser', () => {
expect(tokenizePath('')).toEqual([[]])
})

it('not start with /', () => {
expect(() => tokenizePath('a')).toThrowError(`"a" should be "/a"`)
})

it('escapes :', () => {
expect(tokenizePath('/\\:')).toEqual([
[{ type: TokenType.Static, value: ':' }],
Expand Down
7 changes: 7 additions & 0 deletions src/matcher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ export function createRouterMatcher(
parent.record.path + (path && connectingSlash + path)
}

if (__DEV__ && normalizedRecord.path === '*') {
throw new Error(
'Catch all routes ("*") must now be defined using a param with a custom regexp.\n' +
'See more at https://next.router.vuejs.org/guide/migration/#removed-star-or-catch-all-routes.'
)
}

// create the object before hand so it can be passed to children
matcher = createRouteRecordMatcher(normalizedRecord, parent, options)

Expand Down
13 changes: 11 additions & 2 deletions src/matcher/pathTokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,18 @@ const VALID_PARAM_RE = /[a-zA-Z0-9_]/
export function tokenizePath(path: string): Array<Token[]> {
if (!path) return [[]]
if (path === '/') return [[ROOT_TOKEN]]
// // v3 catchAll must be renew
// if (/^\/?\*/.test(path))
// throw new Error(
// `Catch all routes (/*) must now be defined using a parameter with a custom regex: /:catchAll(.*)`
// )
// remove the leading slash
if (!path.startsWith('/'))
throw new Error(`Route "${path}" should be "/${path}".`)
if (__DEV__ && !path.startsWith('/')) {
throw new Error(
`Route path should start with a "/": "${path}" should be "/${path}". This will break in production.`
)
path = '/' + path
}

// if (tokenCache.has(path)) return tokenCache.get(path)!

Expand Down

0 comments on commit 14e1eb9

Please sign in to comment.