Skip to content

Commit

Permalink
feat(eslint-plugin-query): Update @typescript-eslint/utils to v6 (Tan…
Browse files Browse the repository at this point in the history
…Stack#6864)

* Try to update ts-eslint v6

* Fix recommended

* Finish migration

* Remove any

* Use `project: true`

* Use alternative function syntax
  • Loading branch information
lachlancollins authored Feb 10, 2024
1 parent 92d2deb commit 4220885
Show file tree
Hide file tree
Showing 44 changed files with 252 additions and 143 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const config = {
},
parserOptions: {
tsconfigRootDir: __dirname,
project: './tsconfig.json',
project: true,
sourceType: 'module',
ecmaVersion: 2020,
},
Expand Down
11 changes: 11 additions & 0 deletions examples/angular/basic/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @ts-check

/** @type {import('eslint').Linter.Config} */
const config = {
parserOptions: {
tsconfigRootDir: __dirname,
project: true,
},
}

module.exports = config
2 changes: 1 addition & 1 deletion examples/angular/default-query-function/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
const config = {
parserOptions: {
tsconfigRootDir: __dirname,
project: './tsconfig.json',
project: true,
},
}

Expand Down
2 changes: 1 addition & 1 deletion examples/angular/simple/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
const config = {
parserOptions: {
tsconfigRootDir: __dirname,
project: './tsconfig.json',
project: true,
},
}

Expand Down
2 changes: 1 addition & 1 deletion integrations/react-cra4/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
],
parserOptions: {
tsconfigRootDir: __dirname,
project: './tsconfig.json',
project: true,
},
settings: {
react: {
Expand Down
2 changes: 1 addition & 1 deletion integrations/react-cra5/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
],
parserOptions: {
tsconfigRootDir: __dirname,
project: './tsconfig.json',
project: true,
},
settings: {
react: {
Expand Down
2 changes: 1 addition & 1 deletion integrations/react-next/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
],
parserOptions: {
tsconfigRootDir: __dirname,
project: './tsconfig.json',
project: true,
},
settings: {
react: {
Expand Down
2 changes: 1 addition & 1 deletion integrations/react-vite/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
],
parserOptions: {
tsconfigRootDir: __dirname,
project: './tsconfig.json',
project: true,
},
settings: {
react: {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
"@types/node": "^18.19.3",
"@types/react": "^18.2.45",
"@types/react-dom": "^18.2.18",
"@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^5.62.0",
"@typescript-eslint/eslint-plugin": "^6.20.0",
"@typescript-eslint/parser": "^6.20.0",
"@vitest/coverage-istanbul": "^1.1.0",
"cpy-cli": "^5.0.0",
"esbuild-plugin-file-path-extensions": "^2.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/angular-query-devtools-experimental/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
const config = {
parserOptions: {
tsconfigRootDir: __dirname,
project: './tsconfig.json',
project: true,
},
}

Expand Down
2 changes: 1 addition & 1 deletion packages/angular-query-experimental/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
const config = {
parserOptions: {
tsconfigRootDir: __dirname,
project: './tsconfig.json',
project: true,
},
}

Expand Down
2 changes: 1 addition & 1 deletion packages/codemods/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
const config = {
parserOptions: {
tsconfigRootDir: __dirname,
project: './tsconfig.json',
project: true,
},
overrides: [
{
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin-query/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
const config = {
parserOptions: {
tsconfigRootDir: __dirname,
project: './tsconfig.json',
project: true,
},
}

Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-plugin-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@
"src"
],
"dependencies": {
"@typescript-eslint/utils": "^5.62.0"
"@typescript-eslint/utils": "^6.20.0"
},
"devDependencies": {
"@typescript-eslint/rule-tester": "^6.20.0",
"eslint": "^8.56.0"
},
"peerDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ESLintUtils } from '@typescript-eslint/utils'
import { RuleTester } from '@typescript-eslint/rule-tester'
import { normalizeIndent } from '../utils/test-utils'
import { rule } from '../rules/exhaustive-deps.rule'

const ruleTester = new ESLintUtils.RuleTester({
const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
settings: {},
})
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin-query/src/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ function generateRecommendedConfig(
allRules: Record<string, TSESLint.RuleModule<any, any>>,
) {
return Object.entries(allRules).reduce(
// @ts-expect-error
(memo, [name, rule]) => {
const { recommended } = rule.meta.docs || {}

Expand Down
6 changes: 5 additions & 1 deletion packages/eslint-plugin-query/src/rules.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import * as exhaustiveDeps from './rules/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 type { ESLintUtils } from '@typescript-eslint/utils'

export const rules = {
export const rules: Record<
string,
ESLintUtils.RuleModule<string, any, ESLintUtils.RuleListener>
> = {
[exhaustiveDeps.name]: exhaustiveDeps.rule,
[stableQueryClient.name]: stableQueryClient.rule,
[noRestDestructuring.name]: noRestDestructuring.rule,
Expand Down
15 changes: 9 additions & 6 deletions packages/eslint-plugin-query/src/rules/exhaustive-deps.rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@ import { ASTUtils } from '../utils/ast-utils'
import { createRule } from '../utils/create-rule'
import { uniqueBy } from '../utils/unique-by'
import { ExhaustiveDepsUtils } from './exhaustive-deps.utils'
import type { TSESLint } from '@typescript-eslint/utils'
import type { ESLintUtils, TSESLint } from '@typescript-eslint/utils'

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

export const name = 'exhaustive-deps'

export const rule = createRule({
export const rule: ESLintUtils.RuleModule<
string,
any,
ESLintUtils.RuleListener
> = createRule({
name,
meta: {
type: 'problem',
docs: {
description: 'Exhaustive deps rule for useQuery',
recommended: 'error',
recommended: 'error' as any,
},
messages: {
missingDeps: `The following dependencies are missing in your queryKey: {{deps}}`,
Expand All @@ -28,11 +32,10 @@ export const rule = createRule({
},
defaultOptions: [],

create(context) {
create: (context) => {
return {
Property(node) {
Property: (node) => {
if (
node.parent === undefined ||
!ASTUtils.isObjectExpression(node.parent) ||
!ASTUtils.isIdentifierWithName(node.key, QUERY_KEY)
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const ExhaustiveDepsUtils = {

return (
reference.identifier.name !== 'undefined' &&
reference.identifier.parent?.type !== AST_NODE_TYPES.NewExpression &&
reference.identifier.parent.type !== AST_NODE_TYPES.NewExpression &&
!ExhaustiveDepsUtils.isQueryClientReference(reference)
)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@ import { AST_NODE_TYPES } from '@typescript-eslint/utils'
import { createRule } from '../../utils/create-rule'
import { ASTUtils } from '../../utils/ast-utils'
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 = createRule({
export const rule: ESLintUtils.RuleModule<
string,
any,
ESLintUtils.RuleListener
> = createRule({
name,
meta: {
type: 'problem',
docs: {
description: 'Disallows rest destructuring in queries',
recommended: 'warn',
recommended: 'warn' as any,
},
messages: {
objectRestDestructure: `Object rest destructuring on a query will observe all changes to the query, leading to excessive re-renders.`,
Expand All @@ -22,13 +27,13 @@ export const rule = createRule({
},
defaultOptions: [],

create(context, _, helpers) {
create: (context, _, helpers) => {
return {
CallExpression(node) {
CallExpression: (node) => {
if (
!ASTUtils.isIdentifierWithOneOfNames(node.callee, queryHooks) ||
!helpers.isTanstackQueryImport(node.callee) ||
node.parent?.type !== AST_NODE_TYPES.VariableDeclarator
node.parent.type !== AST_NODE_TYPES.VariableDeclarator
) {
return
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ESLintUtils } from '@typescript-eslint/utils'
import { RuleTester } from '@typescript-eslint/rule-tester'
import { normalizeIndent } from '../../utils/test-utils'
import { rule } from './no-rest-destructuring.rule'

const ruleTester = new ESLintUtils.RuleTester({
const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
settings: {},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@ import { AST_NODE_TYPES } from '@typescript-eslint/utils'
import { ASTUtils } from '../../utils/ast-utils'
import { createRule } from '../../utils/create-rule'
import type { TSESLint } from '@typescript-eslint/utils'
import type { ESLintUtils } from '@typescript-eslint/utils'

export const name = 'stable-query-client'

export const rule = createRule({
export const rule: ESLintUtils.RuleModule<
string,
any,
ESLintUtils.RuleListener
> = createRule({
name,
meta: {
type: 'problem',
docs: {
description: 'Makes sure that QueryClient is stable',
recommended: 'error',
recommended: 'error' as any,
},
messages: {
unstable: [
Expand All @@ -26,13 +31,13 @@ export const rule = createRule({
},
defaultOptions: [],

create(context, _, helpers) {
create: (context, _, helpers) => {
return {
NewExpression(node) {
NewExpression: (node) => {
if (
node.callee.type !== AST_NODE_TYPES.Identifier ||
node.callee.name !== 'QueryClient' ||
node.parent?.type !== AST_NODE_TYPES.VariableDeclarator ||
node.parent.type !== AST_NODE_TYPES.VariableDeclarator ||
!helpers.isSpecificTanstackQueryImport(
node.callee,
'@tanstack/react-query',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ESLintUtils } from '@typescript-eslint/utils'
import { RuleTester } from '@typescript-eslint/rule-tester'
import { normalizeIndent } from '../../utils/test-utils'
import { rule } from './stable-query-client.rule'

const ruleTester = new ESLintUtils.RuleTester({
const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
settings: {},
})
Expand Down
10 changes: 3 additions & 7 deletions packages/eslint-plugin-query/src/utils/ast-utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { AST_NODE_TYPES } from '@typescript-eslint/utils'
import { uniqueBy } from './unique-by'
import type { TSESLint, TSESTree } from '@typescript-eslint/utils'
import type { RuleContext } from '@typescript-eslint/utils/dist/ts-eslint'

export const ASTUtils = {
isNodeOfOneOf<T extends AST_NODE_TYPES>(
Expand Down Expand Up @@ -212,7 +211,7 @@ export const ASTUtils = {
)
},
getFunctionAncestor(
context: Readonly<RuleContext<string, ReadonlyArray<unknown>>>,
context: Readonly<TSESLint.RuleContext<string, ReadonlyArray<unknown>>>,
) {
for (const ancestor of context.getAncestors()) {
if (ancestor.type === AST_NODE_TYPES.FunctionDeclaration) {
Expand All @@ -236,7 +235,7 @@ export const ASTUtils = {
},
getReferencedExpressionByIdentifier(params: {
node: TSESTree.Node
context: Readonly<RuleContext<string, ReadonlyArray<unknown>>>
context: Readonly<TSESLint.RuleContext<string, ReadonlyArray<unknown>>>
}) {
const { node, context } = params

Expand All @@ -254,10 +253,7 @@ export const ASTUtils = {
getClosestVariableDeclarator(node: TSESTree.Node) {
let currentNode: TSESTree.Node | undefined = node

while (
currentNode !== undefined &&
currentNode.type !== AST_NODE_TYPES.Program
) {
while (currentNode.type !== AST_NODE_TYPES.Program) {
if (currentNode.type === AST_NODE_TYPES.VariableDeclarator) {
return currentNode
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function detectTanstackQueryImports(create: EnhancedCreate): Create {
return !!tanstackQueryImportSpecifiers.find((specifier) => {
if (
specifier.type === TSESTree.AST_NODE_TYPES.ImportSpecifier &&
specifier.parent?.type ===
specifier.parent.type ===
TSESTree.AST_NODE_TYPES.ImportDeclaration &&
specifier.parent.source.value === source
) {
Expand Down
2 changes: 1 addition & 1 deletion packages/query-async-storage-persister/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
const config = {
parserOptions: {
tsconfigRootDir: __dirname,
project: './tsconfig.json',
project: true,
},
}

Expand Down
2 changes: 1 addition & 1 deletion packages/query-broadcast-client-experimental/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
const config = {
parserOptions: {
tsconfigRootDir: __dirname,
project: './tsconfig.json',
project: true,
},
}

Expand Down
2 changes: 1 addition & 1 deletion packages/query-core/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
const config = {
parserOptions: {
tsconfigRootDir: __dirname,
project: './tsconfig.json',
project: true,
},
}

Expand Down
Loading

0 comments on commit 4220885

Please sign in to comment.