]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
refactor(types): move tests of types
authorEduardo San Martin Morote <posva13@gmail.com>
Tue, 14 Jun 2022 10:37:20 +0000 (12:37 +0200)
committerEduardo San Martin Morote <posva@users.noreply.github.com>
Thu, 30 Jun 2022 07:59:00 +0000 (09:59 +0200)
packages/router/src/index.ts
packages/router/src/types/named.ts
packages/router/src/types/paths.ts
packages/router/test-dts/paths.test-d.ts

index d27753500f5085781dd6d10222c15fda1a1c858c..e25726ed04eb14656ba99b59c5eec6ec053e60b3 100644 (file)
@@ -64,9 +64,13 @@ export type {
 } from './types'
 export type {
   ParamsFromPath,
-  _ExtractFirstParamName,
-  _RemoveRegexpFromParam,
+  ParamsRawFromPath,
+  _StripRegex,
   _RemoveUntilClosingPar,
+  _ExtractParamsOfPath,
+  _ParamExtractResult,
+  _ExtractModifier,
+  _ModifierExtracTResult,
   _JoinPath,
   _ParamDelimiter,
   _ParamModifier,
index 285c554ff7f8b3ea4ff971f7b2b0c61abd764dbf..d76ab309853b3234d232806106d61d01d0bf82d4 100644 (file)
@@ -4,13 +4,7 @@ import type {
   RouteRecordRaw,
   RouteRecordName,
 } from '.'
-import type {
-  _JoinPath,
-  ParamsFromPath,
-  ParamsRawFromPath,
-  PathFromParams,
-} from './paths'
-import { LiteralUnion } from './utils'
+import type { _JoinPath, ParamsFromPath, ParamsRawFromPath } from './paths'
 
 /**
  * Creates a map with each named route as a properties. Each property contains the type of the params in raw and
index 1b269eda522dc7e6ebb80e19747d4e78b2f8ab7a..87cbc1ac7070110a4de52a1f8e0d1533d1cee00e 100644 (file)
@@ -54,26 +54,7 @@ export type _ParamDelimiter =
   | _ParamModifier
 
 /**
- * Given a simple path, creates an object of the possible param values.
- *
- * @internal
- */
-export type _ExtractParamsPath<
-  P extends string,
-  isRaw extends boolean
-> = P extends `${string}{${infer PP}}${infer Rest}`
-  ? (PP extends `${infer N}${_ParamModifier}`
-      ? PP extends `${N}${infer M}`
-        ? M extends _ParamModifier
-          ? _ParamToObject<N, M, isRaw>
-          : never
-        : never
-      : _ParamToObject<PP, '', isRaw>) &
-      _ExtractParamsPath<Rest, isRaw>
-  : {}
-
-/**
- * Given a path, extracts the possible params or {} when there are no params.
+ * Given a path, extracts the possible params or \{\} when there are no params.
  *
  * @internal
  */
@@ -93,34 +74,18 @@ export type _ExtractParamsOfPath<
       >
       ? _ParamToObject<ParamName, Modifier, isRaw> &
           _ExtractParamsOfPath<Rest2, isRaw>
-      : {
-          NO: 1 // this should never happen as the modifier can be empty
-        }
+      : never // this should never happen as the modifier can be empty
     : // Nothing after the param: /:id, we are done
       _ParamToObject<HasParam, '', isRaw>
   : {
       // EMPTY: 1
     }
 
-type a1 = _ExtractParamsOfPath<'/', false>
-type a2 = _ExtractParamsOfPath<'/:id', false>
-type a3 = _ExtractParamsOfPath<'/:id/:b', false>
-type a4 = _ExtractParamsOfPath<'/:id(.*)', false>
-type a5 = _ExtractParamsOfPath<'/:id(.*)/other', false>
-type a6 = _ExtractParamsOfPath<'/:id(.*)+', false>
-type a7 = _ExtractParamsOfPath<'/:id(.*)+/other', false>
-type a8 = _ExtractParamsOfPath<'/:id(.*)+/other/:b/:c/:d', false>
-
-type test1 =
-  '/:id/:b' extends `${string}:${infer P}${_ParamDelimiter}${infer Rest}`
-    ? [P, Rest]
-    : never
-
 /**
  * Helper type to infer a param name extraction result
  * @internal
  */
-interface _ParamExtractResult<P extends string, Rest extends string> {
+export interface _ParamExtractResult<P extends string, Rest extends string> {
   param: P
   rest: Rest
 }
@@ -146,10 +111,6 @@ type _ExtractParamName<
   : // add the rest to the end after a % which is invalid in a path so it can be used as a delimiter
     _ParamExtractResult<Head, Tail>
 
-type p1 = _ExtractParamName<'id'>
-type p2 = _ExtractParamName<'abc+/dos'>
-type p3 = _ExtractParamName<'abc/:dos)'>
-
 /**
  * We consider a what comes after a param, e.g. For `/:id(\\d+)+/edit`, it would be `(\\d+)+/edit`. This should output
  * 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> =
     : // nothing to remove
       S
 
-const a = '/:id(\\d+)+/edit/:more(.*)' as '/:id+/edit/:more'
-
-type r1 = _StripRegex<'(\\d+)+/edit/'>
-type r3 = _StripRegex<'(.*)*'>
-type r4 = _StripRegex<'?/rest'>
-type r5 = _StripRegex<'*'>
-type r6 = _StripRegex<'-other-stuff'>
-type r7 = _StripRegex<'/edit'>
-
-// type r8 = _StripRegex<'?/rest/:other(.*)'>
-// type r9 = _StripRegex<'(\\d+)+/edit/:other(.*)*'>
-// type r10 = _StripRegex<'?/rest/:other(.*)/more/:b(.*)'>
-
 /**
  * Helper type to infer a modifier extraction result.
  *
@@ -217,12 +165,6 @@ export type _ExtractModifier<P extends string> =
     : // No modifier present
       _ModifierExtracTResult<'', P>
 
-type m1 = _ExtractModifier<''>
-type m2 = _ExtractModifier<'-rest'>
-type m3 = _ExtractModifier<'edit'>
-type m4 = _ExtractModifier<'+'>
-type m5 = _ExtractModifier<'+/edit'>
-
 /**
  * Gets the possible type of a param based on its modifier M.
  *
@@ -308,66 +250,12 @@ export type _RemoveUntilClosingPar<S extends string> =
   S extends `${infer A}\\)${infer Rest}`
     ? // the actual regexp finished before, A has no escaped )
       A extends `${string})${infer Rest2}`
-      ? Rest2 extends `${_ParamModifier}${infer Rest3}`
-        ? Rest2 extends `${infer M}${Rest3}`
-          ? `${M}}${Rest3}\\)${Rest}`
-          : never
-        : `}${Rest2}\\)${Rest}` // job done
+      ? `${Rest2}\\)${Rest}` // job done
       : _RemoveUntilClosingPar<Rest> // we keep removing
     : S extends `${string})${infer Rest}`
-    ? Rest extends `${_ParamModifier}${infer Rest2}`
-      ? Rest extends `${infer M}${Rest2}`
-        ? `${M}}${Rest2}`
-        : never
-      : `}${Rest}`
+    ? Rest
     : never // nothing to remove, should not have been called, easier to spot bugs
 
-type r = _RemoveUntilClosingPar<`aouest)/end`>
-type r2 = _RemoveUntilClosingPar<`aouest`>
-
-/**
- * Reformats a path string `/:id(custom-regex)/:other+` by wrapping params with
- * `{}` and removing custom regexps to make them easier to parse.
- *
- * @internal
- */
-export type _RemoveRegexpFromParam<S extends string> =
-  S extends `${infer A}:${infer P}${_ParamDelimiter}${infer Rest}`
-    ? P extends _ExtractFirstParamName<P>
-      ? S extends `${A}:${P}${infer D}${Rest}`
-        ? D extends _ParamModifier | ''
-          ? `${A}{${P}${D}}${S extends `${A}:${P}${D}${infer Rest2}` // we need to infer again...
-              ? _RemoveRegexpFromParam<Rest2>
-              : never}`
-          : D extends _ParamDelimiter
-          ? '(' extends D
-            ? `${A}{${P}${S extends `${A}:${P}(${infer Rest2}` // we need to infer again to include D
-                ? _RemoveRegexpFromParam<_RemoveUntilClosingPar<Rest2>>
-                : '}'}`
-            : `${A}{${P}}${S extends `${A}:${P}${infer Rest2}` // we need to infer again to include D
-                ? _RemoveRegexpFromParam<Rest2>
-                : never}`
-          : never
-        : never
-      : never
-    : S extends `${infer A}:${infer P}`
-    ? P extends _ExtractFirstParamName<P>
-      ? `${A}{${P}}`
-      : never
-    : S
-
-/**
- * Extract the first param name (after a `:`) and ignores the rest.
- *
- * @internal
- */
-export type _ExtractFirstParamName<S extends string> =
-  S extends `${infer P}${_ParamDelimiter}${string}`
-    ? _ExtractFirstParamName<P>
-    : S extends `${string}${_ParamDelimiter}${string}`
-    ? never
-    : S
-
 /**
  * Joins a prefix and a path putting a `/` between them when necessary
  *
index 7b6d6f6805fd4bdda88cba0db5a6594841d20e08..9555528dcb3c006b8189d1e631c0d6344a7710ac 100644 (file)
@@ -1,8 +1,10 @@
 import type {
   ParamsFromPath,
-  _ExtractFirstParamName,
-  _RemoveRegexpFromParam,
+  _StripRegex,
+  _ExtractParamsOfPath,
   _RemoveUntilClosingPar,
+  _ExtractModifier,
+  _ModifierExtracTResult,
 } from './'
 import { expectType } from './'
 
@@ -11,7 +13,7 @@ function params<T extends string>(_path: T): ParamsFromPath<T> {
 }
 
 // simple
-expectType<{}>(params('/static'))
+expectType<Record<any, never>>(params('/static'))
 expectType<{ id: string }>(params('/users/:id'))
 // simulate a part of the string unknown at compilation time
 expectType<{ id: string }>(params(`/${encodeURI('')}/:id`))
@@ -63,6 +65,13 @@ expectType<{ id: readonly string[] }>(params('/users/:id(one)+'))
 expectType<{ date: string }>(params('/users/:date(\\d{4}-\\d{2}-\\d{2})'))
 expectType<{ a: string }>(params('/:a(pre-(?:\\d{0,5}\\)-end)'))
 
+expectType<{
+  id: readonly [string, ...string[]]
+  b: string
+  c: string
+  d: string
+}>(params('/:id(.*)+/other/:b/:c/:d'))
+
 // special characters
 expectType<{ id: string }>(params('/:id$thing'))
 expectType<{ id: string }>(params('/:id&thing'))
@@ -83,54 +92,52 @@ function removeUntilClosingPar<S extends string>(
   return '' as any
 }
 
-expectType<'}'>(removeUntilClosingPar(')'))
-expectType<'+}'>(removeUntilClosingPar(')+'))
-expectType<'}more'>(removeUntilClosingPar(')more'))
-expectType<'}'>(removeUntilClosingPar('\\w+)'))
-expectType<'}/more-url'>(removeUntilClosingPar('\\w+)/more-url'))
-expectType<'}/:p'>(removeUntilClosingPar('\\w+)/:p'))
-expectType<'+}'>(removeUntilClosingPar('oe)+'))
-expectType<'}/:p(o)'>(removeUntilClosingPar('\\w+)/:p(o)'))
-expectType<'}/:p(o)'>(removeUntilClosingPar('(?:no\\)?-end)/:p(o)'))
-expectType<'}/:p(o(?:no\\)?-end)'>(
+expectType<''>(removeUntilClosingPar(')'))
+expectType<'+'>(removeUntilClosingPar(')+'))
+expectType<'more'>(removeUntilClosingPar(')more'))
+expectType<''>(removeUntilClosingPar('\\w+)'))
+expectType<'/more-url'>(removeUntilClosingPar('\\w+)/more-url'))
+expectType<'/:p'>(removeUntilClosingPar('\\w+)/:p'))
+expectType<'+'>(removeUntilClosingPar('oe)+'))
+expectType<'/:p(o)'>(removeUntilClosingPar('\\w+)/:p(o)'))
+expectType<'/:p(o)'>(removeUntilClosingPar('(?:no\\)?-end)/:p(o)'))
+expectType<'/:p(o(?:no\\)?-end)'>(
   removeUntilClosingPar('-end)/:p(o(?:no\\)?-end)')
 )
-expectType<'}:new(eg)other'>(removeUntilClosingPar('customr):new(eg)other'))
-expectType<'}:new(eg)+other'>(removeUntilClosingPar('customr):new(eg)+other'))
-expectType<'}/:new(eg)+other'>(removeUntilClosingPar('customr)/:new(eg)+other'))
-expectType<'?}/:new(eg)+other'>(
+expectType<':new(eg)other'>(removeUntilClosingPar('customr):new(eg)other'))
+expectType<':new(eg)+other'>(removeUntilClosingPar('customr):new(eg)+other'))
+expectType<'/:new(eg)+other'>(removeUntilClosingPar('customr)/:new(eg)+other'))
+expectType<'?/:new(eg)+other'>(
   removeUntilClosingPar('customr)?/:new(eg)+other')
 )
-function removeRegexp<S extends string>(_s: S): _RemoveRegexpFromParam<S> {
+
+function stripRegex<S extends string>(_s: S): _StripRegex<S> {
   return '' as any
 }
 
-expectType<'/{id?}/{b}'>(removeRegexp('/:id(aue(ee{2,3}\\))?/:b(hey)'))
-expectType<'/{id+}/b'>(removeRegexp('/:id+/b'))
-expectType<'/{id}'>(removeRegexp('/:id'))
-expectType<'/{id+}'>(removeRegexp('/:id+'))
-expectType<'+}'>(removeRegexp('+}'))
-expectType<'/{id+}'>(removeRegexp('/:id(e)+'))
-expectType<'/{id}/b'>(removeRegexp('/:id/b'))
-expectType<'/{id}/{b}'>(removeRegexp('/:id/:b'))
-expectType<'/users/{id}/{b}'>(removeRegexp('/users/:id/:b'))
-expectType<'/{id?}/{b+}'>(removeRegexp('/:id?/:b+'))
-expectType<'/{id?}/{b+}'>(removeRegexp('/:id(aue(ee{2,3}\\))?/:b+'))
-
-function extractParamName<S extends string>(_s: S): _ExtractFirstParamName<S> {
-  return '' as any
+const a = '/:id(\\d+)+/edit/:more(.*)' as '/:id+/edit/:more'
+
+expectType<'+/edit/'>(stripRegex('(\\d+)+/edit/'))
+expectType<'*'>(stripRegex('(.*)*'))
+expectType<'?/rest'>(stripRegex('?/rest'))
+expectType<'*'>(stripRegex('*'))
+expectType<'-other-stuff'>(stripRegex('-other-stuff'))
+expectType<'/edit'>(stripRegex('/edit'))
+expectType<'?/rest/:other(.*)*'>(stripRegex('?/rest/:other(.*)*'))
+expectType<'+/edit/:other(.*)*'>(stripRegex('(\\d+)+/edit/:other(.*)*'))
+expectType<'?/rest/:other(.*)/more/:b(.*)'>(
+  stripRegex('?/rest/:other(.*)/more/:b(.*)')
+)
+
+function extractModifier<S extends string>(_s: S): _ExtractModifier<S> {
+  return {} as any
 }
 
-expectType<'id'>(extractParamName('id(aue(ee{2,3}\\))?/:b(hey)'))
-expectType<'id'>(extractParamName('id(e)+:d(c)'))
-expectType<'id'>(extractParamName('id(e)/:d(c)'))
-expectType<'id'>(extractParamName('id:d'))
-expectType<'id'>(extractParamName('id/:d'))
-expectType<'id'>(extractParamName('id?/other/:d'))
-expectType<'id'>(extractParamName('id/b'))
-expectType<'id'>(extractParamName('id+'))
-expectType<'id'>(extractParamName('id'))
-expectType<'id'>(extractParamName('id-u'))
-expectType<'id'>(extractParamName('id:u'))
-expectType<'id'>(extractParamName('id(o(\\)e)o'))
-expectType<'id'>(extractParamName('id(o(\\)e)?o'))
+expectType<_ModifierExtracTResult<'', ''>>(extractModifier(''))
+expectType<_ModifierExtracTResult<'', '-rest'>>(extractModifier('-rest'))
+expectType<_ModifierExtracTResult<'', 'edit'>>(extractModifier('edit'))
+expectType<_ModifierExtracTResult<'+', ''>>(extractModifier('+'))
+expectType<_ModifierExtracTResult<'+', '/edit'>>(extractModifier('+/edit'))
+expectType<_ModifierExtracTResult<'+', '/edit/:a?'>>(
+  extractModifier('+/edit/:a?')
+)