From 42f82ed5b6fbbccef4afb188edf3914ab0859a3a Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Mon, 28 Feb 2022 15:34:36 +0100 Subject: [PATCH] refactor: check for edge cases too --- src/matcher/index.ts | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/src/matcher/index.ts b/src/matcher/index.ts index 994af7ec..ca31e86f 100644 --- a/src/matcher/index.ts +++ b/src/matcher/index.ts @@ -211,28 +211,17 @@ export function createRouterMatcher( return matchers } - function isChildOf( - child: RouteRecordMatcher, - parent: RouteRecordMatcher - ): Boolean { - return parent.children.some(currChild => { - if (currChild === child) return true - - return isChildOf(child, currChild) - }) - } - function insertMatcher(matcher: RouteRecordMatcher) { let i = 0 - // console.log('i is', { i }) while ( i < matchers.length && comparePathParserScore(matcher, matchers[i]) >= 0 && - !isChildOf(matcher, matchers[i]) + // Adding children with empty path should still appear before the parent + // https://github.com/vuejs/router/issues/1124 + (matcher.record.path !== matchers[i].record.path || + !isRecordChildOf(matcher, matchers[i])) ) i++ - // console.log('END i is', { i }) - // while (i < matchers.length && matcher.score <= matchers[i].score) i++ matchers.splice(i, 0, matcher) // only add the original record to the name map if (matcher.record.name && !isAliasRecord(matcher)) @@ -474,4 +463,13 @@ function checkMissingParamsInAbsolutePath( } } +function isRecordChildOf( + record: RouteRecordMatcher, + parent: RouteRecordMatcher +): boolean { + return parent.children.some( + child => child === record || isRecordChildOf(record, child) + ) +} + export type { PathParserOptions, _PathParserOptions } -- 2.47.3