Skip to content

Commit 794fdd5

Browse files
committed
refactor: rename matcher to resolver
1 parent 94a7e25 commit 794fdd5

File tree

12 files changed

+121
-155
lines changed

12 files changed

+121
-155
lines changed

packages/router/src/experimental/router.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ import type {
2727
NEW_LocationResolved,
2828
NEW_MatcherRecord,
2929
NEW_MatcherRecordRaw,
30-
NEW_RouterMatcher,
31-
} from '../new-route-resolver/matcher'
30+
NEW_RouterResolver,
31+
} from '../new-route-resolver/resolver'
3232
import {
3333
parseQuery as originalParseQuery,
3434
stringifyQuery as originalStringifyQuery,
@@ -51,7 +51,6 @@ import type {
5151
RouteLocationAsRelative,
5252
RouteLocationAsRelativeTyped,
5353
RouteLocationAsString,
54-
RouteLocationGeneric,
5554
RouteLocationNormalized,
5655
RouteLocationNormalizedLoaded,
5756
RouteLocationRaw,
@@ -191,7 +190,7 @@ export interface EXPERIMENTAL_RouterOptions<
191190
* Matcher to use to resolve routes.
192191
* @experimental
193192
*/
194-
matcher: NEW_RouterMatcher<NEW_MatcherRecordRaw, TMatcherRecord>
193+
matcher: NEW_RouterResolver<NEW_MatcherRecordRaw, TMatcherRecord>
195194
}
196195

197196
/**
@@ -407,6 +406,9 @@ export interface EXPERIMENTAL_RouteRecordRaw extends NEW_MatcherRecordRaw {
407406

408407
// TODO: is it worth to have 2 types for the undefined values?
409408
export interface EXPERIMENTAL_RouteRecordNormalized extends NEW_MatcherRecord {
409+
/**
410+
* Arbitrary data attached to the record.
411+
*/
410412
meta: RouteMeta
411413
}
412414

@@ -468,7 +470,7 @@ export function experimental_createRouter(
468470
| EXPERIMENTAL_RouteRecordRaw,
469471
route?: EXPERIMENTAL_RouteRecordRaw
470472
) {
471-
let parent: Parameters<(typeof matcher)['addRoute']>[1] | undefined
473+
let parent: Parameters<(typeof matcher)['addMatcher']>[1] | undefined
472474
let rawRecord: EXPERIMENTAL_RouteRecordRaw
473475

474476
if (isRouteName(parentOrRoute)) {
@@ -486,20 +488,20 @@ export function experimental_createRouter(
486488
rawRecord = parentOrRoute
487489
}
488490

489-
const addedRecord = matcher.addRoute(
491+
const addedRecord = matcher.addMatcher(
490492
normalizeRouteRecord(rawRecord),
491493
parent
492494
)
493495

494496
return () => {
495-
matcher.removeRoute(addedRecord)
497+
matcher.removeMatcher(addedRecord)
496498
}
497499
}
498500

499501
function removeRoute(name: NonNullable<RouteRecordNameGeneric>) {
500502
const recordMatcher = matcher.getMatcher(name)
501503
if (recordMatcher) {
502-
matcher.removeRoute(recordMatcher)
504+
matcher.removeMatcher(recordMatcher)
503505
} else if (__DEV__) {
504506
warn(`Cannot remove non-existent route "${String(name)}"`)
505507
}
@@ -1219,7 +1221,7 @@ export function experimental_createRouter(
12191221

12201222
addRoute,
12211223
removeRoute,
1222-
clearRoutes: matcher.clearRoutes,
1224+
clearRoutes: matcher.clearMatchers,
12231225
hasRoute,
12241226
getRoutes,
12251227
resolve,

packages/router/src/matcher/index.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ import type {
1414
_PathParserOptions,
1515
} from './pathParserRanker'
1616

17-
import { comparePathParserScore } from './pathParserRanker'
17+
import {
18+
comparePathParserScore,
19+
PATH_PARSER_OPTIONS_DEFAULTS,
20+
} from './pathParserRanker'
1821

1922
import { warn } from '../warning'
20-
import { assign, noop } from '../utils'
23+
import { assign, mergeOptions, noop } from '../utils'
2124
import type { RouteRecordNameGeneric, _RouteRecordProps } from '../typed-routes'
2225

2326
/**
@@ -64,8 +67,8 @@ export function createRouterMatcher(
6467
NonNullable<RouteRecordNameGeneric>,
6568
RouteRecordMatcher
6669
>()
67-
globalOptions = mergeOptions(
68-
{ strict: false, end: true, sensitive: false } as PathParserOptions,
70+
globalOptions = mergeOptions<PathParserOptions>(
71+
PATH_PARSER_OPTIONS_DEFAULTS,
6972
globalOptions
7073
)
7174

@@ -429,7 +432,7 @@ export function normalizeRouteRecord(
429432
* components. Also accept a boolean for components.
430433
* @param record
431434
*/
432-
function normalizeRecordProps(
435+
export function normalizeRecordProps(
433436
record: RouteRecordRaw
434437
): Record<string, _RouteRecordProps> {
435438
const propsObject = {} as Record<string, _RouteRecordProps>
@@ -472,18 +475,6 @@ function mergeMetaFields(matched: MatcherLocation['matched']) {
472475
)
473476
}
474477

475-
function mergeOptions<T extends object>(
476-
defaults: T,
477-
partialOptions: Partial<T>
478-
): T {
479-
const options = {} as T
480-
for (const key in defaults) {
481-
options[key] = key in partialOptions ? partialOptions[key]! : defaults[key]
482-
}
483-
484-
return options
485-
}
486-
487478
type ParamKey = RouteRecordMatcher['keys'][number]
488479

489480
function isSameParam(a: ParamKey, b: ParamKey): boolean {
@@ -521,7 +512,7 @@ function checkSameParams(a: RouteRecordMatcher, b: RouteRecordMatcher) {
521512
* @param mainNormalizedRecord - RouteRecordNormalized
522513
* @param parent - RouteRecordMatcher
523514
*/
524-
function checkChildMissingNameWithEmptyPath(
515+
export function checkChildMissingNameWithEmptyPath(
525516
mainNormalizedRecord: RouteRecordNormalized,
526517
parent?: RouteRecordMatcher
527518
) {

packages/router/src/matcher/pathParserRanker.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,3 +367,8 @@ function isLastScoreNegative(score: PathParser['score']): boolean {
367367
const last = score[score.length - 1]
368368
return score.length > 0 && last[last.length - 1] < 0
369369
}
370+
export const PATH_PARSER_OPTIONS_DEFAULTS: PathParserOptions = {
371+
strict: false,
372+
end: true,
373+
sensitive: false,
374+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { createCompiledMatcher } from './matcher'
1+
export { createCompiledMatcher } from './resolver'

packages/router/src/new-route-resolver/matcher-location.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { LocationQueryRaw } from '../query'
2-
import type { MatcherName } from './matcher'
2+
import type { MatcherName } from './resolver'
33

44
/**
55
* Generic object of params that can be passed to a matcher.

packages/router/src/new-route-resolver/matcher-pattern.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { decode, MatcherQueryParams } from './matcher'
1+
import { decode, MatcherQueryParams } from './resolver'
22
import { EmptyParams, MatcherParamsFormatted } from './matcher-location'
33
import { miss } from './matchers/errors'
44

packages/router/src/new-route-resolver/matcher-resolve.spec.ts

Lines changed: 11 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { createRouterMatcher, normalizeRouteRecord } from '../matcher'
22
import { RouteComponent, RouteRecordRaw, MatcherLocation } from '../types'
3-
import { MatcherLocationNormalizedLoose } from '../../__tests__/utils'
43
import { defineComponent } from 'vue'
54
import { START_LOCATION_NORMALIZED } from '../location'
65
import { describe, expect, it } from 'vitest'
@@ -10,7 +9,8 @@ import {
109
MatcherLocationRaw,
1110
NEW_MatcherRecordRaw,
1211
NEW_LocationResolved,
13-
} from './matcher'
12+
NEW_MatcherRecord,
13+
} from './resolver'
1414
import { PathParams, tokensToParser } from '../matcher/pathParserRanker'
1515
import { tokenizePath } from '../matcher/pathTokenizer'
1616
import { miss } from './matchers/errors'
@@ -63,22 +63,23 @@ function compileRouteRecord(
6363

6464
describe('RouterMatcher.resolve', () => {
6565
mockWarn()
66-
type Matcher = ReturnType<typeof createRouterMatcher>
66+
type Matcher = ReturnType<typeof createCompiledMatcher>
6767
type MatcherResolvedLocation = ReturnType<Matcher['resolve']>
6868

69-
const START_LOCATION: NEW_LocationResolved = {
69+
const START_LOCATION: MatcherResolvedLocation = {
7070
name: Symbol('START'),
71-
fullPath: '/',
72-
path: '/',
7371
params: {},
72+
path: '/',
73+
fullPath: '/',
7474
query: {},
7575
hash: '',
7676
matched: [],
77+
// meta: {},
7778
}
7879

7980
function isMatcherLocationResolved(
8081
location: unknown
81-
): location is NEW_LocationResolved {
82+
): location is NEW_LocationResolved<NEW_MatcherRecord> {
8283
return !!(
8384
location &&
8485
typeof location === 'object' &&
@@ -95,16 +96,16 @@ describe('RouterMatcher.resolve', () => {
9596
toLocation: MatcherLocationRaw,
9697
expectedLocation: Partial<MatcherResolvedLocation>,
9798
fromLocation:
98-
| NEW_LocationResolved
99+
| NEW_LocationResolved<NEW_MatcherRecord>
99100
| Exclude<MatcherLocationRaw, string>
100101
| `/${string}` = START_LOCATION
101102
) {
102103
const records = (Array.isArray(record) ? record : [record]).map(
103104
(record): NEW_MatcherRecordRaw => compileRouteRecord(record)
104105
)
105-
const matcher = createCompiledMatcher()
106+
const matcher = createCompiledMatcher<NEW_MatcherRecord>()
106107
for (const record of records) {
107-
matcher.addRoute(record)
108+
matcher.addMatcher(record)
108109
}
109110

110111
const resolved: MatcherResolvedLocation = {
@@ -137,60 +138,6 @@ describe('RouterMatcher.resolve', () => {
137138
})
138139
}
139140

140-
function _assertRecordMatch(
141-
record: RouteRecordRaw | RouteRecordRaw[],
142-
location: MatcherLocationRaw,
143-
resolved: Partial<MatcherLocationNormalizedLoose>,
144-
start: MatcherLocation = START_LOCATION_NORMALIZED
145-
) {
146-
record = Array.isArray(record) ? record : [record]
147-
const matcher = createRouterMatcher(record, {})
148-
149-
if (!('meta' in resolved)) {
150-
resolved.meta = record[0].meta || {}
151-
}
152-
153-
if (!('name' in resolved)) {
154-
resolved.name = undefined
155-
}
156-
157-
// add location if provided as it should be the same value
158-
if ('path' in location && !('path' in resolved)) {
159-
resolved.path = location.path
160-
}
161-
162-
if ('redirect' in record) {
163-
throw new Error('not handled')
164-
} else {
165-
// use one single record
166-
if (!resolved.matched) resolved.matched = record.map(normalizeRouteRecord)
167-
// allow passing an expect.any(Array)
168-
else if (Array.isArray(resolved.matched))
169-
resolved.matched = resolved.matched.map(m => ({
170-
...normalizeRouteRecord(m as any),
171-
aliasOf: m.aliasOf,
172-
}))
173-
}
174-
175-
// allows not passing params
176-
resolved.params =
177-
resolved.params || ('params' in location ? location.params : {})
178-
179-
const startCopy: MatcherLocation = {
180-
...start,
181-
matched: start.matched.map(m => ({
182-
...normalizeRouteRecord(m),
183-
aliasOf: m.aliasOf,
184-
})) as MatcherLocation['matched'],
185-
}
186-
187-
// make matched non enumerable
188-
Object.defineProperty(startCopy, 'matched', { enumerable: false })
189-
190-
const result = matcher.resolve(location, startCopy)
191-
expect(result).toEqual(resolved)
192-
}
193-
194141
/**
195142
*
196143
* @param record - Record or records we are testing the matcher against

packages/router/src/new-route-resolver/matcher.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import {
33
createCompiledMatcher,
44
NO_MATCH_LOCATION,
55
pathEncoded,
6-
} from './matcher'
6+
} from './resolver'
77
import {
88
MatcherPatternParams_Base,
99
MatcherPatternPath,
1010
MatcherPatternQuery,
1111
MatcherPatternPathStatic,
1212
MatcherPatternPathDynamic,
1313
} from './matcher-pattern'
14-
import { NEW_MatcherRecord } from './matcher'
14+
import { NEW_MatcherRecord } from './resolver'
1515
import { miss } from './matchers/errors'
1616
import { EmptyParams } from './matcher-location'
1717

@@ -133,25 +133,25 @@ describe('RouterMatcher', () => {
133133
describe('adding and removing', () => {
134134
it('add static path', () => {
135135
const matcher = createCompiledMatcher()
136-
matcher.addRoute(EMPTY_PATH_ROUTE)
136+
matcher.addMatcher(EMPTY_PATH_ROUTE)
137137
})
138138

139139
it('adds dynamic path', () => {
140140
const matcher = createCompiledMatcher()
141-
matcher.addRoute(USER_ID_ROUTE)
141+
matcher.addMatcher(USER_ID_ROUTE)
142142
})
143143

144144
it('removes static path', () => {
145145
const matcher = createCompiledMatcher()
146-
matcher.addRoute(EMPTY_PATH_ROUTE)
147-
matcher.removeRoute(EMPTY_PATH_ROUTE)
146+
matcher.addMatcher(EMPTY_PATH_ROUTE)
147+
matcher.removeMatcher(EMPTY_PATH_ROUTE)
148148
// Add assertions to verify the route was removed
149149
})
150150

151151
it('removes dynamic path', () => {
152152
const matcher = createCompiledMatcher()
153-
matcher.addRoute(USER_ID_ROUTE)
154-
matcher.removeRoute(USER_ID_ROUTE)
153+
matcher.addMatcher(USER_ID_ROUTE)
154+
matcher.removeMatcher(USER_ID_ROUTE)
155155
// Add assertions to verify the route was removed
156156
})
157157
})

packages/router/src/new-route-resolver/matcher.test-d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import { describe, expectTypeOf, it } from 'vitest'
22
import {
33
NEW_LocationResolved,
44
NEW_MatcherRecordRaw,
5-
NEW_RouterMatcher,
6-
} from './matcher'
5+
NEW_RouterResolver,
6+
} from './resolver'
77
import { EXPERIMENTAL_RouteRecordNormalized } from '../experimental/router'
88

99
describe('Matcher', () => {
1010
type TMatcherRecordRaw = NEW_MatcherRecordRaw
1111
type TMatcherRecord = EXPERIMENTAL_RouteRecordNormalized
1212

13-
const matcher: NEW_RouterMatcher<TMatcherRecordRaw, TMatcherRecord> =
13+
const matcher: NEW_RouterResolver<TMatcherRecordRaw, TMatcherRecord> =
1414
{} as any
1515

1616
describe('matcher.resolve()', () => {

packages/router/src/new-route-resolver/matchers/test-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
MatcherPatternQuery,
55
MatcherPatternParams_Base,
66
} from '../matcher-pattern'
7-
import { NEW_MatcherRecord } from '../matcher'
7+
import { NEW_MatcherRecord } from '../resolver'
88
import { miss } from './errors'
99

1010
export const ANY_PATH_PATTERN_MATCHER: MatcherPatternPath<{

0 commit comments

Comments
 (0)