Skip to content

Commit

Permalink
ci(eslint): add rule to encourage generic naming convention as `^(T|T…
Browse files Browse the repository at this point in the history
…[A-Z][A-Za-z]+)$` (#6684)

* ci(eslint): add rule to force generic naming convention

* chore: reflect review

Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>

* fix(eslint): accept A-Z alone too

* chore: update rule regex

* chore: update rule's regex

* fix(eslint): update typeParameter rule as `^(T|T[A-Z][A-Za-z]+)$` more strictly

* chore: resolve prettier error

---------

Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
  • Loading branch information
manudeli and TkDodo authored Jan 28, 2024
1 parent b32ad24 commit c774772
Show file tree
Hide file tree
Showing 24 changed files with 164 additions and 133 deletions.
13 changes: 13 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ const config = {
{ prefer: 'type-imports' },
],
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/naming-convention': [
'error',
{
selector: 'typeParameter',
format: ['PascalCase'],
leadingUnderscore: 'forbid',
trailingUnderscore: 'forbid',
custom: {
regex: '^(T|T[A-Z][A-Za-z]+)$',
match: true,
},
},
],
'@typescript-eslint/no-empty-interface': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
Expand Down
24 changes: 12 additions & 12 deletions packages/angular-query-experimental/src/inject-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,19 @@ type GetResults<T> =
*/
export type QueriesOptions<
T extends Array<any>,
Result extends Array<any> = [],
Depth extends ReadonlyArray<number> = [],
> = Depth['length'] extends MAXIMUM_DEPTH
TResult extends Array<any> = [],
TDepth extends ReadonlyArray<number> = [],
> = TDepth['length'] extends MAXIMUM_DEPTH
? Array<QueryObserverOptionsForCreateQueries>
: T extends []
? []
: T extends [infer Head]
? [...Result, GetOptions<Head>]
? [...TResult, GetOptions<Head>]
: T extends [infer Head, ...infer Tail]
? QueriesOptions<
[...Tail],
[...Result, GetOptions<Head>],
[...Depth, 1]
[...TResult, GetOptions<Head>],
[...TDepth, 1]
>
: Array<unknown> extends T
? T
Expand Down Expand Up @@ -161,19 +161,19 @@ export type QueriesOptions<
*/
export type QueriesResults<
T extends Array<any>,
Result extends Array<any> = [],
Depth extends ReadonlyArray<number> = [],
> = Depth['length'] extends MAXIMUM_DEPTH
TResult extends Array<any> = [],
TDepth extends ReadonlyArray<number> = [],
> = TDepth['length'] extends MAXIMUM_DEPTH
? Array<QueryObserverResult>
: T extends []
? []
: T extends [infer Head]
? [...Result, GetResults<Head>]
? [...TResult, GetResults<Head>]
: T extends [infer Head, ...infer Tail]
? QueriesResults<
[...Tail],
[...Result, GetResults<Head>],
[...Depth, 1]
[...TResult, GetResults<Head>],
[...TDepth, 1]
>
: T extends Array<
QueryObserverOptionsForCreateQueries<
Expand Down
18 changes: 11 additions & 7 deletions packages/angular-query-experimental/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ export interface BaseQueryNarrowing<TData = unknown, TError = DefaultError> {
export type CreateBaseQueryResult<
TData = unknown,
TError = DefaultError,
State = QueryObserverResult<TData, TError>,
TState = QueryObserverResult<TData, TError>,
> = BaseQueryNarrowing<TData, TError> &
MapToSignals<Omit<State, keyof BaseQueryNarrowing>>
MapToSignals<Omit<TState, keyof BaseQueryNarrowing>>

export interface CreateQueryOptions<
TQueryFnData = unknown,
Expand Down Expand Up @@ -112,8 +112,8 @@ export type CreateInfiniteQueryResult<
export type DefinedCreateQueryResult<
TData = unknown,
TError = DefaultError,
DefinedQueryObserver = DefinedQueryObserverResult<TData, TError>,
> = MapToSignals<DefinedQueryObserver>
TDefinedQueryObserver = DefinedQueryObserverResult<TData, TError>,
> = MapToSignals<TDefinedQueryObserver>

export type CreateMutationOptions<
TData = unknown,
Expand Down Expand Up @@ -232,14 +232,18 @@ export type CreateMutationResult<
TError = DefaultError,
TVariables = unknown,
TContext = unknown,
State = CreateStatusBasedMutationResult<
TState = CreateStatusBasedMutationResult<
CreateBaseMutationResult['status'],
TData,
TError,
TVariables,
TContext
>,
> = BaseMutationNarrowing<TData, TError, TVariables, TContext> &
MapToSignals<Omit<State, keyof BaseMutationNarrowing>>
MapToSignals<Omit<TState, keyof BaseMutationNarrowing>>

type Override<A, B> = { [K in keyof A]: K extends keyof B ? B[K] : A[K] }
type Override<TTargetA, TTargetB> = {
[AKey in keyof TTargetA]: AKey extends keyof TTargetB
? TTargetB[AKey]
: TTargetA[AKey]
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ import {
* injectValue(); // string
* ```
*/
export function assertInjector<Runner extends () => any>(
export function assertInjector<TRunner extends () => any>(
fn: Function,
injector: Injector | undefined | null,
runner: Runner,
): ReturnType<Runner>
runner: TRunner,
): ReturnType<TRunner>
/**
* `assertInjector` extends `assertInInjectionContext` with an optional `Injector`
* After assertion, `assertInjector` returns a guaranteed `Injector` whether it is the default `Injector`
Expand Down
8 changes: 4 additions & 4 deletions packages/query-async-storage-persister/src/asyncThrottle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ const noop = () => {
/* do nothing */
}

export function asyncThrottle<Args extends ReadonlyArray<unknown>>(
func: (...args: Args) => Promise<void>,
export function asyncThrottle<TArgs extends ReadonlyArray<unknown>>(
func: (...args: TArgs) => Promise<void>,
{ interval = 1000, onError = noop }: AsyncThrottleOptions = {},
) {
if (typeof func !== 'function') throw new Error('argument is not function.')

let running = false
let lastTime = 0
let timeout: ReturnType<typeof setTimeout>
let currentArgs: Args | null = null
let currentArgs: TArgs | null = null

const execFunc = async () => {
if (currentArgs) {
Expand Down Expand Up @@ -45,7 +45,7 @@ export function asyncThrottle<Args extends ReadonlyArray<unknown>>(
}, interval)
}

return (...args: Args) => {
return (...args: TArgs) => {
currentArgs = args

const tooSoon = Date.now() - lastTime < interval
Expand Down
12 changes: 6 additions & 6 deletions packages/query-core/src/queryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@ export class QueryClient {

getQueryData<
TQueryFnData = unknown,
TaggedQueryKey extends QueryKey = QueryKey,
TInferredQueryFnData = TaggedQueryKey extends DataTag<
TTaggedQueryKey extends QueryKey = QueryKey,
TInferredQueryFnData = TTaggedQueryKey extends DataTag<
unknown,
infer TaggedValue
>
? TaggedValue
: TQueryFnData,
>(queryKey: TaggedQueryKey): TInferredQueryFnData | undefined
>(queryKey: TTaggedQueryKey): TInferredQueryFnData | undefined
getQueryData(queryKey: QueryKey) {
return this.#queryCache.find({ queryKey })?.state.data
}
Expand Down Expand Up @@ -150,15 +150,15 @@ export class QueryClient {

setQueryData<
TQueryFnData = unknown,
TaggedQueryKey extends QueryKey = QueryKey,
TInferredQueryFnData = TaggedQueryKey extends DataTag<
TTaggedQueryKey extends QueryKey = QueryKey,
TInferredQueryFnData = TTaggedQueryKey extends DataTag<
unknown,
infer TaggedValue
>
? TaggedValue
: TQueryFnData,
>(
queryKey: TaggedQueryKey,
queryKey: TTaggedQueryKey,
updater: Updater<
NoInfer<TInferredQueryFnData> | undefined,
NoInfer<TInferredQueryFnData> | undefined
Expand Down
6 changes: 3 additions & 3 deletions packages/query-core/src/tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ export function setIsServer(isServer: boolean) {

export const doNotExecute = (_func: () => void) => true

export type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <
T,
>() => T extends Y ? 1 : 2
export type Equal<TTargetA, TTargetB> = (<T>() => T extends TTargetA
? 1
: 2) extends <T>() => T extends TTargetB ? 1 : 2
? true
: false

Expand Down
8 changes: 5 additions & 3 deletions packages/query-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export type DefaultError = Register extends {
export type QueryKey = ReadonlyArray<unknown>

export declare const dataTagSymbol: unique symbol
export type DataTag<Type, Value> = Type & {
[dataTagSymbol]: Value
export type DataTag<TType, TValue> = TType & {
[dataTagSymbol]: TValue
}

export type QueryFunction<
Expand Down Expand Up @@ -335,7 +335,9 @@ export interface QueryObserverOptions<
_optimisticResults?: 'optimistic' | 'isRestoring'
}

export type WithRequired<T, K extends keyof T> = T & { [_ in K]: {} }
export type WithRequired<TTarget, TKey extends keyof TTarget> = TTarget & {
[_ in TKey]: {}
}

export type DefaultedQueryObserverOptions<
TQueryFnData = unknown,
Expand Down
6 changes: 3 additions & 3 deletions packages/react-query/src/__tests__/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ export function setActTimeout(fn: () => void, ms?: number) {
}, ms)
}

export type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <
T,
>() => T extends Y ? 1 : 2
export type Equal<TTargetA, TTargetB> = (<T>() => T extends TTargetA
? 1
: 2) extends <T>() => T extends TTargetB ? 1 : 2
? true
: false

Expand Down
6 changes: 5 additions & 1 deletion packages/react-query/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,8 @@ export type UseMutationResult<
TContext = unknown,
> = UseBaseMutationResult<TData, TError, TVariables, TContext>

type Override<A, B> = { [K in keyof A]: K extends keyof B ? B[K] : A[K] }
type Override<TTargetA, TTargetB> = {
[AKey in keyof TTargetA]: AKey extends keyof TTargetB
? TTargetB[AKey]
: TTargetA[AKey]
}
24 changes: 12 additions & 12 deletions packages/react-query/src/useQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,19 +159,19 @@ type GetResults<T> =
*/
export type QueriesOptions<
T extends Array<any>,
Result extends Array<any> = [],
Depth extends ReadonlyArray<number> = [],
> = Depth['length'] extends MAXIMUM_DEPTH
TResult extends Array<any> = [],
TDepth extends ReadonlyArray<number> = [],
> = TDepth['length'] extends MAXIMUM_DEPTH
? Array<UseQueryOptionsForUseQueries>
: T extends []
? []
: T extends [infer Head]
? [...Result, GetOptions<Head>]
? [...TResult, GetOptions<Head>]
: T extends [infer Head, ...infer Tail]
? QueriesOptions<
[...Tail],
[...Result, GetOptions<Head>],
[...Depth, 1]
[...TResult, GetOptions<Head>],
[...TDepth, 1]
>
: Array<unknown> extends T
? T
Expand Down Expand Up @@ -201,19 +201,19 @@ export type QueriesOptions<
*/
export type QueriesResults<
T extends Array<any>,
Result extends Array<any> = [],
Depth extends ReadonlyArray<number> = [],
> = Depth['length'] extends MAXIMUM_DEPTH
TResult extends Array<any> = [],
TDepth extends ReadonlyArray<number> = [],
> = TDepth['length'] extends MAXIMUM_DEPTH
? Array<UseQueryResult>
: T extends []
? []
: T extends [infer Head]
? [...Result, GetResults<Head>]
? [...TResult, GetResults<Head>]
: T extends [infer Head, ...infer Tail]
? QueriesResults<
[...Tail],
[...Result, GetResults<Head>],
[...Depth, 1]
[...TResult, GetResults<Head>],
[...TDepth, 1]
>
: T extends Array<
UseQueryOptionsForUseQueries<
Expand Down
24 changes: 12 additions & 12 deletions packages/react-query/src/useSuspenseQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,19 @@ type GetSuspenseResults<T> =
*/
export type SuspenseQueriesOptions<
T extends Array<any>,
Result extends Array<any> = [],
Depth extends ReadonlyArray<number> = [],
> = Depth['length'] extends MAXIMUM_DEPTH
TResult extends Array<any> = [],
TDepth extends ReadonlyArray<number> = [],
> = TDepth['length'] extends MAXIMUM_DEPTH
? Array<UseSuspenseQueryOptions>
: T extends []
? []
: T extends [infer Head]
? [...Result, GetSuspenseOptions<Head>]
? [...TResult, GetSuspenseOptions<Head>]
: T extends [infer Head, ...infer Tail]
? SuspenseQueriesOptions<
[...Tail],
[...Result, GetSuspenseOptions<Head>],
[...Depth, 1]
[...TResult, GetSuspenseOptions<Head>],
[...TDepth, 1]
>
: Array<unknown> extends T
? T
Expand All @@ -137,19 +137,19 @@ export type SuspenseQueriesOptions<
*/
export type SuspenseQueriesResults<
T extends Array<any>,
Result extends Array<any> = [],
Depth extends ReadonlyArray<number> = [],
> = Depth['length'] extends MAXIMUM_DEPTH
TResult extends Array<any> = [],
TDepth extends ReadonlyArray<number> = [],
> = TDepth['length'] extends MAXIMUM_DEPTH
? Array<UseSuspenseQueryResult>
: T extends []
? []
: T extends [infer Head]
? [...Result, GetSuspenseResults<Head>]
? [...TResult, GetSuspenseResults<Head>]
: T extends [infer Head, ...infer Tail]
? SuspenseQueriesResults<
[...Tail],
[...Result, GetSuspenseResults<Head>],
[...Depth, 1]
[...TResult, GetSuspenseResults<Head>],
[...TDepth, 1]
>
: T extends Array<
UseSuspenseQueryOptions<
Expand Down
6 changes: 3 additions & 3 deletions packages/solid-query/src/__tests__/createQuery.types.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { describe, it } from 'vitest'
import { createQuery, queryOptions } from '../index'

export type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <
T,
>() => T extends Y ? 1 : 2
export type Equal<TTargetA, TTargetB> = (<T>() => T extends TTargetA
? 1
: 2) extends <T>() => T extends TTargetB ? 1 : 2
? true
: false

Expand Down
10 changes: 5 additions & 5 deletions packages/solid-query/src/createBaseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,21 @@ const hydratableObserverResult = <
TError,
TData,
TQueryKey extends QueryKey,
T2,
TDataHydratable,
>(
query: Query<TQueryFnData, TError, TData, TQueryKey>,
result: QueryObserverResult<T2, TError>,
): HydratableQueryState<T2, TError> => {
result: QueryObserverResult<TDataHydratable, TError>,
): HydratableQueryState<TDataHydratable, TError> => {
// Including the extra properties is only relevant on the server
if (!isServer) return result as HydratableQueryState<T2, TError>
if (!isServer) return result as HydratableQueryState<TDataHydratable, TError>

return {
...unwrap(result),

// cast to refetch function should be safe, since we only remove it on the server,
// and refetch is not relevant on the server
refetch: undefined as unknown as HydratableQueryState<
T2,
TDataHydratable,
TError
>['refetch'],

Expand Down
Loading

0 comments on commit c774772

Please sign in to comment.