]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
refactor: check for edge cases too
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 28 Feb 2022 14:34:36 +0000 (15:34 +0100)
committerEduardo San Martin Morote <posva@users.noreply.github.com>
Mon, 28 Feb 2022 14:37:47 +0000 (15:37 +0100)
src/matcher/index.ts

index 994af7ec33b5296da74e41f840c3392df411e933..ca31e86fd48513c7651bfd5339afd01d0fad89e4 100644 (file)
@@ -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 }