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 TanStack Query packages,
26 * and passes helpers that check them.
27 * @param create - The rule's `create` function, which also receives the helpers.
28 * @returns The wrapped `create` function.
29 */
30export function detectTanstackQueryImports(create: EnhancedCreate): Create {
31 return (context, optionsWithDefault) => {
32 const tanstackQueryImportSpecifiers: Array<TSESTree.ImportClause> = []
33β
34 const helpers: Helpers = {
35 isSpecificTanstackQueryImport(node, source) {
36 return !!tanstackQueryImportSpecifiers.find((specifier) => {
37 if (
38 specifier.type === TSESTree.AST_NODE_TYPES.ImportSpecifier &&
39 specifier.parent.type ===
40 TSESTree.AST_NODE_TYPES.ImportDeclaration &&
41 specifier.parent.source.value === source
42 ) {
43 return node.name === specifier.local.name
44 }
45β
46 return false
47 })
48 },
49 isTanstackQueryImport(node) {
50 return !!tanstackQueryImportSpecifiers.find((specifier) => {
51 if (specifier.type === TSESTree.AST_NODE_TYPES.ImportSpecifier) {
52 return node.name === specifier.local.name
53 }
54β
55 return false
56 })
57 },
58 }
59β
60 const detectionInstructions: TSESLint.RuleListener = {
61 ImportDeclaration(node) {
62 if (
63 node.specifiers.length > 0 &&
64 // oxlint-disable-next-line typescript/no-unnecessary-condition -- 'importKind' is undefined when the file is not parsed as TypeScript
65 (node.importKind === 'value' || node.importKind === undefined) &&
66 node.source.value.startsWith('@tanstack/') &&
67 node.source.value.endsWith('-query')
68 ) {
69 tanstackQueryImportSpecifiers.push(...node.specifiers)
70 }
71 },
72 }
73β
74 // Call original rule definition
75 const ruleInstructions = create(context, optionsWithDefault, helpers)
76 const enhancedRuleInstructions: TSESLint.RuleListener = {}
77β
78 const allKeys = new Set(
79 Object.keys(detectionInstructions).concat(Object.keys(ruleInstructions)),
80 )
81β
82 // Iterate over ALL instructions keys so we can override original rule instructions
83 // to prevent their execution if conditions to report errors are not met.
84 allKeys.forEach((instruction) => {
85 enhancedRuleInstructions[instruction] = (node) => {
86 if (instruction in detectionInstructions) {
87 detectionInstructions[instruction]?.(node)
88 }
89β
90 const ruleInstruction = ruleInstructions[instruction]
91β
92 // TODO: canReportErrors()
93 if (ruleInstruction) {
94 return ruleInstruction(node)
95 }
96β
97 return undefined
98 }
99 })
100β
101 return enhancedRuleInstructions
102 }
103}