From: Masaya Kazama Date: Wed, 30 Sep 2020 11:47:17 +0000 (+0900) Subject: feat(warn): help migrating catch all routes X-Git-Tag: v4.0.0-beta.13~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=14e1eb96485f74669f582a87f522d3b13b567c9c;p=thirdparty%2Fvuejs%2Frouter.git feat(warn): help migrating catch all routes --- diff --git a/__tests__/matcher/addingRemoving.spec.ts b/__tests__/matcher/addingRemoving.spec.ts index a3ebabb3..6130754f 100644 --- a/__tests__/matcher/addingRemoving.spec.ts +++ b/__tests__/matcher/addingRemoving.spec.ts @@ -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' }) diff --git a/__tests__/matcher/pathParser.spec.ts b/__tests__/matcher/pathParser.spec.ts index c3355ea6..b897243b 100644 --- a/__tests__/matcher/pathParser.spec.ts +++ b/__tests__/matcher/pathParser.spec.ts @@ -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: ':' }], diff --git a/src/matcher/index.ts b/src/matcher/index.ts index de015091..cb5cc8cd 100644 --- a/src/matcher/index.ts +++ b/src/matcher/index.ts @@ -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) diff --git a/src/matcher/pathTokenizer.ts b/src/matcher/pathTokenizer.ts index f5913d3d..92b0cbaa 100644 --- a/src/matcher/pathTokenizer.ts +++ b/src/matcher/pathTokenizer.ts @@ -46,9 +46,18 @@ const VALID_PARAM_RE = /[a-zA-Z0-9_]/ export function tokenizePath(path: string): Array { 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)!