Skip to content

Commit 993e838

Browse files
feat(eslint-plugin): no-void-queryFn (#8925)
* feat(eslint-plugin-query): implement no-void-query-fn rule * add docs * ci: apply automated fixes * build(eslint-plugin-query): switch from vite to tsup for building * update lockfile * revert lockfile * fix * fix publint * chore: add script to patch missing export equals * ci: apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 644e945 commit 993e838

File tree

13 files changed

+525
-13
lines changed

13 files changed

+525
-13
lines changed

docs/eslint/eslint-plugin-query.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,4 @@ Alternatively, add `@tanstack/query` to the plugins section, and configure the r
9898
- [@tanstack/query/stable-query-client](./stable-query-client.md)
9999
- [@tanstack/query/no-unstable-deps](./no-unstable-deps.md)
100100
- [@tanstack/query/infinite-query-property-order](./infinite-query-property-order.md)
101+
- [@tanstack/query/no-void-query-fn](./no-void-query-fn.md)

docs/eslint/no-void-query-fn.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
id: no-void-query-fn
3+
title: Disallow returning void from query functions
4+
---
5+
6+
Query functions must return a value that will be cached by TanStack Query. Functions that don't return a value (void functions) can lead to unexpected behavior and might indicate a mistake in the implementation.
7+
8+
## Rule Details
9+
10+
Example of **incorrect** code for this rule:
11+
12+
```tsx
13+
/* eslint "@tanstack/query/no-void-query-fn": "error" */
14+
15+
useQuery({
16+
queryKey: ['todos'],
17+
queryFn: async () => {
18+
await api.todos.fetch() // Function doesn't return the fetched data
19+
},
20+
})
21+
```
22+
23+
Example of **correct** code for this rule:
24+
25+
```tsx
26+
/* eslint "@tanstack/query/no-void-query-fn": "error" */
27+
useQuery({
28+
queryKey: ['todos'],
29+
queryFn: async () => {
30+
const todos = await api.todos.fetch()
31+
return todos
32+
},
33+
})
34+
```
35+
36+
## Attributes
37+
38+
- [x] ✅ Recommended
39+
- [ ] 🔧 Fixable

knip.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
{
22
"$schema": "https://unpkg.com/knip@5/schema.json",
3-
"ignore": [".pnpmfile.cjs", "scripts/*.js", "**/root.*.config.*"],
3+
"ignore": [
4+
".pnpmfile.cjs",
5+
"scripts/*.js",
6+
"**/root.*.config.*",
7+
"**/ts-fixture/file.ts"
8+
],
49
"ignoreDependencies": [
510
"@types/react",
611
"@types/react-dom",

packages/eslint-plugin-query/package.json

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"license": "MIT",
77
"repository": {
88
"type": "git",
9-
"url": "https://github.com/TanStack/query.git",
9+
"url": "git+https://github.com/TanStack/query.git",
1010
"directory": "packages/eslint-plugin-query"
1111
},
1212
"homepage": "https://tanstack.com/query",
@@ -31,36 +31,35 @@
3131
"test:lib": "vitest",
3232
"test:lib:dev": "pnpm run test:lib --watch",
3333
"test:build": "publint --strict && attw --pack",
34-
"build": "vite build"
34+
"build": "tsup && node ./scripts/patch-missing-export-equals.js"
3535
},
3636
"type": "module",
37-
"types": "dist/esm/index.d.ts",
38-
"main": "dist/cjs/index.cjs",
39-
"module": "dist/esm/index.js",
37+
"types": "dist/index.d.ts",
38+
"main": "dist/index.cjs",
39+
"module": "dist/index.js",
4040
"exports": {
4141
".": {
4242
"import": {
4343
"@tanstack/custom-condition": "./src/index.ts",
44-
"types": "./dist/esm/index.d.ts",
45-
"default": "./dist/esm/index.js"
44+
"types": "./dist/index.d.ts",
45+
"default": "./dist/index.js"
4646
},
4747
"require": {
48-
"types": "./dist/cjs/index.d.cts",
49-
"default": "./dist/cjs/index.cjs"
48+
"types": "./dist/index.d.cts",
49+
"default": "./dist/index.cjs"
5050
}
5151
},
5252
"./package.json": "./package.json"
5353
},
5454
"sideEffects": false,
5555
"files": [
56-
"dist",
57-
"src",
58-
"!src/__tests__"
56+
"dist"
5957
],
6058
"dependencies": {
6159
"@typescript-eslint/utils": "^8.18.1"
6260
},
6361
"devDependencies": {
62+
"@typescript-eslint/parser": "^8.18.1",
6463
"@typescript-eslint/rule-tester": "^8.18.1",
6564
"combinate": "^1.1.11",
6665
"eslint": "^9.15.0",
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// @ts-check
2+
3+
// https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/MissingExportEquals.md
4+
5+
import fs from 'node:fs/promises'
6+
import path from 'node:path'
7+
8+
const projectDir = new URL('..', import.meta.url).pathname
9+
10+
const dtsFiles = [
11+
path.join(projectDir, 'dist/index.d.ts'),
12+
path.join(projectDir, 'dist/index.d.cts'),
13+
]
14+
15+
for (const file of dtsFiles) {
16+
await fs.appendFile(file, '\n\nexport = plugin')
17+
console.log(
18+
`Appended \`export = plugin\` to ${path.relative(projectDir, file)}`,
19+
)
20+
}

0 commit comments

Comments
 (0)