]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix: try to arrange root scores refactor/path-ranking-scores
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 21 Sep 2020 16:33:47 +0000 (18:33 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 2 Jun 2021 07:49:03 +0000 (09:49 +0200)
__tests__/matcher/pathRanking.spec.ts
src/matcher/pathParserRanker.ts

index ce6e688489f0816e18a8fc7a27cd9cbc9205a259..490def89c5ee18ead5d3bc6d2d09cdbc974aee6b 100644 (file)
@@ -134,7 +134,16 @@ describe('Path ranking', () => {
     ])
   })
 
+  it('root after others', () => {
+    checkPathOrder(['/static', '/'])
+    checkPathOrder(['/:w', '/'])
+    // this isn't possible as long as /:w? > /:w+ because / > /:w?
+    // checkPathOrder(['/:w+', '/'])
+    checkPathOrder(['/', '/:w(.*)'])
+  })
+
   it('puts the slash before optional parameters', () => {
+    // checkPathOrder(['/', '/:a?'])
     possibleOptions.forEach(options => {
       checkPathOrder(['/', ['/:a?', options]])
       checkPathOrder(['/', ['/:a*', options]])
@@ -176,12 +185,6 @@ describe('Path ranking', () => {
     })
   })
 
-  it('empty path before slash', () => {
-    possibleOptions.forEach(options => {
-      checkPathOrder(['', ['/', options]])
-    })
-  })
-
   it('works with long paths', () => {
     checkPathOrder(['/a/b/c/d/e', '/:k/b/c/d/e', '/:k/b/c/d/:j'])
   })
@@ -209,7 +212,7 @@ describe('Path ranking', () => {
 
   it('puts the wildcard at the end', () => {
     possibleOptions.forEach(options => {
-      checkPathOrder([['', options], '/:rest(.*)'])
+      // checkPathOrder([['', options], '/:rest(.*)'])
       checkPathOrder([['/', options], '/:rest(.*)'])
       checkPathOrder([['/ab', options], '/:rest(.*)'])
       checkPathOrder([['/:a', options], '/:rest(.*)'])
index 0a848ffd12d7e8d7ddbc02e735e8ce63511e6988..43d385607098b9e6823e5c8c9a14496b97212f6c 100644 (file)
@@ -85,13 +85,13 @@ const BASE_PATH_PARSER_OPTIONS: Required<_PathParserOptions> = {
 // Scoring values used in tokensToParser
 const enum PathScore {
   _multiplier = 10,
-  Root = 9 * _multiplier, // just /
+  Root = 5.5 * _multiplier, // just /
   Segment = 4 * _multiplier, // /a-segment
   SubSegment = 3 * _multiplier, // /multiple-:things-in-one-:segment
   Static = 4 * _multiplier, // /static
   Dynamic = 2 * _multiplier, // /:someId
   BonusCustomRegExp = 1 * _multiplier, // /:someId(\\d+)
-  BonusWildcard = -4 * _multiplier - BonusCustomRegExp, // /:namedWildcard(.*) we remove the bonus added by the custom regexp
+  BonusWildcard = -5 * _multiplier - BonusCustomRegExp, // /:namedWildcard(.*) we remove the bonus added by the custom regexp
   BonusRepeatable = -2 * _multiplier, // /:w+ or /:w*
   BonusOptional = -0.8 * _multiplier, // /:w? or /:w*
   // these two have to be under 0.1 so a strict /:page is still lower than /:a-:b
@@ -124,7 +124,11 @@ export function tokensToParser(
 
   for (const segment of segments) {
     // the root segment needs special treatment
-    const segmentScores: number[] = segment.length ? [] : [PathScore.Root]
+    const segmentScores: number[] =
+      // the root can also be a leading slash with a single token with value = ''
+      segment.length > 1 || (segment[0] && segment[0].value)
+        ? []
+        : [PathScore.Root]
 
     // allow trailing slash
     if (options.strict && !segment.length) pattern += '/'