From 4a0cf8f404c0ce5da7278692841033acdf14e2bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20K=C4=99drzy=C5=84ski?= Date: Thu, 25 Jun 2020 14:42:16 +0200 Subject: [PATCH] fix(utils): flat routes if child routes have absolute paths (#7604) --- packages/utils/src/route.js | 15 ++++++++++----- packages/utils/test/route.test.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/packages/utils/src/route.js b/packages/utils/src/route.js index 3c08109d6a..95dc8f238e 100644 --- a/packages/utils/src/route.js +++ b/packages/utils/src/route.js @@ -13,14 +13,19 @@ export const flatRoutes = function flatRoutes (router, fileName = '', routes = [ if (fileName === '' && r.path === '/') { routes.push('/') } + return flatRoutes(r.children, fileName + r.path + '/', routes) } fileName = fileName.replace(/\/+/g, '/') - routes.push( - (r.path === '' && fileName[fileName.length - 1] === '/' - ? fileName.slice(0, -1) - : fileName) + r.path - ) + + // if child path is already absolute, do not make any concatenations + if (r.path && r.path.startsWith('/')) { + routes.push(r.path) + } else if (r.path === '' && fileName[fileName.length - 1] === '/') { + routes.push(fileName.slice(0, -1) + r.path) + } else { + routes.push(fileName + r.path) + } }) return routes } diff --git a/packages/utils/test/route.test.js b/packages/utils/test/route.test.js index 4ec0ee35bf..7422d55c13 100644 --- a/packages/utils/test/route.test.js +++ b/packages/utils/test/route.test.js @@ -40,6 +40,37 @@ describe('util: route', () => { expect(routes).toEqual(['/', '/foo/bar', '/foo/baz']) }) + test('should flat absolute routes', () => { + const routes = flatRoutes([ + { + name: 'foo', + path: '/foo', + children: [ + { name: 'foo-bar', path: '/foo/bar' }, + { name: 'foo-baz', path: '/foo/baz' } + ] + } + ]) + + expect(routes).toEqual(['/foo/bar', '/foo/baz']) + }) + + test('should flat absolute routes with empty path', () => { + const routes = flatRoutes([ + { + name: 'foo', + path: '/foo', + children: [ + { name: 'foo-root', path: '' }, + { name: 'foo-bar', path: '/foo/bar' }, + { name: 'foo-baz', path: '/foo/baz' } + ] + } + ]) + + expect(routes).toEqual(['/foo', '/foo/bar', '/foo/baz']) + }) + describe('util: route guard', () => { test('should guard parent dir', () => { expect(() => {