Skip to content

Commit

Permalink
fix(create-route-map): warn about root paths without a leading slash
Browse files Browse the repository at this point in the history
  • Loading branch information
Sayegh7 committed Jan 22, 2019
1 parent b1c178c commit 0416191
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
5 changes: 0 additions & 5 deletions src/create-matcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions src/create-route-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
42 changes: 42 additions & 0 deletions test/unit/specs/create-map.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down

0 comments on commit 0416191

Please sign in to comment.