Skip to content

Commit

Permalink
Ensure consistent Prettier formatting (including *.ts files).
Browse files Browse the repository at this point in the history
  • Loading branch information
molefrog committed May 10, 2023
1 parent c743cb3 commit c79668b
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 93 deletions.
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
}
},
"scripts": {
"fix:p": "prettier --write './**/*.js{x,}'",
"fix:p": "prettier --write './**/*.(js|ts){x,}'",
"test": "jest --verbose --coverage",
"size": "size-limit",
"build": "npm run bundle",
Expand All @@ -95,6 +95,12 @@
"use-sync-external-store": "^1.0.0"
},
"repository": "molefrog/wouter",
"prettier": {
"tabWidth": 2,
"semi": true,
"singleQuote": false,
"printWidth": 80
},
"size-limit": [
{
"path": "index.js",
Expand Down
9 changes: 7 additions & 2 deletions preact/types/matcher.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ export interface DefaultParams {

export type Params<T extends DefaultParams = DefaultParams> = T;

export type MatchWithParams<T extends DefaultParams = DefaultParams> = [true, Params<T>];
export type MatchWithParams<T extends DefaultParams = DefaultParams> = [
true,
Params<T>
];
export type NoMatch = [false, null];
export type Match<T extends DefaultParams = DefaultParams> = MatchWithParams<T> | NoMatch;
export type Match<T extends DefaultParams = DefaultParams> =
| MatchWithParams<T>
| NoMatch;

export type MatcherFn = (pattern: Path, path: Path) => Match;

Expand Down
72 changes: 45 additions & 27 deletions preact/types/ts4.1/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
// Minimum TypeScript Version: 4.1
// tslint:disable:no-unnecessary-generics

import { JSX, FunctionComponent, ComponentType, ComponentChildren, VNode } from "preact";
import {
JSX,
FunctionComponent,
ComponentType,
ComponentChildren,
VNode,
} from "preact";

import {
Path,
Expand All @@ -17,25 +23,28 @@ import { DefaultParams, Match, MatcherFn } from "../matcher";
export * from "../matcher";
export * from "../use-location";

export type ExtractRouteOptionalParam<PathType extends Path> = PathType extends `${infer Param}?`
? { readonly [k in Param]: string | undefined }
: PathType extends `${infer Param}*`
? { readonly [k in Param]: string | undefined }
: PathType extends `${infer Param}+`
? { readonly [k in Param]: string }
: { readonly [k in PathType]: string };

export type ExtractRouteParams<PathType extends string> = string extends PathType
? DefaultParams
: PathType extends `${infer _Start}:${infer ParamWithOptionalRegExp}/${infer Rest}`
? ParamWithOptionalRegExp extends `${infer Param}(${infer _RegExp})`
? ExtractRouteOptionalParam<Param> & ExtractRouteParams<Rest>
: ExtractRouteOptionalParam<ParamWithOptionalRegExp> & ExtractRouteParams<Rest>
: PathType extends `${infer _Start}:${infer ParamWithOptionalRegExp}`
? ParamWithOptionalRegExp extends `${infer Param}(${infer _RegExp})`
? ExtractRouteOptionalParam<Param>
: ExtractRouteOptionalParam<ParamWithOptionalRegExp>
: {};
export type ExtractRouteOptionalParam<PathType extends Path> =
PathType extends `${infer Param}?`
? { readonly [k in Param]: string | undefined }
: PathType extends `${infer Param}*`
? { readonly [k in Param]: string | undefined }
: PathType extends `${infer Param}+`
? { readonly [k in Param]: string }
: { readonly [k in PathType]: string };

export type ExtractRouteParams<PathType extends string> =
string extends PathType
? DefaultParams
: PathType extends `${infer _Start}:${infer ParamWithOptionalRegExp}/${infer Rest}`
? ParamWithOptionalRegExp extends `${infer Param}(${infer _RegExp})`
? ExtractRouteOptionalParam<Param> & ExtractRouteParams<Rest>
: ExtractRouteOptionalParam<ParamWithOptionalRegExp> &
ExtractRouteParams<Rest>
: PathType extends `${infer _Start}:${infer ParamWithOptionalRegExp}`
? ParamWithOptionalRegExp extends `${infer Param}(${infer _RegExp})`
? ExtractRouteOptionalParam<Param>
: ExtractRouteOptionalParam<ParamWithOptionalRegExp>
: {};

/*
* Components: <Route />
Expand All @@ -50,11 +59,15 @@ export interface RouteProps<
RoutePath extends Path = Path
> {
children?:
| ((params: T extends DefaultParams ? T : ExtractRouteParams<RoutePath>) => ComponentChildren)
| ((
params: T extends DefaultParams ? T : ExtractRouteParams<RoutePath>
) => ComponentChildren)
| ComponentChildren;
path?: RoutePath;
component?: ComponentType<
RouteComponentProps<T extends DefaultParams ? T : ExtractRouteParams<RoutePath>>
RouteComponentProps<
T extends DefaultParams ? T : ExtractRouteParams<RoutePath>
>
>;
}

Expand All @@ -79,9 +92,10 @@ export type LinkProps<H extends BaseLocationHook = LocationHook> = Omit<
> &
NavigationalProps<H>;

export type RedirectProps<H extends BaseLocationHook = LocationHook> = NavigationalProps<H> & {
children?: never;
};
export type RedirectProps<H extends BaseLocationHook = LocationHook> =
NavigationalProps<H> & {
children?: never;
};

export function Link<H extends BaseLocationHook = LocationHook>(
props: LinkProps<H>
Expand Down Expand Up @@ -124,8 +138,12 @@ export function useRouter(): RouterProps;
export function useRoute<
T extends DefaultParams | undefined = undefined,
RoutePath extends Path = Path
>(pattern: RoutePath): Match<T extends DefaultParams ? T : ExtractRouteParams<RoutePath>>;
>(
pattern: RoutePath
): Match<T extends DefaultParams ? T : ExtractRouteParams<RoutePath>>;

export function useLocation<H extends BaseLocationHook = LocationHook>(): HookReturnValue<H>;
export function useLocation<
H extends BaseLocationHook = LocationHook
>(): HookReturnValue<H>;

// tslint:enable:no-unnecessary-generics
17 changes: 13 additions & 4 deletions preact/types/ts4.1/type-specs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,17 @@ const invalidParamsWithGeneric: Params<{ id: number }> = { id: 13 }; // $ExpectE
This is a <b>mixed</b> content
</Route>;

<Route path="/users/:id">{(params: Params): React.ReactNode => `User id: ${params.id}`}</Route>;
<Route path="/users/:id">
{(params: Params): React.ReactNode => `User id: ${params.id}`}
</Route>;

<Route path="/users/:id">{({ id }) => `User id: ${id}`}</Route>;

<Route path="/users/:id">{({ age }) => `User age: ${age}`}</Route>; // $ExpectError

<Route path="/users/:id">{({ age }: { age: string }) => `User age: ${age}`}</Route>;
<Route path="/users/:id">
{({ age }: { age: string }) => `User age: ${age}`}
</Route>;

<Route path="/app" match={true} />; // $ExpectError

Expand All @@ -79,7 +83,9 @@ const invalidParamsWithGeneric: Params<{ id: number }> = { id: 13 }; // $ExpectE
</Route>;

// infer only named params
<Route path="/:first/:second">{({ first, second }) => `first: ${first}, second: ${second}`}</Route>;
<Route path="/:first/:second">
{({ first, second }) => `first: ${first}, second: ${second}`}
</Route>;

// for pathToRegexp matcher
<Route path="/:user([a-z]i+)/profile/:tab/:first+/:second*">
Expand Down Expand Up @@ -150,7 +156,10 @@ const invalidParamsWithGeneric: Params<{ id: number }> = { id: 13 }; // $ExpectE

Redirect<UseNetworkLocation>({ href: "/home", delay: 1000 });
// example custom hook
type UseLocWithNoOptions = () => [string, (to: string, foo: number, bar: string) => void];
type UseLocWithNoOptions = () => [
string,
(to: string, foo: number, bar: string) => void
];
Redirect<UseLocWithNoOptions>({ href: "/app" });

<Redirect>something</Redirect>; // $ExpectError
Expand Down
33 changes: 21 additions & 12 deletions preact/types/use-location.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ export type Path = string;

// the base useLocation hook type. Any custom hook (including the
// default one) should inherit from it.
export type BaseLocationHook = (...args: any[]) => [Path, (path: Path, ...args: any[]) => any];
export type BaseLocationHook = (
...args: any[]
) => [Path, (path: Path, ...args: any[]) => any];

/*
* Utility types that operate on hook
Expand All @@ -16,24 +18,31 @@ export type BaseLocationHook = (...args: any[]) => [Path, (path: Path, ...args:
export type HookReturnValue<H extends BaseLocationHook> = ReturnType<H>;

// Returns the type of the navigation options that hook's push function accepts.
export type HookNavigationOptions<H extends BaseLocationHook> = HookReturnValue<H>[1] extends (
path: Path,
options: infer R,
...rest: any[]
) => any
? R extends { [k: string]: any }
? R
: {}
: {};
export type HookNavigationOptions<H extends BaseLocationHook> =
HookReturnValue<H>[1] extends (
path: Path,
options: infer R,
...rest: any[]
) => any
? R extends { [k: string]: any }
? R
: {}
: {};

type Primitive = string | number | bigint | boolean | null | undefined | symbol;
export const useLocationProperty: <S extends Primitive>(fn: () => S, ssrFn?: () => S) => S;
export const useLocationProperty: <S extends Primitive>(
fn: () => S,
ssrFn?: () => S
) => S;

export const useSearch: () => string;

export const usePathname: (options?: { ssrPath?: Path }) => Path;

export const navigate: (to: string | URL, options?: { replace?: boolean }) => void;
export const navigate: (
to: string | URL,
options?: { replace?: boolean }
) => void;

/*
* Default `useLocation`
Expand Down
9 changes: 7 additions & 2 deletions types/matcher.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ export interface DefaultParams {

export type Params<T extends DefaultParams = DefaultParams> = T;

export type MatchWithParams<T extends DefaultParams = DefaultParams> = [true, Params<T>];
export type MatchWithParams<T extends DefaultParams = DefaultParams> = [
true,
Params<T>
];
export type NoMatch = [false, null];
export type Match<T extends DefaultParams = DefaultParams> = MatchWithParams<T> | NoMatch;
export type Match<T extends DefaultParams = DefaultParams> =
| MatchWithParams<T>
| NoMatch;

export type MatcherFn = (pattern: Path, path: Path) => Match;

Expand Down
9 changes: 7 additions & 2 deletions types/ts3.9.4/type-specs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ const invalidParamsWithGeneric: Params<{ id: number }> = { id: 13 }; // $ExpectE

<Route<{ id: string }> path="/users/:id">{({ id }) => `User id: ${id}`}</Route>;

<Route<{ id: string }> path="/users/:id">{({ age }) => `User age: ${age}`}</Route>; // $ExpectError
<Route<{ id: string }> path="/users/:id">
{({ age }) => `User age: ${age}`}
</Route>; // $ExpectError

<Route path="/app" match={true} />; // $ExpectError

Expand Down Expand Up @@ -127,7 +129,10 @@ const invalidParamsWithGeneric: Params<{ id: number }> = { id: 13 }; // $ExpectE

Redirect<UseNetworkLocation>({ href: "/home", delay: 1000 });
// example custom hook
type UseLocWithNoOptions = () => [string, (to: string, foo: number, bar: string) => void];
type UseLocWithNoOptions = () => [
string,
(to: string, foo: number, bar: string) => void
];
Redirect<UseLocWithNoOptions>({ href: "/app" });

<Redirect>something</Redirect>; // $ExpectError
Expand Down
74 changes: 46 additions & 28 deletions types/ts4.1/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,36 @@ export * from "../router";
// React <18 only: fixes incorrect `ReactNode` declaration that had `{}` in the union.
// This issue has been fixed in React 18 type declaration.
// https://github.com/DefinitelyTyped/DefinitelyTyped/pull/56210
type ReactNode = ReactChild | Iterable<ReactNode> | ReactPortal | boolean | null | undefined;

export type ExtractRouteOptionalParam<PathType extends Path> = PathType extends `${infer Param}?`
? { readonly [k in Param]: string | undefined }
: PathType extends `${infer Param}*`
? { readonly [k in Param]: string | undefined }
: PathType extends `${infer Param}+`
? { readonly [k in Param]: string }
: { readonly [k in PathType]: string };

export type ExtractRouteParams<PathType extends string> = string extends PathType
? DefaultParams
: PathType extends `${infer _Start}:${infer ParamWithOptionalRegExp}/${infer Rest}`
? ParamWithOptionalRegExp extends `${infer Param}(${infer _RegExp})`
? ExtractRouteOptionalParam<Param> & ExtractRouteParams<Rest>
: ExtractRouteOptionalParam<ParamWithOptionalRegExp> & ExtractRouteParams<Rest>
: PathType extends `${infer _Start}:${infer ParamWithOptionalRegExp}`
? ParamWithOptionalRegExp extends `${infer Param}(${infer _RegExp})`
? ExtractRouteOptionalParam<Param>
: ExtractRouteOptionalParam<ParamWithOptionalRegExp>
: {};
type ReactNode =
| ReactChild
| Iterable<ReactNode>
| ReactPortal
| boolean
| null
| undefined;

export type ExtractRouteOptionalParam<PathType extends Path> =
PathType extends `${infer Param}?`
? { readonly [k in Param]: string | undefined }
: PathType extends `${infer Param}*`
? { readonly [k in Param]: string | undefined }
: PathType extends `${infer Param}+`
? { readonly [k in Param]: string }
: { readonly [k in PathType]: string };

export type ExtractRouteParams<PathType extends string> =
string extends PathType
? DefaultParams
: PathType extends `${infer _Start}:${infer ParamWithOptionalRegExp}/${infer Rest}`
? ParamWithOptionalRegExp extends `${infer Param}(${infer _RegExp})`
? ExtractRouteOptionalParam<Param> & ExtractRouteParams<Rest>
: ExtractRouteOptionalParam<ParamWithOptionalRegExp> &
ExtractRouteParams<Rest>
: PathType extends `${infer _Start}:${infer ParamWithOptionalRegExp}`
? ParamWithOptionalRegExp extends `${infer Param}(${infer _RegExp})`
? ExtractRouteOptionalParam<Param>
: ExtractRouteOptionalParam<ParamWithOptionalRegExp>
: {};

/*
* Components: <Route />
Expand All @@ -66,11 +75,15 @@ export interface RouteProps<
RoutePath extends Path = Path
> {
children?:
| ((params: T extends DefaultParams ? T : ExtractRouteParams<RoutePath>) => ReactNode)
| ((
params: T extends DefaultParams ? T : ExtractRouteParams<RoutePath>
) => ReactNode)
| ReactNode;
path?: RoutePath;
component?: ComponentType<
RouteComponentProps<T extends DefaultParams ? T : ExtractRouteParams<RoutePath>>
RouteComponentProps<
T extends DefaultParams ? T : ExtractRouteParams<RoutePath>
>
>;
}

Expand All @@ -95,9 +108,10 @@ export type LinkProps<H extends BaseLocationHook = LocationHook> = Omit<
> &
NavigationalProps<H>;

export type RedirectProps<H extends BaseLocationHook = LocationHook> = NavigationalProps<H> & {
children?: never;
};
export type RedirectProps<H extends BaseLocationHook = LocationHook> =
NavigationalProps<H> & {
children?: never;
};

export function Redirect<H extends BaseLocationHook = LocationHook>(
props: PropsWithChildren<RedirectProps<H>>,
Expand Down Expand Up @@ -138,8 +152,12 @@ export function useRouter(): RouterObject;
export function useRoute<
T extends DefaultParams | undefined = undefined,
RoutePath extends Path = Path
>(pattern: RoutePath): Match<T extends DefaultParams ? T : ExtractRouteParams<RoutePath>>;
>(
pattern: RoutePath
): Match<T extends DefaultParams ? T : ExtractRouteParams<RoutePath>>;

export function useLocation<H extends BaseLocationHook = LocationHook>(): HookReturnValue<H>;
export function useLocation<
H extends BaseLocationHook = LocationHook
>(): HookReturnValue<H>;

// tslint:enable:no-unnecessary-generics

0 comments on commit c79668b

Please sign in to comment.