From ad2a9884dba91e14b89d42170bad36f8eb89ee83 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Sun, 14 Jan 2018 17:25:34 +0100 Subject: [PATCH 1/3] fix(match): use fullPath for the param of * routes Fix #1994 Combining an asterisk route (*) with props: true would result in an error because the name of the param would be the number 0 making it an invalid attribute + the fact that vue passes props as attributes if they're not declared as props --- src/create-matcher.js | 3 ++- test/e2e/specs/route-matching.js | 4 ++-- test/unit/specs/create-matcher.spec.js | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/create-matcher.js b/src/create-matcher.js index 8be8b980a..9a8f56c55 100644 --- a/src/create-matcher.js +++ b/src/create-matcher.js @@ -189,7 +189,8 @@ function matchRoute ( const key = regex.keys[i - 1] const val = typeof m[i] === 'string' ? decodeURIComponent(m[i]) : m[i] if (key) { - params[key.name] = val + // Fix #1994: using * with props: true generates a param named 0 + params[key.name || 'fullPath'] = val } } diff --git a/test/e2e/specs/route-matching.js b/test/e2e/specs/route-matching.js index ea1ef3a29..065b10827 100644 --- a/test/e2e/specs/route-matching.js +++ b/test/e2e/specs/route-matching.js @@ -83,7 +83,7 @@ module.exports = { route.matched[0].path === '/asterisk/*' && route.fullPath === '/asterisk/foo' && JSON.stringify(route.params) === JSON.stringify({ - 0: 'foo' + 'fullPath': 'foo' }) ) }, null, '/asterisk/foo') @@ -96,7 +96,7 @@ module.exports = { route.matched[0].path === '/asterisk/*' && route.fullPath === '/asterisk/foo/bar' && JSON.stringify(route.params) === JSON.stringify({ - 0: 'foo/bar' + 'fullPath': 'foo/bar' }) ) }, null, '/asterisk/foo/bar') diff --git a/test/unit/specs/create-matcher.spec.js b/test/unit/specs/create-matcher.spec.js index bb932c702..01befcf31 100644 --- a/test/unit/specs/create-matcher.spec.js +++ b/test/unit/specs/create-matcher.spec.js @@ -3,7 +3,7 @@ import { createMatcher } from '../../../src/create-matcher' const routes = [ { path: '/', name: 'home', component: { name: 'home' }}, - { path: '/foo', name: 'foo', component: { name: 'foo' }}, + { path: '/foo', name: 'foo', component: { name: 'foo' }} ] describe('Creating Matcher', function () { From b505400b22ce1b0e701c7478e59ca275199604a2 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Sun, 14 Jan 2018 17:31:47 +0100 Subject: [PATCH 2/3] test(match): add test for asterisk route with props: true --- test/unit/specs/create-matcher.spec.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/test/unit/specs/create-matcher.spec.js b/test/unit/specs/create-matcher.spec.js index 01befcf31..6c13fb5b0 100644 --- a/test/unit/specs/create-matcher.spec.js +++ b/test/unit/specs/create-matcher.spec.js @@ -3,7 +3,8 @@ import { createMatcher } from '../../../src/create-matcher' const routes = [ { path: '/', name: 'home', component: { name: 'home' }}, - { path: '/foo', name: 'foo', component: { name: 'foo' }} + { path: '/foo', name: 'foo', component: { name: 'foo' }}, + { path: '*', props: true, component: { name: 'notFound' }} ] describe('Creating Matcher', function () { @@ -32,4 +33,9 @@ describe('Creating Matcher', function () { match({ name: 'foo' }, routes[0]) expect(console.warn).not.toHaveBeenCalled() }) + + it('matches asterisk routes with fullName as the param', function () { + const { params } = match({ path: '/not-found' }, routes[0]) + expect(params).toEqual({ fullPath: '/not-found' }) + }) }) From 5edc2ec84501625fa95a6cfe9c2c12c2b33c76f4 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Sun, 14 Jan 2018 17:36:21 +0100 Subject: [PATCH 3/3] refactor(match): use pathMatch instead of fullPath for unnamed params --- src/create-matcher.js | 2 +- test/e2e/specs/route-matching.js | 6 +++--- test/unit/specs/create-matcher.spec.js | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/create-matcher.js b/src/create-matcher.js index 9a8f56c55..07987c431 100644 --- a/src/create-matcher.js +++ b/src/create-matcher.js @@ -190,7 +190,7 @@ function matchRoute ( const val = typeof m[i] === 'string' ? decodeURIComponent(m[i]) : m[i] if (key) { // Fix #1994: using * with props: true generates a param named 0 - params[key.name || 'fullPath'] = val + params[key.name || 'pathMatch'] = val } } diff --git a/test/e2e/specs/route-matching.js b/test/e2e/specs/route-matching.js index 065b10827..ab52dfc77 100644 --- a/test/e2e/specs/route-matching.js +++ b/test/e2e/specs/route-matching.js @@ -83,7 +83,7 @@ module.exports = { route.matched[0].path === '/asterisk/*' && route.fullPath === '/asterisk/foo' && JSON.stringify(route.params) === JSON.stringify({ - 'fullPath': 'foo' + pathMatch: 'foo' }) ) }, null, '/asterisk/foo') @@ -96,7 +96,7 @@ module.exports = { route.matched[0].path === '/asterisk/*' && route.fullPath === '/asterisk/foo/bar' && JSON.stringify(route.params) === JSON.stringify({ - 'fullPath': 'foo/bar' + pathMatch: 'foo/bar' }) ) }, null, '/asterisk/foo/bar') @@ -120,7 +120,7 @@ module.exports = { route.matched[0].path === '/optional-group/(foo/)?bar' && route.fullPath === '/optional-group/foo/bar' && JSON.stringify(route.params) === JSON.stringify({ - 0: 'foo/' + pathMatch: 'foo/' }) ) }, null, '/optional-group/foo/bar') diff --git a/test/unit/specs/create-matcher.spec.js b/test/unit/specs/create-matcher.spec.js index 6c13fb5b0..5028957e5 100644 --- a/test/unit/specs/create-matcher.spec.js +++ b/test/unit/specs/create-matcher.spec.js @@ -34,8 +34,8 @@ describe('Creating Matcher', function () { expect(console.warn).not.toHaveBeenCalled() }) - it('matches asterisk routes with fullName as the param', function () { + it('matches asterisk routes with a default param name', function () { const { params } = match({ path: '/not-found' }, routes[0]) - expect(params).toEqual({ fullPath: '/not-found' }) + expect(params).toEqual({ pathMatch: '/not-found' }) }) })