-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
feat(eslint-plugin-query): initial commit #4364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
52 commits
Select commit
Hold shift + click to select a range
a341931
add @tanstack/eslint-plugin-query
Newbie012 b0a6732
fix .eslintrc rule name
Newbie012 758bb61
Merge branch 'main' into feature-eslint-plugin
TkDodo 0b3d91c
make rule compatible with babel parser
Newbie012 b30209f
fix script typo
Newbie012 7a22b6f
remove dedicated example in favor of the basic example
Newbie012 23a447c
align package version with the other packages
Newbie012 8f41031
align eslint-plugin-query version
Newbie012 a6af4a9
fix displayName
Newbie012 f2f2c0a
remove typescript from package
Newbie012 4da87d9
omit call expressions from keys without the need of whitelist
Newbie012 4d571ee
lint queryKey instead of useQuery
Newbie012 3f7e1c7
update pnpm-lock.yaml
Newbie012 b7fba1a
Merge branch 'feature-eslint-plugin' of https://github.com/Newbie012/…
Newbie012 dc8c0eb
fix auto-fix suggestion for complex ast
Newbie012 59214a8
better inline query key
Newbie012 a78f85f
fix typo
Newbie012 776bca6
update scripts
Newbie012 6a71436
restore .eslintrc
Newbie012 02a062c
update typescript-eslint packages
Newbie012 1c38511
support member expression keys
Newbie012 eaee66b
format
Newbie012 e0b4f9f
should not fix with duplicate keys
Newbie012 64a7e7a
Merge branch 'main' into feature-eslint-plugin
Newbie012 df3a802
format
Newbie012 ab94f9a
Merge branch 'feature-eslint-plugin' of https://github.com/Newbie012/…
Newbie012 e8c2632
empty commit (flaky test)
Newbie012 fdaa6a6
should ignore keys from callback
Newbie012 535be2c
Update packages/eslint-plugin-query/package.json
Newbie012 afd006e
Merge branch 'main' into feature-eslint-plugin
TkDodo 3e1f2b6
add prefer-query-object-syntax rule
Newbie012 0170633
Merge branch 'feature-eslint-plugin' of https://github.com/Newbie012/…
Newbie012 432017f
fix indention
Newbie012 96d9192
fix headline
Newbie012 616b156
Merge branch 'main' into feature-eslint-plugin
TkDodo eba0444
Merge branch 'main' into feature-eslint-plugin
Newbie012 bc4c70a
add rule description
TkDodo 5501dbc
Merge branch 'main' into feature-eslint-plugin
TkDodo c83ae52
fix lint errors
TkDodo 260d231
widen import check for all adapters
Newbie012 d8f6dfe
add support for solid-query (createQuery)
Newbie012 405cd8b
Merge branch 'feature-eslint-plugin' of https://github.com/Newbie012/…
Newbie012 253ced7
docs: add eslint plugin section
TkDodo 47192a7
prettier
TkDodo 176d662
move docs
TkDodo 348f461
EsLint -> ESLint
TkDodo 58bb647
fix typo
TkDodo 7f961d2
add missing dep
TkDodo 3551295
update lockfile
Newbie012 6ec2643
give credit where credit is due
TkDodo 350f057
Merge branch 'main' into feature-eslint-plugin
TkDodo 25b969b
Merge branch 'main' into feature-eslint-plugin
TkDodo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| --- | ||
| id: eslint-plugin-query | ||
| title: ESLint Plugin Query | ||
| --- | ||
|
|
||
| TanStack Query comes with its own ESLint plugin. This plugin is used to enforce best practices and to help you avoid common mistakes. | ||
|
|
||
| ## Installation | ||
|
|
||
| The plugin is a separate package that you need to install: | ||
|
|
||
| ```bash | ||
| $ npm i @tanstack/eslint-plugin-query | ||
| # or | ||
| $ pnpm add @tanstack/eslint-plugin-query | ||
| # or | ||
| $ yarn add @tanstack/eslint-plugin-query | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| Add `@tanstack/eslint-plugin-query` to the plugins section of your `.eslintrc` configuration file: | ||
|
|
||
| ```json | ||
| { | ||
| "plugins": ["@tanstack/query"] | ||
| } | ||
| ``` | ||
|
|
||
| Then configure the rules you want to use under the rules section: | ||
|
|
||
| ```json | ||
| { | ||
| "rules": { | ||
| "@tanstack/query/exhaustive-deps": "error", | ||
| "@tanstack/query/prefer-query-object-syntax": "error" | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### Recommended config | ||
|
|
||
| You can also enable all the recommended rules for our plugin. Add `plugin:@tanstack/eslint-plugin-query/recommended` in extends: | ||
|
|
||
| ```json | ||
| { | ||
| "extends": ["plugin:@tanstack/eslint-plugin-query/recommended"] | ||
| } | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| --- | ||
| id: exhaustive-deps | ||
| title: Exhaustive dependencies for query keys | ||
| --- | ||
|
|
||
| Query keys should be seen like a dependency array to your query function: Every variable that is used inside the queryFn should be added to the query key. | ||
| This makes sure that queries are cached independently and that queries are refetched automatically when the variables changes. | ||
|
|
||
| ## Rule Details | ||
|
|
||
| Examples of **incorrect** code for this rule: | ||
|
|
||
| ```ts | ||
| /* eslint "@tanstack/query/exhaustive-deps": "error" */ | ||
|
|
||
| useQuery({ | ||
| queryKey: ["todo"], | ||
| queryFn: () => api.getTodo(todoId) | ||
| }) | ||
|
|
||
| const todoQueries = { | ||
| detail: (id) => ({ queryKey: ["todo"], queryFn: () => api.getTodo(id) }) | ||
| } | ||
| ``` | ||
|
|
||
|
|
||
| Examples of **correct** code for this rule: | ||
|
|
||
| ```ts | ||
| useQuery({ | ||
| queryKey: ["todo", todoId], | ||
| queryFn: () => api.getTodo(todoId) | ||
| }) | ||
|
|
||
| const todoQueries = { | ||
| detail: (id) => ({ queryKey: ["todo", id], queryFn: () => api.getTodo(id) }) | ||
| } | ||
| ``` | ||
|
|
||
| ## When Not To Use It | ||
|
|
||
| If you don't care about the rules of the query keys, then you will not need this rule. | ||
|
|
||
| ## Attributes | ||
|
|
||
| - [x] ✅ Recommended | ||
| - [x] 🔧 Fixable |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| --- | ||
| id: prefer-query-object-syntax | ||
| title: Prefer object syntax for useQuery | ||
| --- | ||
|
|
||
| You can use [`useQuery`](https://tanstack.com/query/v4/docs/reference/useQuery) in two different ways. | ||
|
|
||
| Standard | ||
|
|
||
| ```ts | ||
| useQuery(queryKey, queryFn?, options?) | ||
|
|
||
| // or | ||
|
|
||
| useQuery(options) | ||
| ``` | ||
|
|
||
| This rule prefers the second option, as it is more consistent with other React Query hooks, like `useQueries`. It will also be the only available option in a future major version. | ||
|
|
||
| ## Rule Details | ||
|
|
||
| Examples of **incorrect** code for this rule: | ||
|
|
||
| ```js | ||
| /* eslint "@tanstack/query/prefer-query-object-syntax": "error" */ | ||
|
|
||
| import { useQuery } from '@tanstack/react-query'; | ||
|
|
||
| useQuery(queryKey, queryFn, { | ||
| onSuccess, | ||
| }); | ||
|
|
||
| useQuery(queryKey, { | ||
| queryFn, | ||
| onSuccess, | ||
| }); | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule: | ||
|
|
||
| ```js | ||
| import { useQuery } from '@tanstack/react-query'; | ||
|
|
||
| useQuery({ | ||
| queryKey, | ||
| queryFn, | ||
| onSuccess, | ||
| }); | ||
| ``` | ||
|
|
||
| ## When Not To Use It | ||
|
|
||
| If you don't care about useQuery consistency, then you will not need this rule. | ||
|
|
||
| ## Attributes | ||
|
|
||
| - [x] ✅ Recommended | ||
| - [x] 🔧 Fixable | ||
|
|
||
| ## Credits | ||
|
|
||
| This rule was initially developed by [KubaJastrz](https://github.com/KubaJastrz) in [eslint-plugin-react-query](https://github.com/KubaJastrz/eslint-plugin-react-query). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,3 @@ | ||
| { | ||
| "extends": ["react-app", "prettier"], | ||
| "rules": { | ||
| // "eqeqeq": 0, | ||
| // "jsx-a11y/anchor-is-valid": 0 | ||
| } | ||
| "extends": ["react-app", "prettier", "plugin:@tanstack/eslint-plugin-query/recommended"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,3 @@ | ||
| { | ||
| "extends": ["react-app", "prettier"], | ||
| "rules": { | ||
| // "eqeqeq": 0, | ||
| // "jsx-a11y/anchor-is-valid": 0 | ||
| } | ||
| "extends": ["react-app", "prettier", "plugin:@tanstack/eslint-plugin-query/recommended"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "parser": "@typescript-eslint/parser", | ||
| "parserOptions": { | ||
| "project": "./tsconfig.json", | ||
| "sourceType": "module" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export default { | ||
| displayName: 'eslint-plugin-query', | ||
| preset: '../../jest-preset.js', | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| { | ||
| "name": "@tanstack/eslint-plugin-query", | ||
| "version": "4.13.0", | ||
| "description": "ESLint plugin for TanStack Query", | ||
| "author": "Eliya Cohen", | ||
| "license": "MIT", | ||
| "repository": "tanstack/query", | ||
| "homepage": "https://tanstack.com/query", | ||
| "funding": { | ||
| "type": "github", | ||
| "url": "https://github.com/sponsors/tannerlinsley" | ||
| }, | ||
| "main": "build/lib/index.js", | ||
| "scripts": { | ||
| "typecheck": "tsc", | ||
| "build": "tsup --minify", | ||
| "dev": "tsup --watch --sourcemap", | ||
TkDodo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "clean": "rm -rf ./build", | ||
| "test:jest": "../../node_modules/.bin/jest --config ./jest.config.ts", | ||
| "test:eslint": "../../node_modules/.bin/eslint --ext .ts,.tsx ./src", | ||
| "test:dev": "pnpm run test:jest --watch" | ||
| }, | ||
| "files": [ | ||
| "build" | ||
| ], | ||
| "tsup": { | ||
| "entry": [ | ||
| "src/index.ts" | ||
| ], | ||
| "external": [ | ||
| "eslint" | ||
| ], | ||
| "clean": true, | ||
| "bundle": true, | ||
| "outDir": "build/lib" | ||
| }, | ||
| "devDependencies": { | ||
| "@typescript-eslint/eslint-plugin": "^5.41.0", | ||
| "@typescript-eslint/parser": "^5.41.0", | ||
| "@typescript-eslint/utils": "^5.41.0", | ||
| "eslint": "^8.26.0", | ||
| "tsup": "^6.3.0" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { configs } from './index' | ||
|
|
||
| describe('configs', () => { | ||
| it('should match snapshot', () => { | ||
| expect(configs).toMatchInlineSnapshot(` | ||
| { | ||
| "recommended": { | ||
| "plugins": [ | ||
| "@tanstack/eslint-plugin-query", | ||
| ], | ||
| "rules": { | ||
| "@tanstack/query/exhaustive-deps": "error", | ||
| "@tanstack/query/prefer-query-object-syntax": "error", | ||
| }, | ||
| }, | ||
| } | ||
| `) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import type { TSESLint } from '@typescript-eslint/utils' | ||
| import { rules } from '../rules' | ||
|
|
||
| function generateRecommendedConfig( | ||
| allRules: Record<string, TSESLint.RuleModule<any, any>>, | ||
| ) { | ||
| return Object.entries(allRules).reduce((memo, [name, rule]) => { | ||
| const { recommended } = rule.meta.docs || {} | ||
|
|
||
| return { | ||
| ...memo, | ||
| ...(recommended ? { [`@tanstack/query/${name}`]: recommended } : {}), | ||
| } | ||
| }, {} as Record<string, 'strict' | 'error' | 'warn'>) | ||
| } | ||
|
|
||
| export const configs = { | ||
| recommended: { | ||
| plugins: ['@tanstack/eslint-plugin-query'], | ||
| rules: generateRecommendedConfig(rules), | ||
| }, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export { configs } from './configs' | ||
| export { rules } from './rules' |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.