Skip to content

Commit

Permalink
refactor(eslint-plugin-query): Use ESLintUtils.RuleCreator (#6867)
Browse files Browse the repository at this point in the history
* refactor: Use ESLintUtils.RuleCreator

* Move files, fix folder name

* Remove empty type import

* Fix knip
  • Loading branch information
lachlancollins committed Feb 10, 2024
1 parent e105930 commit 7b7cc2f
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RuleTester } from '@typescript-eslint/rule-tester'
import { normalizeIndent } from '../utils/test-utils'
import { rule } from '../rules/exhaustive-deps.rule'
import { rule } from '../rules/exhaustive-deps/exhaustive-deps.rule'
import { normalizeIndent } from './test-utils'

const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RuleTester } from '@typescript-eslint/rule-tester'
import { normalizeIndent } from '../../utils/test-utils'
import { rule } from './no-rest-destructuring.rule'
import { rule } from '../rules/no-rest-destructuring/no-rest-destructuring.rule'
import { normalizeIndent } from './test-utils'

const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RuleTester } from '@typescript-eslint/rule-tester'
import { normalizeIndent } from '../../utils/test-utils'
import { rule } from './stable-query-client.rule'
import { rule } from '../rules/stable-query-client/stable-query-client.rule'
import { normalizeIndent } from './test-utils'

const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
Expand Down
11 changes: 9 additions & 2 deletions packages/eslint-plugin-query/src/configs.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { rules } from './rules'
import type { TSESLint } from '@typescript-eslint/utils'
import type { ESLintUtils } from '@typescript-eslint/utils'

function generateRecommendedConfig(
allRules: Record<string, TSESLint.RuleModule<any, any>>,
allRules: Record<
string,
ESLintUtils.RuleModule<
string,
ReadonlyArray<unknown>,
ESLintUtils.RuleListener
>
>,
) {
return Object.entries(allRules).reduce(
// @ts-expect-error
Expand Down
10 changes: 7 additions & 3 deletions packages/eslint-plugin-query/src/rules.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import * as exhaustiveDeps from './rules/exhaustive-deps.rule'
import * as exhaustiveDeps from './rules/exhaustive-deps/exhaustive-deps.rule'
import * as stableQueryClient from './rules/stable-query-client/stable-query-client.rule'
import * as noRestDestructuring from './rules/no-rest-desctructuring/no-rest-destructuring.rule'
import * as noRestDestructuring from './rules/no-rest-destructuring/no-rest-destructuring.rule'
import type { ESLintUtils } from '@typescript-eslint/utils'

export const rules: Record<
string,
ESLintUtils.RuleModule<string, any, ESLintUtils.RuleListener>
ESLintUtils.RuleModule<
string,
ReadonlyArray<unknown>,
ESLintUtils.RuleListener
>
> = {
[exhaustiveDeps.name]: exhaustiveDeps.rule,
[stableQueryClient.name]: stableQueryClient.rule,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { AST_NODE_TYPES } from '@typescript-eslint/utils'
import { ASTUtils } from '../utils/ast-utils'
import { createRule } from '../utils/create-rule'
import { uniqueBy } from '../utils/unique-by'
import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils'
import { ASTUtils } from '../../utils/ast-utils'
import { getDocsUrl } from '../../utils/get-docs-url'
import { uniqueBy } from '../../utils/unique-by'
import { detectTanstackQueryImports } from '../../utils/detect-react-query-imports'
import { ExhaustiveDepsUtils } from './exhaustive-deps.utils'
import type { ESLintUtils, TSESLint } from '@typescript-eslint/utils'
import type { TSESLint } from '@typescript-eslint/utils'

const QUERY_KEY = 'queryKey'
const QUERY_FN = 'queryFn'

export const name = 'exhaustive-deps'

export const rule: ESLintUtils.RuleModule<
string,
any,
ESLintUtils.RuleListener
> = createRule({
const createRule = ESLintUtils.RuleCreator(getDocsUrl)

export const rule = createRule({
name,
meta: {
type: 'problem',
Expand All @@ -32,7 +31,7 @@ export const rule: ESLintUtils.RuleModule<
},
defaultOptions: [],

create: (context) => {
create: detectTanstackQueryImports((context) => {
return {
Property: (node) => {
if (
Expand Down Expand Up @@ -153,5 +152,5 @@ export const rule: ESLintUtils.RuleModule<
}
},
}
},
}),
})
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AST_NODE_TYPES } from '@typescript-eslint/utils'
import { ASTUtils } from '../utils/ast-utils'
import { ASTUtils } from '../../utils/ast-utils'
import type { TSESLint } from '@typescript-eslint/utils'

export const ExhaustiveDepsUtils = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { AST_NODE_TYPES } from '@typescript-eslint/utils'
import { createRule } from '../../utils/create-rule'
import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils'
import { getDocsUrl } from '../../utils/get-docs-url'
import { ASTUtils } from '../../utils/ast-utils'
import { detectTanstackQueryImports } from '../../utils/detect-react-query-imports'
import { NoRestDestructuringUtils } from './no-rest-destructuring.utils'
import type { ESLintUtils } from '@typescript-eslint/utils'

export const name = 'no-rest-destructuring'

const queryHooks = ['useQuery', 'useQueries', 'useInfiniteQuery']

export const rule: ESLintUtils.RuleModule<
string,
any,
ESLintUtils.RuleListener
> = createRule({
const createRule = ESLintUtils.RuleCreator(getDocsUrl)

export const rule = createRule({
name,
meta: {
type: 'problem',
Expand All @@ -27,7 +25,7 @@ export const rule: ESLintUtils.RuleModule<
},
defaultOptions: [],

create: (context, _, helpers) => {
create: detectTanstackQueryImports((context, _, helpers) => {
return {
CallExpression: (node) => {
if (
Expand Down Expand Up @@ -65,5 +63,5 @@ export const rule: ESLintUtils.RuleModule<
})
},
}
},
}),
})
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { AST_NODE_TYPES } from '@typescript-eslint/utils'
import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils'
import { ASTUtils } from '../../utils/ast-utils'
import { createRule } from '../../utils/create-rule'
import { getDocsUrl } from '../../utils/get-docs-url'
import { detectTanstackQueryImports } from '../../utils/detect-react-query-imports'
import type { TSESLint } from '@typescript-eslint/utils'
import type { ESLintUtils } from '@typescript-eslint/utils'

export const name = 'stable-query-client'

export const rule: ESLintUtils.RuleModule<
string,
any,
ESLintUtils.RuleListener
> = createRule({
const createRule = ESLintUtils.RuleCreator(getDocsUrl)

export const rule = createRule({
name,
meta: {
type: 'problem',
Expand All @@ -31,7 +29,7 @@ export const rule: ESLintUtils.RuleModule<
},
defaultOptions: [],

create: (context, _, helpers) => {
create: detectTanstackQueryImports((context, _, helpers) => {
return {
NewExpression: (node) => {
if (
Expand Down Expand Up @@ -79,5 +77,5 @@ export const rule: ESLintUtils.RuleModule<
})
},
}
},
}),
})
20 changes: 0 additions & 20 deletions packages/eslint-plugin-query/src/utils/create-rule.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Helpers = {
isTanstackQueryImport: (node: TSESTree.Identifier) => boolean
}

export type EnhancedCreate = (
type EnhancedCreate = (
context: Context,
options: Options,
helpers: Helpers,
Expand Down
2 changes: 2 additions & 0 deletions packages/eslint-plugin-query/src/utils/get-docs-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const getDocsUrl = (ruleName: string): string =>
`https://tanstack.com/query/latest/docs/eslint/${ruleName}`

0 comments on commit 7b7cc2f

Please sign in to comment.