Skip to content

Commit

Permalink
chore(eslint): set array-type lint rule to generic (#5899)
Browse files Browse the repository at this point in the history
* chore(eslint): set array-type lint rule to generic

* chore: fix prettier
  • Loading branch information
TkDodo authored Aug 22, 2023
1 parent 770d454 commit 13b17ee
Show file tree
Hide file tree
Showing 67 changed files with 642 additions and 590 deletions.
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const config = {
},
},
rules: {
'@typescript-eslint/array-type': ['error', { default: 'generic', readonly: 'generic' }],
'@typescript-eslint/ban-types': 'off',
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/consistent-type-imports': [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import * as React from 'react'
import { useQueries, useQuery, useQueryClient } from 'react-query'

type Todos = {
items: readonly {
items: ReadonlyArray<{
id: string
text: string
}[]
}>
ts: number
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import * as React from 'react'
import { useQueries, useQuery, useQueryClient } from 'react-query'

type Todos = {
items: readonly {
items: ReadonlyArray<{
id: string
text: string
}[]
}>
ts: number
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { TSESLint } from '@typescript-eslint/utils'

export const ExhaustiveDepsUtils = {
isRelevantReference(params: {
context: Readonly<TSESLint.RuleContext<string, readonly unknown[]>>
context: Readonly<TSESLint.RuleContext<string, ReadonlyArray<unknown>>>
reference: TSESLint.Scope.Reference
scopeManager: TSESLint.Scope.ScopeManager
}) {
Expand Down
24 changes: 13 additions & 11 deletions packages/eslint-plugin-query/src/utils/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { RuleContext } from '@typescript-eslint/utils/dist/ts-eslint'
export const ASTUtils = {
isNodeOfOneOf<T extends AST_NODE_TYPES>(
node: TSESTree.Node,
types: readonly T[],
types: ReadonlyArray<T>,
): node is TSESTree.Node & { type: T } {
return types.includes(node.type as T)
},
Expand All @@ -20,7 +20,7 @@ export const ASTUtils = {
): node is TSESTree.Identifier {
return ASTUtils.isIdentifier(node) && node.name === name
},
isIdentifierWithOneOfNames<T extends string[]>(
isIdentifierWithOneOfNames<T extends Array<string>>(
node: TSESTree.Node,
name: T,
): node is TSESTree.Identifier & { name: T[number] } {
Expand All @@ -41,15 +41,15 @@ export const ASTUtils = {
)
},
findPropertyWithIdentifierKey(
properties: TSESTree.ObjectLiteralElement[],
properties: Array<TSESTree.ObjectLiteralElement>,
key: string,
): TSESTree.Property | undefined {
return properties.find((x) =>
ASTUtils.isPropertyWithIdentifierKey(x, key),
) as TSESTree.Property | undefined
},
getNestedIdentifiers(node: TSESTree.Node): TSESTree.Identifier[] {
const identifiers: TSESTree.Identifier[] = []
getNestedIdentifiers(node: TSESTree.Node): Array<TSESTree.Identifier> {
const identifiers: Array<TSESTree.Identifier> = []

if (ASTUtils.isIdentifier(node)) {
identifiers.push(node)
Expand Down Expand Up @@ -131,7 +131,7 @@ export const ASTUtils = {
},
traverseUpOnly(
identifier: TSESTree.Node,
allowedNodeTypes: AST_NODE_TYPES[],
allowedNodeTypes: Array<AST_NODE_TYPES>,
): TSESTree.Node {
const parent = identifier.parent

Expand Down Expand Up @@ -159,7 +159,7 @@ export const ASTUtils = {
scopeManager: TSESLint.Scope.ScopeManager
sourceCode: Readonly<TSESLint.SourceCode>
node: TSESTree.Node
}): TSESLint.Scope.Reference[] {
}): Array<TSESLint.Scope.Reference> {
const { scopeManager, sourceCode, node } = params
const scope = scopeManager.acquire(node)

Expand Down Expand Up @@ -207,7 +207,7 @@ export const ASTUtils = {
return identifier !== null && /^(use|[A-Z])/.test(identifier.name)
},
getFunctionAncestor(
context: Readonly<RuleContext<string, readonly unknown[]>>,
context: Readonly<RuleContext<string, ReadonlyArray<unknown>>>,
) {
return context.getAncestors().find((x) => {
if (x.type === AST_NODE_TYPES.FunctionDeclaration) {
Expand All @@ -227,7 +227,7 @@ export const ASTUtils = {
},
getReferencedExpressionByIdentifier(params: {
node: TSESTree.Node
context: Readonly<RuleContext<string, readonly unknown[]>>
context: Readonly<RuleContext<string, ReadonlyArray<unknown>>>
}) {
const { node, context } = params

Expand All @@ -242,8 +242,10 @@ export const ASTUtils = {

return resolvedNode.init
},
getNestedReturnStatements(node: TSESTree.Node): TSESTree.ReturnStatement[] {
const returnStatements: TSESTree.ReturnStatement[] = []
getNestedReturnStatements(
node: TSESTree.Node,
): Array<TSESTree.ReturnStatement> {
const returnStatements: Array<TSESTree.ReturnStatement> = []

if (node.type === AST_NODE_TYPES.ReturnStatement) {
returnStatements.push(node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type EnhancedCreate = (

export function detectTanstackQueryImports(create: EnhancedCreate): Create {
return (context, optionsWithDefault) => {
const tanstackQueryImportSpecifiers: TSESTree.ImportClause[] = []
const tanstackQueryImportSpecifiers: Array<TSESTree.ImportClause> = []

const helpers: Helpers = {
isTanstackQueryImport(node) {
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin-query/src/utils/unique-by.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export function uniqueBy<T>(arr: T[], fn: (x: T) => unknown): T[] {
export function uniqueBy<T>(arr: Array<T>, fn: (x: T) => unknown): Array<T> {
return arr.filter((x, i, a) => a.findIndex((y) => fn(x) === fn(y)) === i)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { sleep as delay } from './utils'
describe('asyncThrottle', () => {
test('basic', async () => {
const interval = 10
const execTimeStamps: number[] = []
const execTimeStamps: Array<number> = []
const mockFunc = vi.fn(
async (id: number, complete?: (value?: unknown) => void) => {
await delay(1)
Expand Down Expand Up @@ -33,7 +33,7 @@ describe('asyncThrottle', () => {

test('Bug #3331 case 1: Special timing', async () => {
const interval = 1000
const execTimeStamps: number[] = []
const execTimeStamps: Array<number> = []
const mockFunc = vi.fn(
async (id: number, complete?: (value?: unknown) => void) => {
await delay(30)
Expand Down Expand Up @@ -62,7 +62,7 @@ describe('asyncThrottle', () => {

test('Bug #3331 case 2: "func" execution time is greater than the interval.', async () => {
const interval = 1000
const execTimeStamps: number[] = []
const execTimeStamps: Array<number> = []
const mockFunc = vi.fn(
async (id: number, complete?: (value?: unknown) => void) => {
await delay(interval + 10)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const noop = () => {
/* do nothing */
}

export function asyncThrottle<Args extends readonly unknown[]>(
export function asyncThrottle<Args extends ReadonlyArray<unknown>>(
func: (...args: Args) => Promise<void>,
{ interval = 1000, onError = noop }: AsyncThrottleOptions = {},
) {
Expand Down
4 changes: 2 additions & 2 deletions packages/query-core/src/hydration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ interface DehydratedQuery {
}

export interface DehydratedState {
mutations: DehydratedMutation[]
queries: DehydratedQuery[]
mutations: Array<DehydratedMutation>
queries: Array<DehydratedQuery>
}

// FUNCTIONS
Expand Down
2 changes: 1 addition & 1 deletion packages/query-core/src/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class Mutation<
options!: MutationOptions<TData, TError, TVariables, TContext>
readonly mutationId: number

#observers: MutationObserver<TData, TError, TVariables, TContext>[]
#observers: Array<MutationObserver<TData, TError, TVariables, TContext>>
#defaultOptions?: MutationOptions<TData, TError, TVariables, TContext>
#mutationCache: MutationCache
#retryer?: Retryer<TData>
Expand Down
6 changes: 3 additions & 3 deletions packages/query-core/src/mutationCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ type MutationCacheListener = (event: MutationCacheNotifyEvent) => void
// CLASS

export class MutationCache extends Subscribable<MutationCacheListener> {
#mutations: Mutation<any, any, any, any>[]
#mutations: Array<Mutation<any, any, any, any>>
#mutationId: number
#resuming: Promise<unknown> | undefined

Expand Down Expand Up @@ -127,7 +127,7 @@ export class MutationCache extends Subscribable<MutationCacheListener> {
})
}

getAll(): Mutation[] {
getAll(): Array<Mutation> {
return this.#mutations
}

Expand All @@ -146,7 +146,7 @@ export class MutationCache extends Subscribable<MutationCacheListener> {
)
}

findAll(filters: MutationFilters = {}): Mutation[] {
findAll(filters: MutationFilters = {}): Array<Mutation> {
return this.#mutations.filter((mutation) =>
matchMutation(filters, mutation),
)
Expand Down
6 changes: 3 additions & 3 deletions packages/query-core/src/notifyManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ type NotifyFunction = (callback: () => void) => void

type BatchNotifyFunction = (callback: () => void) => void

type BatchCallsCallback<T extends unknown[]> = (...args: T) => void
type BatchCallsCallback<T extends Array<unknown>> = (...args: T) => void

export function createNotifyManager() {
let queue: NotifyCallback[] = []
let queue: Array<NotifyCallback> = []
let transactions = 0
let notifyFn: NotifyFunction = (callback) => {
callback()
Expand Down Expand Up @@ -47,7 +47,7 @@ export function createNotifyManager() {
/**
* All calls to the wrapped function will be batched.
*/
const batchCalls = <T extends unknown[]>(
const batchCalls = <T extends Array<unknown>>(
callback: BatchCallsCallback<T>,
): BatchCallsCallback<T> => {
return (...args) => {
Expand Down
49 changes: 24 additions & 25 deletions packages/query-core/src/queriesObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,37 @@ import type {
import type { QueryClient } from './queryClient'
import type { NotifyOptions } from './queryObserver'

function difference<T>(array1: T[], array2: T[]): T[] {
function difference<T>(array1: Array<T>, array2: Array<T>): Array<T> {
return array1.filter((x) => !array2.includes(x))
}

function replaceAt<T>(array: T[], index: number, value: T): T[] {
function replaceAt<T>(array: Array<T>, index: number, value: T): Array<T> {
const copy = array.slice(0)
copy[index] = value
return copy
}

type QueriesObserverListener = (result: QueryObserverResult[]) => void
type QueriesObserverListener = (result: Array<QueryObserverResult>) => void

export interface QueriesObserverOptions<
TCombinedResult = QueryObserverResult[],
TCombinedResult = Array<QueryObserverResult>,
> {
combine?: (result: QueryObserverResult[]) => TCombinedResult
combine?: (result: Array<QueryObserverResult>) => TCombinedResult
}

export class QueriesObserver<
TCombinedResult = QueryObserverResult[],
TCombinedResult = Array<QueryObserverResult>,
> extends Subscribable<QueriesObserverListener> {
#client: QueryClient
#result!: QueryObserverResult[]
#queries: QueryObserverOptions[]
#observers: QueryObserver[]
#result!: Array<QueryObserverResult>
#queries: Array<QueryObserverOptions>
#observers: Array<QueryObserver>
#options?: QueriesObserverOptions<TCombinedResult>
#combinedResult!: TCombinedResult

constructor(
client: QueryClient,
queries: QueryObserverOptions[],
queries: Array<QueryObserverOptions>,
options?: QueriesObserverOptions<TCombinedResult>,
) {
super()
Expand All @@ -53,7 +53,7 @@ export class QueriesObserver<
this.setQueries(queries, options)
}

#setResult(value: QueryObserverResult[]) {
#setResult(value: Array<QueryObserverResult>) {
this.#result = value
this.#combinedResult = this.#combineResult(value)
}
Expand Down Expand Up @@ -82,7 +82,7 @@ export class QueriesObserver<
}

setQueries(
queries: QueryObserverOptions[],
queries: Array<QueryObserverOptions>,
options?: QueriesObserverOptions<TCombinedResult>,
notifyOptions?: NotifyOptions,
): void {
Expand Down Expand Up @@ -145,11 +145,11 @@ export class QueriesObserver<
}

getOptimisticResult(
queries: QueryObserverOptions[],
queries: Array<QueryObserverOptions>,
): [
rawResult: QueryObserverResult[],
combineResult: (r?: QueryObserverResult[]) => TCombinedResult,
trackResult: () => QueryObserverResult[],
rawResult: Array<QueryObserverResult>,
combineResult: (r?: Array<QueryObserverResult>) => TCombinedResult,
trackResult: () => Array<QueryObserverResult>,
] {
const matches = this.#findMatchingObservers(queries)
const result = matches.map((match) =>
Expand All @@ -158,7 +158,7 @@ export class QueriesObserver<

return [
result,
(r?: QueryObserverResult[]) => {
(r?: Array<QueryObserverResult>) => {
return this.#combineResult(r ?? result)
},
() => {
Expand All @@ -172,7 +172,7 @@ export class QueriesObserver<
]
}

#combineResult(input: QueryObserverResult[]): TCombinedResult {
#combineResult(input: Array<QueryObserverResult>): TCombinedResult {
const combine = this.#options?.combine
if (combine) {
return replaceEqualDeep(this.#combinedResult, combine(input))
Expand All @@ -181,8 +181,8 @@ export class QueriesObserver<
}

#findMatchingObservers(
queries: QueryObserverOptions[],
): QueryObserverMatch[] {
queries: Array<QueryObserverOptions>,
): Array<QueryObserverMatch> {
const prevObservers = this.#observers
const prevObserversMap = new Map(
prevObservers.map((observer) => [observer.options.queryHash, observer]),
Expand All @@ -192,7 +192,7 @@ export class QueriesObserver<
this.#client.defaultQueryOptions(options),
)

const matchingObservers: QueryObserverMatch[] =
const matchingObservers: Array<QueryObserverMatch> =
defaultedQueryOptions.flatMap((defaultedOptions) => {
const match = prevObserversMap.get(defaultedOptions.queryHash)
if (match != null) {
Expand All @@ -218,14 +218,13 @@ export class QueriesObserver<
)
}

const newOrReusedObservers: QueryObserverMatch[] = unmatchedQueries.map(
(options) => {
const newOrReusedObservers: Array<QueryObserverMatch> =
unmatchedQueries.map((options) => {
return {
defaultedQueryOptions: options,
observer: getObserver(options),
}
},
)
})

const sortMatchesByOrderOfQueries = (
a: QueryObserverMatch,
Expand Down
2 changes: 1 addition & 1 deletion packages/query-core/src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export class Query<
#cache: QueryCache
#promise?: Promise<TData>
#retryer?: Retryer<TData>
#observers: QueryObserver<any, any, any, any, any>[]
#observers: Array<QueryObserver<any, any, any, any, any>>
#defaultOptions?: QueryOptions<TQueryFnData, TError, TData, TQueryKey>
#abortSignalConsumed: boolean

Expand Down
Loading

0 comments on commit 13b17ee

Please sign in to comment.