Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

plugin.ts

51 lines1.5 KB

1import { builtinRules } from 'eslint/use-at-your-own-risk'
2import type { ESLint, Rule } from 'eslint'
3​
4const noRestrictedSyntax = builtinRules.get('no-restricted-syntax')
5​
6if (!noRestrictedSyntax) {
7 throw new Error('ESLint core rule `no-restricted-syntax` was not found')
8}
9​
10const typeParameterPattern = /^(T|T[A-Z][A-Za-z]+)$/
11​
12/**
13 * Replaces `@typescript-eslint/naming-convention`, which requires type
14 * information from the ESLint parser services that Oxlint does not provide.
15 */
16const typeParameterNaming: Rule.RuleModule = {
17 meta: {
18 type: 'suggestion',
19 docs: {
20 description: `Require type parameter names to match ${typeParameterPattern}`,
21 },
22 schema: [],
23 },
24 create: (context) => ({
25 TSTypeParameter(node: {
26 name: string | { name: string }
27 parent: { type: string }
28 }) {
29 // Matches `@typescript-eslint/naming-convention`, which skips `infer` declarations
30 if (node.parent.type === 'TSInferType') return
31 const name = typeof node.name === 'string' ? node.name : node.name.name
32 if (!typeParameterPattern.test(name)) {
33 context.report({
34 // @ts-expect-error TSESTree node types are not part of ESLint's node map
35 node,
36 message: `Type parameter \`${name}\` must match ${typeParameterPattern}.`,
37 })
38 }
39 },
40 }),
42​
43const plugin: ESLint.Plugin = {
44 meta: { name: 'tanstack-query' },
45 rules: {
46 'no-restricted-syntax': noRestrictedSyntax,
47 'type-parameter-naming': typeParameterNaming,
48 },
50​
51export default plugin