From b1c178c0c4cad319dfdb905b67a478f6803f221a Mon Sep 17 00:00:00 2001 From: Ahmed El Sayegh Date: Tue, 22 Jan 2019 03:06:07 +0200 Subject: [PATCH 1/7] fix(create-matcher): warn about root paths without a leading slash close #2550 --- src/create-matcher.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/create-matcher.js b/src/create-matcher.js index 07987c431..3381afaf2 100644 --- a/src/create-matcher.js +++ b/src/create-matcher.js @@ -18,6 +18,11 @@ export function createMatcher ( router: VueRouter ): Matcher { const { pathList, pathMap, nameMap } = createRouteMap(routes) + const pathsMissingSlashes = pathList.filter(path => !!path && path.charAt(0) !== '/') + + if (pathsMissingSlashes.length > 0) { + warn(false, `There is at least one root route without a leading slash in its path.`) + } function addRoutes (routes) { createRouteMap(routes, pathList, pathMap, nameMap) From 04161915c184294489cdb6cf992ec792ac38e5f9 Mon Sep 17 00:00:00 2001 From: Ahmed El Sayegh Date: Tue, 22 Jan 2019 14:05:56 +0200 Subject: [PATCH 2/7] fix(create-route-map): warn about root paths without a leading slash close #2550 --- src/create-matcher.js | 5 ---- src/create-route-map.js | 10 +++++++ test/unit/specs/create-map.spec.js | 42 ++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/src/create-matcher.js b/src/create-matcher.js index 3381afaf2..07987c431 100644 --- a/src/create-matcher.js +++ b/src/create-matcher.js @@ -18,11 +18,6 @@ export function createMatcher ( router: VueRouter ): Matcher { const { pathList, pathMap, nameMap } = createRouteMap(routes) - const pathsMissingSlashes = pathList.filter(path => !!path && path.charAt(0) !== '/') - - if (pathsMissingSlashes.length > 0) { - warn(false, `There is at least one root route without a leading slash in its path.`) - } function addRoutes (routes) { createRouteMap(routes, pathList, pathMap, nameMap) diff --git a/src/create-route-map.js b/src/create-route-map.js index 560e05fdd..74714f9be 100644 --- a/src/create-route-map.js +++ b/src/create-route-map.js @@ -34,6 +34,16 @@ export function createRouteMap ( } } + // warn if routes do not include leading slashes + const pathsMissingSlashes = pathList + .filter(path => !!path && path.charAt(0) !== '*' && path.charAt(0) !== '/') + .map(path => path.split('/')[0]) + .filter((path, index, pathList) => pathList.indexOf(path) === index) + + if (pathsMissingSlashes.length > 0) { + warn(false, `The following routes require a leading slash in their paths: ${pathsMissingSlashes.join(', ')}`) + } + return { pathList, pathMap, diff --git a/test/unit/specs/create-map.spec.js b/test/unit/specs/create-map.spec.js index 67fba131a..4a0a3c1b9 100644 --- a/test/unit/specs/create-map.spec.js +++ b/test/unit/specs/create-map.spec.js @@ -97,6 +97,48 @@ describe('Creating Route Map', function () { expect(console.warn.calls.argsFor(0)[0]).toMatch('vue-router] Duplicate param keys in route with path: "/foo/:id/bar/:id"') }) + it('in development, warn if a path is missing a leading slash', function () { + process.env.NODE_ENV = 'development' + maps = createRouteMap([ + { path: '/', name: 'home', component: Home }, + { path: 'bar', name: 'bar', component: Bar }, + { path: 'foo', name: 'foo', component: Foo, + children: [ + { path: 'bar/:id', component: Bar } + ] + }, + { path: '*', name: 'any', component: Baz } + ]) + expect(console.warn).toHaveBeenCalledTimes(1) + expect(console.warn.calls.argsFor(0)[0]).toEqual('[vue-router] The following routes require a leading slash in their paths: bar, foo') + }) + + it('in development, it has not logged a missing leading slash warning when all paths have slashes', function () { + process.env.NODE_ENV = 'development' + maps = createRouteMap([ + { path: '/', name: 'home', component: Home }, + { path: '/bar', name: 'bar', component: Bar }, + { path: '/foo', name: 'foo', component: Foo, + children: [ + { path: 'bar/:id', component: Bar } + ] + }, + { path: '*', name: 'any', component: Baz } + ]) + expect(console.warn).not.toHaveBeenCalled() + }) + + it('in production, it has not logged a missing leading slash warning', function () { + process.env.NODE_ENV = 'production' + maps = createRouteMap([ + { path: '/', name: 'home', component: Home }, + { path: 'bar', name: 'bar', component: Bar }, + { path: 'foo', name: 'foo', component: Foo }, + { path: '*', name: 'any', component: Baz } + ]) + expect(console.warn).not.toHaveBeenCalled() + }) + describe('path-to-regexp options', function () { const routes = [ { path: '/foo', name: 'foo', component: Foo }, From 6121c82773d13bdc51d180a7d6b4d10a3ffe89bc Mon Sep 17 00:00:00 2001 From: Ahmed El Sayegh Date: Sun, 25 Aug 2019 10:37:13 +0200 Subject: [PATCH 3/7] fix(create-route-map): only warn about first route without slash --- src/create-route-map.js | 16 ++++++++-------- test/unit/specs/create-map.spec.js | 24 +++++------------------- 2 files changed, 13 insertions(+), 27 deletions(-) diff --git a/src/create-route-map.js b/src/create-route-map.js index 74714f9be..061fb985c 100644 --- a/src/create-route-map.js +++ b/src/create-route-map.js @@ -34,14 +34,14 @@ export function createRouteMap ( } } - // warn if routes do not include leading slashes - const pathsMissingSlashes = pathList - .filter(path => !!path && path.charAt(0) !== '*' && path.charAt(0) !== '/') - .map(path => path.split('/')[0]) - .filter((path, index, pathList) => pathList.indexOf(path) === index) - - if (pathsMissingSlashes.length > 0) { - warn(false, `The following routes require a leading slash in their paths: ${pathsMissingSlashes.join(', ')}`) + if (process.env.NODE_ENV === 'development') { + // warn if routes do not include leading slashes + const found = pathList + .find(path => path.charAt(0) !== '*' && path.charAt(0) !== '/') + + if (found) { + warn(false, `Non-nested routes must include a leading slash character. Replace "${found}" with "/${found}".`) + } } return { diff --git a/test/unit/specs/create-map.spec.js b/test/unit/specs/create-map.spec.js index 4a0a3c1b9..228b26295 100644 --- a/test/unit/specs/create-map.spec.js +++ b/test/unit/specs/create-map.spec.js @@ -100,35 +100,21 @@ 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: '/', name: 'home', component: Home }, - { path: 'bar', name: 'bar', component: Bar }, - { path: 'foo', name: 'foo', component: Foo, - children: [ - { path: 'bar/:id', component: Bar } - ] - }, - { path: '*', name: 'any', component: Baz } + { path: 'bar', name: 'bar', component: Bar } ]) expect(console.warn).toHaveBeenCalledTimes(1) - expect(console.warn.calls.argsFor(0)[0]).toEqual('[vue-router] The following routes require a leading slash in their paths: bar, foo') + expect(console.warn.calls.argsFor(0)[0]).toEqual('[vue-router] Non-nested routes must include a leading slash character. Replace "bar" with "/bar".') }) - it('in development, it has not logged a missing leading slash warning when all paths have slashes', function () { + it('in development, it does not log the missing leading slash when routes are valid', function () { process.env.NODE_ENV = 'development' maps = createRouteMap([ - { path: '/', name: 'home', component: Home }, - { path: '/bar', name: 'bar', component: Bar }, - { path: '/foo', name: 'foo', component: Foo, - children: [ - { path: 'bar/:id', component: Bar } - ] - }, - { path: '*', name: 'any', component: Baz } + { path: '/bar', name: 'bar', component: Bar } ]) expect(console.warn).not.toHaveBeenCalled() }) - it('in production, it has not logged a missing leading slash warning', function () { + it('in production, it does not log the missing leading slash warning', function () { process.env.NODE_ENV = 'production' maps = createRouteMap([ { path: '/', name: 'home', component: Home }, From 2103945a96641d209212129da20fc2817ed760fb Mon Sep 17 00:00:00 2001 From: Ahmed El Sayegh Date: Sun, 25 Aug 2019 11:45:11 +0200 Subject: [PATCH 4/7] fix(create-route-map): handle case of '' route --- src/create-route-map.js | 6 +++--- test/unit/specs/create-map.spec.js | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/create-route-map.js b/src/create-route-map.js index 061fb985c..755068beb 100644 --- a/src/create-route-map.js +++ b/src/create-route-map.js @@ -37,10 +37,10 @@ export function createRouteMap ( if (process.env.NODE_ENV === 'development') { // warn if routes do not include leading slashes const found = pathList - .find(path => path.charAt(0) !== '*' && path.charAt(0) !== '/') + .filter(path => path && path.charAt(0) !== '*' && path.charAt(0) !== '/') - if (found) { - warn(false, `Non-nested routes must include a leading slash character. Replace "${found}" with "/${found}".`) + if (found.length > 0) { + warn(false, `Non-nested routes must include a leading slash character. Replace "${found[0]}" with "/${found[0]}".`) } } diff --git a/test/unit/specs/create-map.spec.js b/test/unit/specs/create-map.spec.js index 228b26295..e2727766a 100644 --- a/test/unit/specs/create-map.spec.js +++ b/test/unit/specs/create-map.spec.js @@ -68,6 +68,7 @@ describe('Creating Route Map', function () { process.env.NODE_ENV = 'development' maps = createRouteMap(routes) expect(console.warn).toHaveBeenCalledTimes(1) + console.log(console.warn.calls.argsFor(1)) expect(console.warn.calls.argsFor(0)[0]).toMatch('vue-router] Named Route \'bar\'') }) From a094f45044b8979e6588e90bc841d6ad47887444 Mon Sep 17 00:00:00 2001 From: Ahmed El Sayegh Date: Sun, 25 Aug 2019 11:47:57 +0200 Subject: [PATCH 5/7] fix(create-route-map): remove leftover console.log --- test/unit/specs/create-map.spec.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/unit/specs/create-map.spec.js b/test/unit/specs/create-map.spec.js index e2727766a..228b26295 100644 --- a/test/unit/specs/create-map.spec.js +++ b/test/unit/specs/create-map.spec.js @@ -68,7 +68,6 @@ describe('Creating Route Map', function () { process.env.NODE_ENV = 'development' maps = createRouteMap(routes) expect(console.warn).toHaveBeenCalledTimes(1) - console.log(console.warn.calls.argsFor(1)) expect(console.warn.calls.argsFor(0)[0]).toMatch('vue-router] Named Route \'bar\'') }) From 0d867c59ff4f84ac61f6522aaffd3f118d6f3ac6 Mon Sep 17 00:00:00 2001 From: Ahmed El Sayegh Date: Sun, 25 Aug 2019 22:53:49 +0200 Subject: [PATCH 6/7] fix(create-route-map): warn about root paths only --- src/create-route-map.js | 7 ++++++- test/unit/specs/create-map.spec.js | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/create-route-map.js b/src/create-route-map.js index 755068beb..df910d92c 100644 --- a/src/create-route-map.js +++ b/src/create-route-map.js @@ -37,10 +37,15 @@ 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) !== '/') + // split the path to get the root path part only + .map(path => path.split('/')[0]) + // remove duplicates caused by split child paths + .filter((path, index, pathList) => pathList.indexOf(path) === index) if (found.length > 0) { - warn(false, `Non-nested routes must include a leading slash character. Replace "${found[0]}" with "/${found[0]}".`) + warn(false, `Non-nested routes must include a leading slash character. Fix the following routes: ${found.join('\n')}.`) } } diff --git a/test/unit/specs/create-map.spec.js b/test/unit/specs/create-map.spec.js index 228b26295..31c725ee6 100644 --- a/test/unit/specs/create-map.spec.js +++ b/test/unit/specs/create-map.spec.js @@ -103,7 +103,7 @@ describe('Creating Route Map', function () { { 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. Replace "bar" with "/bar".') + expect(console.warn.calls.argsFor(0)[0]).toEqual('[vue-router] Non-nested routes must include a leading slash character. Fix the following routes: bar.') }) it('in development, it does not log the missing leading slash when routes are valid', function () { From f8d35b45cd494815c0fdae0c32f52e8326edfd19 Mon Sep 17 00:00:00 2001 From: Ahmed El Sayegh Date: Thu, 29 Aug 2019 13:09:59 +0200 Subject: [PATCH 7/7] fix(create-route-map): show prettier warning message --- src/create-route-map.js | 7 ++----- test/unit/specs/create-map.spec.js | 7 ++----- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/src/create-route-map.js b/src/create-route-map.js index df910d92c..d4d1a2504 100644 --- a/src/create-route-map.js +++ b/src/create-route-map.js @@ -39,13 +39,10 @@ export function createRouteMap ( const found = pathList // check for missing leading slash .filter(path => path && path.charAt(0) !== '*' && path.charAt(0) !== '/') - // split the path to get the root path part only - .map(path => path.split('/')[0]) - // remove duplicates caused by split child paths - .filter((path, index, pathList) => pathList.indexOf(path) === index) if (found.length > 0) { - warn(false, `Non-nested routes must include a leading slash character. Fix the following routes: ${found.join('\n')}.`) + 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}`) } } diff --git a/test/unit/specs/create-map.spec.js b/test/unit/specs/create-map.spec.js index 31c725ee6..bf1ae63d5 100644 --- a/test/unit/specs/create-map.spec.js +++ b/test/unit/specs/create-map.spec.js @@ -103,7 +103,7 @@ describe('Creating Route Map', function () { { 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: bar.') + 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 () { @@ -117,10 +117,7 @@ describe('Creating Route Map', function () { it('in production, it does not log the missing leading slash warning', function () { process.env.NODE_ENV = 'production' maps = createRouteMap([ - { path: '/', name: 'home', component: Home }, - { path: 'bar', name: 'bar', component: Bar }, - { path: 'foo', name: 'foo', component: Foo }, - { path: '*', name: 'any', component: Baz } + { path: 'bar', name: 'bar', component: Bar } ]) expect(console.warn).not.toHaveBeenCalled() })