Skip to content

Commit 51f12c2

Browse files
committed
refactor(types): move tests of types
1 parent 943cfcd commit 51f12c2

File tree

4 files changed

+64
-171
lines changed

4 files changed

+64
-171
lines changed

packages/router/src/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,13 @@ export type {
6464
} from './types'
6565
export type {
6666
ParamsFromPath,
67-
_ExtractFirstParamName,
68-
_RemoveRegexpFromParam,
67+
ParamsRawFromPath,
68+
_StripRegex,
6969
_RemoveUntilClosingPar,
70+
_ExtractParamsOfPath,
71+
_ParamExtractResult,
72+
_ExtractModifier,
73+
_ModifierExtracTResult,
7074
_JoinPath,
7175
_ParamDelimiter,
7276
_ParamModifier,

packages/router/src/types/named.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@ import type {
44
RouteRecordRaw,
55
RouteRecordName,
66
} from '.'
7-
import type {
8-
_JoinPath,
9-
ParamsFromPath,
10-
ParamsRawFromPath,
11-
PathFromParams,
12-
} from './paths'
13-
import { LiteralUnion } from './utils'
7+
import type { _JoinPath, ParamsFromPath, ParamsRawFromPath } from './paths'
148

159
/**
1610
* Creates a map with each named route as a properties. Each property contains the type of the params in raw and

packages/router/src/types/paths.ts

Lines changed: 5 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -54,26 +54,7 @@ export type _ParamDelimiter =
5454
| _ParamModifier
5555

5656
/**
57-
* Given a simple path, creates an object of the possible param values.
58-
*
59-
* @internal
60-
*/
61-
export type _ExtractParamsPath<
62-
P extends string,
63-
isRaw extends boolean
64-
> = P extends `${string}{${infer PP}}${infer Rest}`
65-
? (PP extends `${infer N}${_ParamModifier}`
66-
? PP extends `${N}${infer M}`
67-
? M extends _ParamModifier
68-
? _ParamToObject<N, M, isRaw>
69-
: never
70-
: never
71-
: _ParamToObject<PP, '', isRaw>) &
72-
_ExtractParamsPath<Rest, isRaw>
73-
: {}
74-
75-
/**
76-
* Given a path, extracts the possible params or {} when there are no params.
57+
* Given a path, extracts the possible params or \{\} when there are no params.
7758
*
7859
* @internal
7960
*/
@@ -93,34 +74,18 @@ export type _ExtractParamsOfPath<
9374
>
9475
? _ParamToObject<ParamName, Modifier, isRaw> &
9576
_ExtractParamsOfPath<Rest2, isRaw>
96-
: {
97-
NO: 1 // this should never happen as the modifier can be empty
98-
}
77+
: never // this should never happen as the modifier can be empty
9978
: // Nothing after the param: /:id, we are done
10079
_ParamToObject<HasParam, '', isRaw>
10180
: {
10281
// EMPTY: 1
10382
}
10483

105-
type a1 = _ExtractParamsOfPath<'/', false>
106-
type a2 = _ExtractParamsOfPath<'/:id', false>
107-
type a3 = _ExtractParamsOfPath<'/:id/:b', false>
108-
type a4 = _ExtractParamsOfPath<'/:id(.*)', false>
109-
type a5 = _ExtractParamsOfPath<'/:id(.*)/other', false>
110-
type a6 = _ExtractParamsOfPath<'/:id(.*)+', false>
111-
type a7 = _ExtractParamsOfPath<'/:id(.*)+/other', false>
112-
type a8 = _ExtractParamsOfPath<'/:id(.*)+/other/:b/:c/:d', false>
113-
114-
type test1 =
115-
'/:id/:b' extends `${string}:${infer P}${_ParamDelimiter}${infer Rest}`
116-
? [P, Rest]
117-
: never
118-
11984
/**
12085
* Helper type to infer a param name extraction result
12186
* @internal
12287
*/
123-
interface _ParamExtractResult<P extends string, Rest extends string> {
88+
export interface _ParamExtractResult<P extends string, Rest extends string> {
12489
param: P
12590
rest: Rest
12691
}
@@ -146,10 +111,6 @@ type _ExtractParamName<
146111
: // add the rest to the end after a % which is invalid in a path so it can be used as a delimiter
147112
_ParamExtractResult<Head, Tail>
148113

149-
type p1 = _ExtractParamName<'id'>
150-
type p2 = _ExtractParamName<'abc+/dos'>
151-
type p3 = _ExtractParamName<'abc/:dos)'>
152-
153114
/**
154115
* We consider a what comes after a param, e.g. For `/:id(\\d+)+/edit`, it would be `(\\d+)+/edit`. This should output
155116
* everything after the regex while handling escaped `)`: `+/edit`. Note this type should be used with a string that
@@ -173,19 +134,6 @@ export type _StripRegex<S extends string> =
173134
: // nothing to remove
174135
S
175136

176-
const a = '/:id(\\d+)+/edit/:more(.*)' as '/:id+/edit/:more'
177-
178-
type r1 = _StripRegex<'(\\d+)+/edit/'>
179-
type r3 = _StripRegex<'(.*)*'>
180-
type r4 = _StripRegex<'?/rest'>
181-
type r5 = _StripRegex<'*'>
182-
type r6 = _StripRegex<'-other-stuff'>
183-
type r7 = _StripRegex<'/edit'>
184-
185-
// type r8 = _StripRegex<'?/rest/:other(.*)'>
186-
// type r9 = _StripRegex<'(\\d+)+/edit/:other(.*)*'>
187-
// type r10 = _StripRegex<'?/rest/:other(.*)/more/:b(.*)'>
188-
189137
/**
190138
* Helper type to infer a modifier extraction result.
191139
*
@@ -217,12 +165,6 @@ export type _ExtractModifier<P extends string> =
217165
: // No modifier present
218166
_ModifierExtracTResult<'', P>
219167

220-
type m1 = _ExtractModifier<''>
221-
type m2 = _ExtractModifier<'-rest'>
222-
type m3 = _ExtractModifier<'edit'>
223-
type m4 = _ExtractModifier<'+'>
224-
type m5 = _ExtractModifier<'+/edit'>
225-
226168
/**
227169
* Gets the possible type of a param based on its modifier M.
228170
*
@@ -308,66 +250,12 @@ export type _RemoveUntilClosingPar<S extends string> =
308250
S extends `${infer A}\\)${infer Rest}`
309251
? // the actual regexp finished before, A has no escaped )
310252
A extends `${string})${infer Rest2}`
311-
? Rest2 extends `${_ParamModifier}${infer Rest3}`
312-
? Rest2 extends `${infer M}${Rest3}`
313-
? `${M}}${Rest3}\\)${Rest}`
314-
: never
315-
: `}${Rest2}\\)${Rest}` // job done
253+
? `${Rest2}\\)${Rest}` // job done
316254
: _RemoveUntilClosingPar<Rest> // we keep removing
317255
: S extends `${string})${infer Rest}`
318-
? Rest extends `${_ParamModifier}${infer Rest2}`
319-
? Rest extends `${infer M}${Rest2}`
320-
? `${M}}${Rest2}`
321-
: never
322-
: `}${Rest}`
256+
? Rest
323257
: never // nothing to remove, should not have been called, easier to spot bugs
324258

325-
type r = _RemoveUntilClosingPar<`aouest)/end`>
326-
type r2 = _RemoveUntilClosingPar<`aouest`>
327-
328-
/**
329-
* Reformats a path string `/:id(custom-regex)/:other+` by wrapping params with
330-
* `{}` and removing custom regexps to make them easier to parse.
331-
*
332-
* @internal
333-
*/
334-
export type _RemoveRegexpFromParam<S extends string> =
335-
S extends `${infer A}:${infer P}${_ParamDelimiter}${infer Rest}`
336-
? P extends _ExtractFirstParamName<P>
337-
? S extends `${A}:${P}${infer D}${Rest}`
338-
? D extends _ParamModifier | ''
339-
? `${A}{${P}${D}}${S extends `${A}:${P}${D}${infer Rest2}` // we need to infer again...
340-
? _RemoveRegexpFromParam<Rest2>
341-
: never}`
342-
: D extends _ParamDelimiter
343-
? '(' extends D
344-
? `${A}{${P}${S extends `${A}:${P}(${infer Rest2}` // we need to infer again to include D
345-
? _RemoveRegexpFromParam<_RemoveUntilClosingPar<Rest2>>
346-
: '}'}`
347-
: `${A}{${P}}${S extends `${A}:${P}${infer Rest2}` // we need to infer again to include D
348-
? _RemoveRegexpFromParam<Rest2>
349-
: never}`
350-
: never
351-
: never
352-
: never
353-
: S extends `${infer A}:${infer P}`
354-
? P extends _ExtractFirstParamName<P>
355-
? `${A}{${P}}`
356-
: never
357-
: S
358-
359-
/**
360-
* Extract the first param name (after a `:`) and ignores the rest.
361-
*
362-
* @internal
363-
*/
364-
export type _ExtractFirstParamName<S extends string> =
365-
S extends `${infer P}${_ParamDelimiter}${string}`
366-
? _ExtractFirstParamName<P>
367-
: S extends `${string}${_ParamDelimiter}${string}`
368-
? never
369-
: S
370-
371259
/**
372260
* Joins a prefix and a path putting a `/` between them when necessary
373261
*

packages/router/test-dts/paths.test-d.ts

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import type {
22
ParamsFromPath,
3-
_ExtractFirstParamName,
4-
_RemoveRegexpFromParam,
3+
_StripRegex,
4+
_ExtractParamsOfPath,
55
_RemoveUntilClosingPar,
6+
_ExtractModifier,
7+
_ModifierExtracTResult,
68
} from './'
79
import { expectType } from './'
810

@@ -11,7 +13,7 @@ function params<T extends string>(_path: T): ParamsFromPath<T> {
1113
}
1214

1315
// simple
14-
expectType<{}>(params('/static'))
16+
expectType<Record<any, never>>(params('/static'))
1517
expectType<{ id: string }>(params('/users/:id'))
1618
// simulate a part of the string unknown at compilation time
1719
expectType<{ id: string }>(params(`/${encodeURI('')}/:id`))
@@ -63,6 +65,13 @@ expectType<{ id: readonly string[] }>(params('/users/:id(one)+'))
6365
expectType<{ date: string }>(params('/users/:date(\\d{4}-\\d{2}-\\d{2})'))
6466
expectType<{ a: string }>(params('/:a(pre-(?:\\d{0,5}\\)-end)'))
6567

68+
expectType<{
69+
id: readonly [string, ...string[]]
70+
b: string
71+
c: string
72+
d: string
73+
}>(params('/:id(.*)+/other/:b/:c/:d'))
74+
6675
// special characters
6776
expectType<{ id: string }>(params('/:id$thing'))
6877
expectType<{ id: string }>(params('/:id&thing'))
@@ -83,54 +92,52 @@ function removeUntilClosingPar<S extends string>(
8392
return '' as any
8493
}
8594

86-
expectType<'}'>(removeUntilClosingPar(')'))
87-
expectType<'+}'>(removeUntilClosingPar(')+'))
88-
expectType<'}more'>(removeUntilClosingPar(')more'))
89-
expectType<'}'>(removeUntilClosingPar('\\w+)'))
90-
expectType<'}/more-url'>(removeUntilClosingPar('\\w+)/more-url'))
91-
expectType<'}/:p'>(removeUntilClosingPar('\\w+)/:p'))
92-
expectType<'+}'>(removeUntilClosingPar('oe)+'))
93-
expectType<'}/:p(o)'>(removeUntilClosingPar('\\w+)/:p(o)'))
94-
expectType<'}/:p(o)'>(removeUntilClosingPar('(?:no\\)?-end)/:p(o)'))
95-
expectType<'}/:p(o(?:no\\)?-end)'>(
95+
expectType<''>(removeUntilClosingPar(')'))
96+
expectType<'+'>(removeUntilClosingPar(')+'))
97+
expectType<'more'>(removeUntilClosingPar(')more'))
98+
expectType<''>(removeUntilClosingPar('\\w+)'))
99+
expectType<'/more-url'>(removeUntilClosingPar('\\w+)/more-url'))
100+
expectType<'/:p'>(removeUntilClosingPar('\\w+)/:p'))
101+
expectType<'+'>(removeUntilClosingPar('oe)+'))
102+
expectType<'/:p(o)'>(removeUntilClosingPar('\\w+)/:p(o)'))
103+
expectType<'/:p(o)'>(removeUntilClosingPar('(?:no\\)?-end)/:p(o)'))
104+
expectType<'/:p(o(?:no\\)?-end)'>(
96105
removeUntilClosingPar('-end)/:p(o(?:no\\)?-end)')
97106
)
98-
expectType<'}:new(eg)other'>(removeUntilClosingPar('customr):new(eg)other'))
99-
expectType<'}:new(eg)+other'>(removeUntilClosingPar('customr):new(eg)+other'))
100-
expectType<'}/:new(eg)+other'>(removeUntilClosingPar('customr)/:new(eg)+other'))
101-
expectType<'?}/:new(eg)+other'>(
107+
expectType<':new(eg)other'>(removeUntilClosingPar('customr):new(eg)other'))
108+
expectType<':new(eg)+other'>(removeUntilClosingPar('customr):new(eg)+other'))
109+
expectType<'/:new(eg)+other'>(removeUntilClosingPar('customr)/:new(eg)+other'))
110+
expectType<'?/:new(eg)+other'>(
102111
removeUntilClosingPar('customr)?/:new(eg)+other')
103112
)
104-
function removeRegexp<S extends string>(_s: S): _RemoveRegexpFromParam<S> {
113+
114+
function stripRegex<S extends string>(_s: S): _StripRegex<S> {
105115
return '' as any
106116
}
107117

108-
expectType<'/{id?}/{b}'>(removeRegexp('/:id(aue(ee{2,3}\\))?/:b(hey)'))
109-
expectType<'/{id+}/b'>(removeRegexp('/:id+/b'))
110-
expectType<'/{id}'>(removeRegexp('/:id'))
111-
expectType<'/{id+}'>(removeRegexp('/:id+'))
112-
expectType<'+}'>(removeRegexp('+}'))
113-
expectType<'/{id+}'>(removeRegexp('/:id(e)+'))
114-
expectType<'/{id}/b'>(removeRegexp('/:id/b'))
115-
expectType<'/{id}/{b}'>(removeRegexp('/:id/:b'))
116-
expectType<'/users/{id}/{b}'>(removeRegexp('/users/:id/:b'))
117-
expectType<'/{id?}/{b+}'>(removeRegexp('/:id?/:b+'))
118-
expectType<'/{id?}/{b+}'>(removeRegexp('/:id(aue(ee{2,3}\\))?/:b+'))
119-
120-
function extractParamName<S extends string>(_s: S): _ExtractFirstParamName<S> {
121-
return '' as any
118+
const a = '/:id(\\d+)+/edit/:more(.*)' as '/:id+/edit/:more'
119+
120+
expectType<'+/edit/'>(stripRegex('(\\d+)+/edit/'))
121+
expectType<'*'>(stripRegex('(.*)*'))
122+
expectType<'?/rest'>(stripRegex('?/rest'))
123+
expectType<'*'>(stripRegex('*'))
124+
expectType<'-other-stuff'>(stripRegex('-other-stuff'))
125+
expectType<'/edit'>(stripRegex('/edit'))
126+
expectType<'?/rest/:other(.*)*'>(stripRegex('?/rest/:other(.*)*'))
127+
expectType<'+/edit/:other(.*)*'>(stripRegex('(\\d+)+/edit/:other(.*)*'))
128+
expectType<'?/rest/:other(.*)/more/:b(.*)'>(
129+
stripRegex('?/rest/:other(.*)/more/:b(.*)')
130+
)
131+
132+
function extractModifier<S extends string>(_s: S): _ExtractModifier<S> {
133+
return {} as any
122134
}
123135

124-
expectType<'id'>(extractParamName('id(aue(ee{2,3}\\))?/:b(hey)'))
125-
expectType<'id'>(extractParamName('id(e)+:d(c)'))
126-
expectType<'id'>(extractParamName('id(e)/:d(c)'))
127-
expectType<'id'>(extractParamName('id:d'))
128-
expectType<'id'>(extractParamName('id/:d'))
129-
expectType<'id'>(extractParamName('id?/other/:d'))
130-
expectType<'id'>(extractParamName('id/b'))
131-
expectType<'id'>(extractParamName('id+'))
132-
expectType<'id'>(extractParamName('id'))
133-
expectType<'id'>(extractParamName('id-u'))
134-
expectType<'id'>(extractParamName('id:u'))
135-
expectType<'id'>(extractParamName('id(o(\\)e)o'))
136-
expectType<'id'>(extractParamName('id(o(\\)e)?o'))
136+
expectType<_ModifierExtracTResult<'', ''>>(extractModifier(''))
137+
expectType<_ModifierExtracTResult<'', '-rest'>>(extractModifier('-rest'))
138+
expectType<_ModifierExtracTResult<'', 'edit'>>(extractModifier('edit'))
139+
expectType<_ModifierExtracTResult<'+', ''>>(extractModifier('+'))
140+
expectType<_ModifierExtracTResult<'+', '/edit'>>(extractModifier('+/edit'))
141+
expectType<_ModifierExtracTResult<'+', '/edit/:a?'>>(
142+
extractModifier('+/edit/:a?')
143+
)

0 commit comments

Comments
 (0)