From 7d7e048490e46f4d433ec6cf897468409d158c9b Mon Sep 17 00:00:00 2001 From: Ahmed El Sayegh Date: Fri, 30 Aug 2019 09:56:33 +0200 Subject: [PATCH] feat: warn about root paths without a leading slash (#2591) * fix(create-matcher): warn about root paths without a leading slash close #2550 * fix(create-route-map): warn about root paths without a leading slash close #2550 * fix(create-route-map): only warn about first route without slash * fix(create-route-map): handle case of '' route * fix(create-route-map): remove leftover console.log * fix(create-route-map): warn about root paths only * fix(create-route-map): show prettier warning message --- src/create-route-map.js | 12 ++++++++++++ test/unit/specs/create-map.spec.js | 25 +++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/create-route-map.js b/src/create-route-map.js index 0f790108c..aa51864a9 100644 --- a/src/create-route-map.js +++ b/src/create-route-map.js @@ -34,6 +34,18 @@ export function createRouteMap ( } } + if (process.env.NODE_ENV === 'development') { + // warn if routes do not include leading slashes + const found = pathList + // check for missing leading slash + .filter(path => path && path.charAt(0) !== '*' && path.charAt(0) !== '/') + + if (found.length > 0) { + const pathNames = found.map(path => `- ${path}`).join('\n') + warn(false, `Non-nested routes must include a leading slash character. Fix the following routes: \n${pathNames}`) + } + } + return { pathList, pathMap, diff --git a/test/unit/specs/create-map.spec.js b/test/unit/specs/create-map.spec.js index a7d09daff..25bdb3d45 100644 --- a/test/unit/specs/create-map.spec.js +++ b/test/unit/specs/create-map.spec.js @@ -138,6 +138,31 @@ describe('Creating Route Map', function () { ) }) + it('in development, warn if a path is missing a leading slash', function () { + process.env.NODE_ENV = 'development' + maps = createRouteMap([ + { path: 'bar', name: 'bar', component: Bar } + ]) + expect(console.warn).toHaveBeenCalledTimes(1) + expect(console.warn.calls.argsFor(0)[0]).toEqual('[vue-router] Non-nested routes must include a leading slash character. Fix the following routes: \n- bar') + }) + + it('in development, it does not log the missing leading slash when routes are valid', function () { + process.env.NODE_ENV = 'development' + maps = createRouteMap([ + { path: '/bar', name: 'bar', component: Bar } + ]) + expect(console.warn).not.toHaveBeenCalled() + }) + + it('in production, it does not log the missing leading slash warning', function () { + process.env.NODE_ENV = 'production' + maps = createRouteMap([ + { path: 'bar', name: 'bar', component: Bar } + ]) + expect(console.warn).not.toHaveBeenCalled() + }) + describe('path-to-regexp options', function () { const routes = [ { path: '/foo', name: 'foo', component: Foo },