From: Eduardo San Martin Morote Date: Mon, 21 Sep 2020 16:33:47 +0000 (+0200) Subject: fix: try to arrange root scores X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=41b21bfa470a419d88a201b39a6059fa6bf77b87;p=thirdparty%2Fvuejs%2Frouter.git fix: try to arrange root scores --- diff --git a/__tests__/matcher/pathRanking.spec.ts b/__tests__/matcher/pathRanking.spec.ts index ce6e6884..490def89 100644 --- a/__tests__/matcher/pathRanking.spec.ts +++ b/__tests__/matcher/pathRanking.spec.ts @@ -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(.*)']) diff --git a/src/matcher/pathParserRanker.ts b/src/matcher/pathParserRanker.ts index 0a848ffd..43d38560 100644 --- a/src/matcher/pathParserRanker.ts +++ b/src/matcher/pathParserRanker.ts @@ -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 += '/'