|
| 1 | +# @openapi-qraft/eslint-plugin-query |
| 2 | + |
| 3 | +ESLint plugin for projects using `@openapi-qraft/react`. It helps enforce best practices around Qraft-generated hooks (e.g. `qraft.service.operation.useQuery()`, `qraft.useInfiniteQuery()`) and prevents mistakes that cause unnecessary re-renders or unstable dependencies. |
| 4 | + |
| 5 | +- Homepage: `https://openapi-qraft.github.io/openapi-qraft/docs/eslint/eslint-plugin-query` |
| 6 | +- Repository: `https://github.com/OpenAPI-Qraft/openapi-qraft` |
| 7 | + |
| 8 | +## Requirements |
| 9 | + |
| 10 | +- Node.js >= 18 |
| 11 | +- ESLint ^8.57.0 or ^9.0.0 |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +```bash |
| 16 | +npm i -D @openapi-qraft/eslint-plugin-query |
| 17 | +# or |
| 18 | +pnpm add -D @openapi-qraft/eslint-plugin-query |
| 19 | +# or |
| 20 | +yarn add -D @openapi-qraft/eslint-plugin-query |
| 21 | +# or |
| 22 | +bun add -D @openapi-qraft/eslint-plugin-query |
| 23 | +``` |
| 24 | + |
| 25 | +## Usage (ESLint Flat Config) |
| 26 | + |
| 27 | +Create or update `eslint.config.js`: |
| 28 | + |
| 29 | +```js |
| 30 | +import pluginQraftQuery from '@openapi-qraft/eslint-plugin-query' |
| 31 | + |
| 32 | +export default [ |
| 33 | + // Enable the recommended rules for this plugin |
| 34 | + ...pluginQraftQuery.configs['flat/recommended'], |
| 35 | + // Any other config... |
| 36 | +] |
| 37 | +``` |
| 38 | + |
| 39 | +### Custom setup (Flat Config) |
| 40 | + |
| 41 | +```js |
| 42 | +import pluginQraftQuery from '@openapi-qraft/eslint-plugin-query' |
| 43 | + |
| 44 | +export default [ |
| 45 | + { |
| 46 | + plugins: { |
| 47 | + '@openapi-qraft/query': pluginQraftQuery, |
| 48 | + }, |
| 49 | + rules: { |
| 50 | + '@openapi-qraft/query/no-rest-destructuring': 'warn', |
| 51 | + '@openapi-qraft/query/no-unstable-deps': 'error', |
| 52 | + }, |
| 53 | + }, |
| 54 | +] |
| 55 | +``` |
| 56 | + |
| 57 | +## Usage (Legacy config) |
| 58 | + |
| 59 | +Add to your `.eslintrc`: |
| 60 | + |
| 61 | +```json |
| 62 | +{ |
| 63 | + "extends": [ |
| 64 | + "plugin:@openapi-qraft/query/recommended" |
| 65 | + ] |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +### Custom setup (Legacy) |
| 70 | + |
| 71 | +```json |
| 72 | +{ |
| 73 | + "plugins": ["@openapi-qraft/query"], |
| 74 | + "rules": { |
| 75 | + "@openapi-qraft/query/no-rest-destructuring": "warn", |
| 76 | + "@openapi-qraft/query/no-unstable-deps": "error" |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +## Options |
| 82 | + |
| 83 | +Some rules detect Qraft-generated hooks only when they are called as methods on your client instance (e.g. `qraft.service.operation.useQuery()`). |
| 84 | +To support custom client names, each rule accepts an optional `clientNamePattern` (string or regex literal) option. By default, it matches the case-insensitive regex `/qraft|api/i`. |
| 85 | + |
| 86 | +Example (Flat Config): |
| 87 | + |
| 88 | +```js |
| 89 | +import pluginQraftQuery from '@openapi-qraft/eslint-plugin-query' |
| 90 | + |
| 91 | +export default [ |
| 92 | + { |
| 93 | + plugins: { '@openapi-qraft/query': pluginQraftQuery }, |
| 94 | + rules: { |
| 95 | + '@openapi-qraft/query/no-rest-destructuring': [ |
| 96 | + 'warn', |
| 97 | + { clientNamePattern: '/myQraftClient/i' }, |
| 98 | + ], |
| 99 | + '@openapi-qraft/query/no-unstable-deps': [ |
| 100 | + 'error', |
| 101 | + { clientNamePattern: '/myQraftClient/i' }, |
| 102 | + ], |
| 103 | + }, |
| 104 | + }, |
| 105 | +] |
| 106 | +``` |
| 107 | + |
| 108 | +## Configurations |
| 109 | + |
| 110 | +This plugin ships with the following shareable configurations: |
| 111 | + |
| 112 | +- `plugin:@openapi-qraft/query/recommended` (Legacy) |
| 113 | +- `plugin.configs['flat/recommended']` (Flat) |
| 114 | + |
| 115 | +They enable the rules as follows: |
| 116 | + |
| 117 | +- `@openapi-qraft/query/no-rest-destructuring`: warn |
| 118 | +- `@openapi-qraft/query/no-unstable-deps`: error |
| 119 | + |
| 120 | +## Rules |
| 121 | + |
| 122 | +- `@openapi-qraft/query/no-rest-destructuring` — Disallow object rest destructuring on query results. Recommended: warn. Docs: `https://openapi-qraft.github.io/openapi-qraft/docs/eslint/no-rest-destructuring` |
| 123 | +- `@openapi-qraft/query/no-unstable-deps` — Disallow putting the result of query hooks directly in a React hook dependency array. Recommended: error. Docs: `https://openapi-qraft.github.io/openapi-qraft/docs/eslint/no-unstable-deps` |
| 124 | + |
| 125 | +## TypeScript |
| 126 | + |
| 127 | +This plugin is implemented with `@typescript-eslint/utils` and works in both JavaScript and TypeScript projects. No special TypeScript configuration is required beyond your project's usual setup. |
| 128 | + |
| 129 | +## License |
| 130 | + |
| 131 | +MIT © OpenAPI Qraft |
0 commit comments