Open History to see the last commit
1import { TSESTree } from '@typescript-eslint/utils'
2import type { ESLintUtils, TSESLint } from '@typescript-eslint/utils'
3β
4type Create = Parameters<
5 ReturnType<typeof ESLintUtils.RuleCreator>
6>[0]['create']
7β
8type Context = Parameters<Create>[0]
9type Options = Parameters<Create>[1]
10type Helpers = {
11 isSpecificTanstackQueryImport: (
12 node: TSESTree.Identifier,
13 source: string,
14 ) => boolean
15 isTanstackQueryImport: (node: TSESTree.Identifier) => boolean
16}
17β
18type EnhancedCreate = (
19 context: Context,
20 options: Options,
21 helpers: Helpers,
22) => ReturnType<Create>
23β
24/**
25 * Wraps a rule's `create` function to track the identifiers imported from
26 * TanStack Query packages, and passes helpers that check them.
27 *
28 * @param create - The rule's `create` function, which also receives the
29 * helpers.
30 * @returns The wrapped `create` function.
31 */
32export function detectTanstackQueryImports(create: EnhancedCreate): Create {
33 return (context, optionsWithDefault) => {
34 const tanstackQueryImportSpecifiers: Array<TSESTree.ImportClause> = []
35β
36 const helpers: Helpers = {
37 isSpecificTanstackQueryImport(node, source) {
38 return !!tanstackQueryImportSpecifiers.find((specifier) => {
39 if (
40 specifier.type === TSESTree.AST_NODE_TYPES.ImportSpecifier &&
41 specifier.parent.type ===
42 TSESTree.AST_NODE_TYPES.ImportDeclaration &&
43 specifier.parent.source.value === source
44 ) {
45 return node.name === specifier.local.name
46 }
47β
48 return false
49 })
50 },
51 isTanstackQueryImport(node) {
52 return !!tanstackQueryImportSpecifiers.find((specifier) => {
53 if (specifier.type === TSESTree.AST_NODE_TYPES.ImportSpecifier) {
54 return node.name === specifier.local.name
55 }
56β
57 return false
58 })
59 },
60 }
61β
62 const detectionInstructions: TSESLint.RuleListener = {
63 ImportDeclaration(node) {
64 if (
65 node.specifiers.length > 0 &&
66 // oxlint-disable-next-line typescript/no-unnecessary-condition
67 (node.importKind === 'value' || node.importKind === undefined) &&
68 node.source.value.startsWith('@tanstack/') &&
69 node.source.value.endsWith('-query')
70 ) {
71 tanstackQueryImportSpecifiers.push(...node.specifiers)
72 }
73 },
74 }
75β
76 // Call original rule definition
77 const ruleInstructions = create(context, optionsWithDefault, helpers)
78 const enhancedRuleInstructions: TSESLint.RuleListener = {}
79β
80 const allKeys = new Set(
81 Object.keys(detectionInstructions).concat(Object.keys(ruleInstructions)),
82 )
83β
84 // Iterate over ALL instructions keys so we can override original rule instructions
85 // to prevent their execution if conditions to report errors are not met.
86 allKeys.forEach((instruction) => {
87 enhancedRuleInstructions[instruction] = (node) => {
88 if (instruction in detectionInstructions) {
89 detectionInstructions[instruction]?.(node)
90 }
91β
92 const ruleInstruction = ruleInstructions[instruction]
93β
94 // TODO: canReportErrors()
95 if (ruleInstruction) {
96 return ruleInstruction(node)
97 }
98β
99 return undefined
100 }
101 })
102β
103 return enhancedRuleInstructions
104 }
105}