]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix(matcher): leave catchall routes at the end
authorEduardo San Martin Morote <posva13@gmail.com>
Thu, 9 Jun 2022 18:57:56 +0000 (20:57 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Thu, 9 Jun 2022 18:57:56 +0000 (20:57 +0200)
Fix #1435

__tests__/matcher/pathRanking.spec.ts
src/matcher/pathParserRanker.ts

index ce6e688489f0816e18a8fc7a27cd9cbc9205a259..a5d4d46a1e227ac164ad0eb487cade57f38bb695 100644 (file)
@@ -143,6 +143,15 @@ describe('Path ranking', () => {
     })
   })
 
+  it('puts catchall param after same prefix', () => {
+    possibleOptions.forEach(options => {
+      checkPathOrder([
+        ['/a', options],
+        ['/a/:a(.*)*', options],
+      ])
+    })
+  })
+
   it('sensitive should go before non sensitive', () => {
     checkPathOrder([
       ['/Home', { sensitive: true }],
index 483a29c473ef21ccb766236e47f7a7111602f51e..f225fa7a1be513236e7b62d6198163d0e5824b83 100644 (file)
@@ -329,6 +329,10 @@ export function comparePathParserScore(a: PathParser, b: PathParser): number {
 
     i++
   }
+  if (Math.abs(bScore.length - aScore.length) === 1) {
+    if (isLastScoreNegative(aScore)) return 1
+    if (isLastScoreNegative(bScore)) return -1
+  }
 
   // if a and b share the same score entries but b has more, sort b first
   return bScore.length - aScore.length
@@ -339,3 +343,14 @@ export function comparePathParserScore(a: PathParser, b: PathParser): number {
   //   ? -1
   //   : 0
 }
+
+/**
+ * This allows detecting splats at the end of a path: /home/:id(.*)*
+ *
+ * @param score - score to check
+ * @returns true if the last entry is negative
+ */
+function isLastScoreNegative(score: PathParser['score']): boolean {
+  const last = score[score.length - 1]
+  return score.length > 0 && last[last.length - 1] < 0
+}