@tanstack/eslint-plugin-query 5.94.4 → 5.95.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/build/legacy/_tsup-dts-rollup.d.cts +329 -0
  2. package/build/legacy/_tsup-dts-rollup.d.ts +329 -0
  3. package/build/legacy/chunk-T44AUBX4.js +1344 -0
  4. package/build/legacy/chunk-T44AUBX4.js.map +1 -0
  5. package/build/legacy/index.cjs +1413 -0
  6. package/build/legacy/index.cjs.map +1 -0
  7. package/build/legacy/index.d.cts +3 -0
  8. package/build/legacy/index.d.ts +3 -0
  9. package/build/legacy/index.js +50 -0
  10. package/build/legacy/index.js.map +1 -0
  11. package/build/legacy/rules.cjs +1370 -0
  12. package/build/legacy/rules.cjs.map +1 -0
  13. package/build/legacy/rules.d.cts +1 -0
  14. package/build/legacy/rules.d.ts +1 -0
  15. package/build/legacy/rules.js +7 -0
  16. package/build/legacy/rules.js.map +1 -0
  17. package/build/legacy/types.cjs +19 -0
  18. package/build/legacy/types.cjs.map +1 -0
  19. package/build/legacy/types.d.cts +1 -0
  20. package/build/legacy/types.d.ts +1 -0
  21. package/build/legacy/types.js +1 -0
  22. package/build/legacy/types.js.map +1 -0
  23. package/build/modern/_tsup-dts-rollup.d.cts +329 -0
  24. package/build/modern/_tsup-dts-rollup.d.ts +329 -0
  25. package/build/modern/chunk-XH4ABCYA.js +1335 -0
  26. package/build/modern/chunk-XH4ABCYA.js.map +1 -0
  27. package/build/modern/index.cjs +1405 -0
  28. package/build/modern/index.cjs.map +1 -0
  29. package/build/modern/index.d.cts +3 -0
  30. package/build/modern/index.d.ts +3 -0
  31. package/build/modern/index.js +50 -0
  32. package/build/modern/index.js.map +1 -0
  33. package/build/modern/rules.cjs +1362 -0
  34. package/build/modern/rules.cjs.map +1 -0
  35. package/build/modern/rules.d.cts +1 -0
  36. package/build/modern/rules.d.ts +1 -0
  37. package/build/modern/rules.js +7 -0
  38. package/build/modern/rules.js.map +1 -0
  39. package/build/modern/types.cjs +20 -0
  40. package/build/modern/types.cjs.map +1 -0
  41. package/build/modern/types.d.cts +1 -0
  42. package/build/modern/types.d.ts +1 -0
  43. package/build/modern/types.js +1 -0
  44. package/build/modern/types.js.map +1 -0
  45. package/package.json +1 -1
  46. package/src/rules/exhaustive-deps/exhaustive-deps.rule.ts +136 -90
  47. package/src/rules/exhaustive-deps/exhaustive-deps.utils.ts +248 -22
  48. package/src/utils/ast-utils.ts +2 -25
@@ -0,0 +1,1335 @@
1
+ // src/rules/exhaustive-deps/exhaustive-deps.rule.ts
2
+ import { AST_NODE_TYPES as AST_NODE_TYPES3, ESLintUtils } from "@typescript-eslint/utils";
3
+
4
+ // src/utils/ast-utils.ts
5
+ import { AST_NODE_TYPES } from "@typescript-eslint/utils";
6
+
7
+ // src/utils/unique-by.ts
8
+ function uniqueBy(arr, fn) {
9
+ return arr.filter((x, i, a) => a.findIndex((y) => fn(x) === fn(y)) === i);
10
+ }
11
+
12
+ // src/utils/ast-utils.ts
13
+ var ASTUtils = {
14
+ isNodeOfOneOf(node, types) {
15
+ return types.includes(node.type);
16
+ },
17
+ isIdentifier(node) {
18
+ return node.type === AST_NODE_TYPES.Identifier;
19
+ },
20
+ isIdentifierWithName(node, name8) {
21
+ return ASTUtils.isIdentifier(node) && node.name === name8;
22
+ },
23
+ isIdentifierWithOneOfNames(node, name8) {
24
+ return ASTUtils.isIdentifier(node) && name8.includes(node.name);
25
+ },
26
+ isProperty(node) {
27
+ return node.type === AST_NODE_TYPES.Property;
28
+ },
29
+ isObjectExpression(node) {
30
+ return node.type === AST_NODE_TYPES.ObjectExpression;
31
+ },
32
+ isPropertyWithIdentifierKey(node, key) {
33
+ return ASTUtils.isProperty(node) && ASTUtils.isIdentifierWithName(node.key, key);
34
+ },
35
+ findPropertyWithIdentifierKey(properties, key) {
36
+ return properties.find(
37
+ (x) => ASTUtils.isPropertyWithIdentifierKey(x, key)
38
+ );
39
+ },
40
+ getNestedIdentifiers(node) {
41
+ const identifiers = [];
42
+ if (ASTUtils.isIdentifier(node)) {
43
+ identifiers.push(node);
44
+ }
45
+ if ("arguments" in node) {
46
+ node.arguments.forEach((x) => {
47
+ identifiers.push(...ASTUtils.getNestedIdentifiers(x));
48
+ });
49
+ }
50
+ if ("elements" in node) {
51
+ node.elements.forEach((x) => {
52
+ if (x !== null) {
53
+ identifiers.push(...ASTUtils.getNestedIdentifiers(x));
54
+ }
55
+ });
56
+ }
57
+ if ("properties" in node) {
58
+ node.properties.forEach((x) => {
59
+ identifiers.push(...ASTUtils.getNestedIdentifiers(x));
60
+ });
61
+ }
62
+ if ("expressions" in node) {
63
+ node.expressions.forEach((x) => {
64
+ identifiers.push(...ASTUtils.getNestedIdentifiers(x));
65
+ });
66
+ }
67
+ if ("left" in node) {
68
+ identifiers.push(...ASTUtils.getNestedIdentifiers(node.left));
69
+ }
70
+ if ("right" in node) {
71
+ identifiers.push(...ASTUtils.getNestedIdentifiers(node.right));
72
+ }
73
+ if (node.type === AST_NODE_TYPES.Property) {
74
+ identifiers.push(...ASTUtils.getNestedIdentifiers(node.value));
75
+ }
76
+ if (node.type === AST_NODE_TYPES.SpreadElement) {
77
+ identifiers.push(...ASTUtils.getNestedIdentifiers(node.argument));
78
+ }
79
+ if (node.type === AST_NODE_TYPES.MemberExpression) {
80
+ identifiers.push(...ASTUtils.getNestedIdentifiers(node.object));
81
+ }
82
+ if (node.type === AST_NODE_TYPES.UnaryExpression) {
83
+ identifiers.push(...ASTUtils.getNestedIdentifiers(node.argument));
84
+ }
85
+ if (node.type === AST_NODE_TYPES.ChainExpression) {
86
+ identifiers.push(...ASTUtils.getNestedIdentifiers(node.expression));
87
+ }
88
+ if (node.type === AST_NODE_TYPES.TSNonNullExpression) {
89
+ identifiers.push(...ASTUtils.getNestedIdentifiers(node.expression));
90
+ }
91
+ if (node.type === AST_NODE_TYPES.ArrowFunctionExpression) {
92
+ identifiers.push(...ASTUtils.getNestedIdentifiers(node.body));
93
+ }
94
+ if (node.type === AST_NODE_TYPES.FunctionExpression) {
95
+ identifiers.push(...ASTUtils.getNestedIdentifiers(node.body));
96
+ }
97
+ if (node.type === AST_NODE_TYPES.BlockStatement) {
98
+ identifiers.push(
99
+ ...node.body.map((body) => ASTUtils.getNestedIdentifiers(body)).flat()
100
+ );
101
+ }
102
+ if (node.type === AST_NODE_TYPES.ReturnStatement && node.argument) {
103
+ identifiers.push(...ASTUtils.getNestedIdentifiers(node.argument));
104
+ }
105
+ return identifiers;
106
+ },
107
+ traverseUpOnly(identifier, allowedNodeTypes) {
108
+ const parent = identifier.parent;
109
+ if (parent !== void 0 && allowedNodeTypes.includes(parent.type)) {
110
+ return ASTUtils.traverseUpOnly(parent, allowedNodeTypes);
111
+ }
112
+ return identifier;
113
+ },
114
+ isDeclaredInNode(params) {
115
+ const { functionNode, reference, scopeManager } = params;
116
+ const scope = scopeManager.acquire(functionNode);
117
+ if (scope === null) {
118
+ return false;
119
+ }
120
+ return scope.set.has(reference.identifier.name);
121
+ },
122
+ getExternalRefs(params) {
123
+ const { scopeManager, sourceCode, node } = params;
124
+ const scope = scopeManager.acquire(node);
125
+ if (scope === null) {
126
+ return [];
127
+ }
128
+ const collectReferences = (currentScope) => {
129
+ const references2 = [...currentScope.references];
130
+ for (const childScope of currentScope.childScopes) {
131
+ references2.push(...collectReferences(childScope));
132
+ }
133
+ return references2;
134
+ };
135
+ const references = collectReferences(scope).filter((x) => x.isRead() && !scope.set.has(x.identifier.name)).map((x) => {
136
+ const referenceNode = ASTUtils.traverseUpOnly(x.identifier, [
137
+ AST_NODE_TYPES.MemberExpression,
138
+ AST_NODE_TYPES.Identifier
139
+ ]);
140
+ return {
141
+ variable: x,
142
+ node: referenceNode,
143
+ text: sourceCode.getText(referenceNode)
144
+ };
145
+ });
146
+ const localRefIds = new Set(
147
+ [...scope.set.values()].map((x) => sourceCode.getText(x.identifiers[0]))
148
+ );
149
+ const externalRefs = references.filter(
150
+ (x) => x.variable.resolved === null || !localRefIds.has(x.text)
151
+ );
152
+ return uniqueBy(externalRefs, (x) => x.text).map((x) => x.variable);
153
+ },
154
+ mapKeyNodeToText(node, sourceCode) {
155
+ return sourceCode.getText(
156
+ ASTUtils.traverseUpOnly(node, [
157
+ AST_NODE_TYPES.MemberExpression,
158
+ AST_NODE_TYPES.TSNonNullExpression,
159
+ AST_NODE_TYPES.Identifier
160
+ ])
161
+ );
162
+ },
163
+ mapKeyNodeToBaseText(node, sourceCode) {
164
+ return ASTUtils.mapKeyNodeToText(node, sourceCode).replace(
165
+ /(?:\?(\.)|!)/g,
166
+ "$1"
167
+ );
168
+ },
169
+ isValidReactComponentOrHookName(identifier) {
170
+ return identifier !== null && identifier !== void 0 && /^(use|[A-Z])/.test(identifier.name);
171
+ },
172
+ getFunctionAncestor(sourceCode, node) {
173
+ for (const ancestor of sourceCode.getAncestors(node)) {
174
+ if (ASTUtils.isNodeOfOneOf(ancestor, [
175
+ AST_NODE_TYPES.FunctionDeclaration,
176
+ AST_NODE_TYPES.FunctionExpression,
177
+ AST_NODE_TYPES.ArrowFunctionExpression
178
+ ])) {
179
+ return ancestor;
180
+ }
181
+ if (ancestor.parent?.type === AST_NODE_TYPES.VariableDeclarator && ancestor.parent.id.type === AST_NODE_TYPES.Identifier && ASTUtils.isNodeOfOneOf(ancestor, [
182
+ AST_NODE_TYPES.FunctionDeclaration,
183
+ AST_NODE_TYPES.FunctionExpression,
184
+ AST_NODE_TYPES.ArrowFunctionExpression
185
+ ])) {
186
+ return ancestor;
187
+ }
188
+ }
189
+ return void 0;
190
+ },
191
+ getReferencedExpressionByIdentifier(params) {
192
+ const { node, context } = params;
193
+ const sourceCode = context.sourceCode ?? context.getSourceCode();
194
+ const scope = context.sourceCode.getScope(node) ? sourceCode.getScope(node) : context.getScope();
195
+ const resolvedNode = scope.references.find((ref) => ref.identifier === node)?.resolved?.defs[0]?.node;
196
+ if (resolvedNode?.type !== AST_NODE_TYPES.VariableDeclarator) {
197
+ return null;
198
+ }
199
+ return resolvedNode.init;
200
+ },
201
+ getClosestVariableDeclarator(node) {
202
+ let currentNode = node;
203
+ while (currentNode.type !== AST_NODE_TYPES.Program) {
204
+ if (currentNode.type === AST_NODE_TYPES.VariableDeclarator) {
205
+ return currentNode;
206
+ }
207
+ currentNode = currentNode.parent;
208
+ }
209
+ return void 0;
210
+ },
211
+ getNestedReturnStatements(node) {
212
+ const returnStatements = [];
213
+ if (node.type === AST_NODE_TYPES.ReturnStatement) {
214
+ returnStatements.push(node);
215
+ }
216
+ if ("body" in node && node.body !== void 0 && node.body !== null) {
217
+ Array.isArray(node.body) ? node.body.forEach((x) => {
218
+ returnStatements.push(...ASTUtils.getNestedReturnStatements(x));
219
+ }) : returnStatements.push(
220
+ ...ASTUtils.getNestedReturnStatements(node.body)
221
+ );
222
+ }
223
+ if ("consequent" in node) {
224
+ Array.isArray(node.consequent) ? node.consequent.forEach((x) => {
225
+ returnStatements.push(...ASTUtils.getNestedReturnStatements(x));
226
+ }) : returnStatements.push(
227
+ ...ASTUtils.getNestedReturnStatements(node.consequent)
228
+ );
229
+ }
230
+ if ("alternate" in node && node.alternate !== null) {
231
+ Array.isArray(node.alternate) ? node.alternate.forEach((x) => {
232
+ returnStatements.push(...ASTUtils.getNestedReturnStatements(x));
233
+ }) : returnStatements.push(
234
+ ...ASTUtils.getNestedReturnStatements(node.alternate)
235
+ );
236
+ }
237
+ if ("cases" in node) {
238
+ node.cases.forEach((x) => {
239
+ returnStatements.push(...ASTUtils.getNestedReturnStatements(x));
240
+ });
241
+ }
242
+ if ("block" in node) {
243
+ returnStatements.push(...ASTUtils.getNestedReturnStatements(node.block));
244
+ }
245
+ if ("handler" in node && node.handler !== null) {
246
+ returnStatements.push(...ASTUtils.getNestedReturnStatements(node.handler));
247
+ }
248
+ if ("finalizer" in node && node.finalizer !== null) {
249
+ returnStatements.push(
250
+ ...ASTUtils.getNestedReturnStatements(node.finalizer)
251
+ );
252
+ }
253
+ if ("expression" in node && node.expression !== true && node.expression !== false) {
254
+ returnStatements.push(
255
+ ...ASTUtils.getNestedReturnStatements(node.expression)
256
+ );
257
+ }
258
+ if ("test" in node && node.test !== null) {
259
+ returnStatements.push(...ASTUtils.getNestedReturnStatements(node.test));
260
+ }
261
+ return returnStatements;
262
+ }
263
+ };
264
+
265
+ // src/utils/get-docs-url.ts
266
+ var getDocsUrl = (ruleName) => `https://tanstack.com/query/latest/docs/eslint/${ruleName}`;
267
+
268
+ // src/utils/detect-react-query-imports.ts
269
+ import { TSESTree } from "@typescript-eslint/utils";
270
+ function detectTanstackQueryImports(create) {
271
+ return (context, optionsWithDefault) => {
272
+ const tanstackQueryImportSpecifiers = [];
273
+ const helpers = {
274
+ isSpecificTanstackQueryImport(node, source) {
275
+ return !!tanstackQueryImportSpecifiers.find((specifier) => {
276
+ if (specifier.type === TSESTree.AST_NODE_TYPES.ImportSpecifier && specifier.parent.type === TSESTree.AST_NODE_TYPES.ImportDeclaration && specifier.parent.source.value === source) {
277
+ return node.name === specifier.local.name;
278
+ }
279
+ return false;
280
+ });
281
+ },
282
+ isTanstackQueryImport(node) {
283
+ return !!tanstackQueryImportSpecifiers.find((specifier) => {
284
+ if (specifier.type === TSESTree.AST_NODE_TYPES.ImportSpecifier) {
285
+ return node.name === specifier.local.name;
286
+ }
287
+ return false;
288
+ });
289
+ }
290
+ };
291
+ const detectionInstructions = {
292
+ ImportDeclaration(node) {
293
+ if (node.specifiers.length > 0 && // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
294
+ (node.importKind === "value" || node.importKind === void 0) && node.source.value.startsWith("@tanstack/") && node.source.value.endsWith("-query")) {
295
+ tanstackQueryImportSpecifiers.push(...node.specifiers);
296
+ }
297
+ }
298
+ };
299
+ const ruleInstructions = create(context, optionsWithDefault, helpers);
300
+ const enhancedRuleInstructions = {};
301
+ const allKeys = new Set(
302
+ Object.keys(detectionInstructions).concat(Object.keys(ruleInstructions))
303
+ );
304
+ allKeys.forEach((instruction) => {
305
+ enhancedRuleInstructions[instruction] = (node) => {
306
+ if (instruction in detectionInstructions) {
307
+ detectionInstructions[instruction]?.(node);
308
+ }
309
+ const ruleInstruction = ruleInstructions[instruction];
310
+ if (ruleInstruction) {
311
+ return ruleInstruction(node);
312
+ }
313
+ return void 0;
314
+ };
315
+ });
316
+ return enhancedRuleInstructions;
317
+ };
318
+ }
319
+
320
+ // src/rules/exhaustive-deps/exhaustive-deps.utils.ts
321
+ import { AST_NODE_TYPES as AST_NODE_TYPES2 } from "@typescript-eslint/utils";
322
+ var ExhaustiveDepsUtils = {
323
+ isRelevantReference(params) {
324
+ const { sourceCode, reference, scopeManager, node, filename } = params;
325
+ const component = ASTUtils.getFunctionAncestor(sourceCode, node);
326
+ const queryFnScope = scopeManager.acquire(node);
327
+ if (queryFnScope === null || reference.isValueReference === false) {
328
+ return false;
329
+ }
330
+ let currentScope = reference.resolved?.scope ?? null;
331
+ while (currentScope !== null) {
332
+ if (currentScope === queryFnScope) {
333
+ return false;
334
+ }
335
+ currentScope = currentScope.upper;
336
+ }
337
+ if (component !== void 0) {
338
+ if (!ASTUtils.isDeclaredInNode({
339
+ scopeManager,
340
+ reference,
341
+ functionNode: component
342
+ })) {
343
+ return false;
344
+ }
345
+ } else {
346
+ const isVueFile = filename.endsWith(".vue");
347
+ if (!isVueFile) {
348
+ return false;
349
+ }
350
+ const definition = reference.resolved?.defs[0];
351
+ const isGlobalVariable = definition === void 0;
352
+ const isImport = definition?.type === "ImportBinding";
353
+ if (isGlobalVariable || isImport) {
354
+ return false;
355
+ }
356
+ }
357
+ return reference.identifier.name !== "undefined" && reference.identifier.parent.type !== AST_NODE_TYPES2.NewExpression && !ExhaustiveDepsUtils.isInstanceOfKind(reference.identifier.parent);
358
+ },
359
+ /**
360
+ * Given required refs and existing queryKey entries, compute missing dependency paths
361
+ * respecting allowlisted variables and types.
362
+ */
363
+ computeFilteredMissingPaths(params) {
364
+ const {
365
+ requiredRefs,
366
+ allowlistedVariables,
367
+ existingRootIdentifiers,
368
+ existingFullPaths
369
+ } = params;
370
+ const missingPaths = /* @__PURE__ */ new Set();
371
+ for (const { root, path, allowlistedByType } of requiredRefs) {
372
+ if (existingRootIdentifiers.has(root)) continue;
373
+ if (allowlistedVariables.has(root)) continue;
374
+ if (existingFullPaths.has(path)) continue;
375
+ if (allowlistedByType) continue;
376
+ missingPaths.add(path);
377
+ }
378
+ for (const path of missingPaths) {
379
+ const root = path.split(".")[0];
380
+ if (root !== path && root !== void 0 && missingPaths.has(root)) {
381
+ missingPaths.delete(path);
382
+ }
383
+ }
384
+ return Array.from(missingPaths);
385
+ },
386
+ /**
387
+ * Extract existing queryKey deps as root identifiers and full member paths.
388
+ */
389
+ collectQueryKeyDeps(params) {
390
+ const { sourceCode, scopeManager, queryKeyNode } = params;
391
+ const roots = /* @__PURE__ */ new Set();
392
+ const paths = /* @__PURE__ */ new Set();
393
+ const visitorKeys = sourceCode.visitorKeys;
394
+ function addRoot(name8) {
395
+ const cleaned = ExhaustiveDepsUtils.normalizeChain(name8);
396
+ roots.add(cleaned);
397
+ paths.add(cleaned);
398
+ }
399
+ function addFull(text) {
400
+ const cleaned = ExhaustiveDepsUtils.normalizeChain(text);
401
+ paths.add(cleaned);
402
+ }
403
+ function addRefPath(refPath) {
404
+ if (!refPath) return;
405
+ if (refPath.coversRootMembers) {
406
+ addRoot(refPath.root);
407
+ return;
408
+ }
409
+ addFull(refPath.path);
410
+ }
411
+ function visitChildren(node) {
412
+ const keys = visitorKeys[node.type] ?? [];
413
+ for (const key of keys) {
414
+ const value = node[key];
415
+ if (Array.isArray(value)) {
416
+ for (const item of value) {
417
+ if (ExhaustiveDepsUtils.isNode(item)) {
418
+ visit(item);
419
+ }
420
+ }
421
+ continue;
422
+ }
423
+ if (ExhaustiveDepsUtils.isNode(value)) {
424
+ visit(value);
425
+ }
426
+ }
427
+ }
428
+ function visit(node) {
429
+ if (!node) return;
430
+ switch (node.type) {
431
+ case AST_NODE_TYPES2.Identifier: {
432
+ addRefPath(
433
+ ExhaustiveDepsUtils.computeRefPath({
434
+ identifier: node,
435
+ sourceCode
436
+ })
437
+ );
438
+ return;
439
+ }
440
+ case AST_NODE_TYPES2.ArrowFunctionExpression:
441
+ case AST_NODE_TYPES2.FunctionExpression:
442
+ for (const reference of ExhaustiveDepsUtils.collectExternalRefsInFunction(
443
+ {
444
+ functionNode: node,
445
+ scopeManager
446
+ }
447
+ )) {
448
+ if (reference.identifier.type !== AST_NODE_TYPES2.Identifier) {
449
+ continue;
450
+ }
451
+ addRefPath(
452
+ ExhaustiveDepsUtils.computeRefPath({
453
+ identifier: reference.identifier,
454
+ sourceCode
455
+ })
456
+ );
457
+ }
458
+ return;
459
+ case AST_NODE_TYPES2.Property:
460
+ visit(node.value);
461
+ return;
462
+ case AST_NODE_TYPES2.MemberExpression:
463
+ if (node.parent.type === AST_NODE_TYPES2.CallExpression && node.parent.callee === node && node.object.type === AST_NODE_TYPES2.Identifier) {
464
+ addRoot(node.object.name);
465
+ } else {
466
+ visit(node.object);
467
+ }
468
+ return;
469
+ case AST_NODE_TYPES2.CallExpression:
470
+ node.arguments.forEach((argument) => visit(argument));
471
+ switch (node.callee.type) {
472
+ case AST_NODE_TYPES2.Identifier:
473
+ case AST_NODE_TYPES2.MemberExpression:
474
+ case AST_NODE_TYPES2.ChainExpression:
475
+ case AST_NODE_TYPES2.TSNonNullExpression:
476
+ visit(node.callee);
477
+ break;
478
+ }
479
+ return;
480
+ }
481
+ visitChildren(node);
482
+ }
483
+ visit(queryKeyNode);
484
+ return { roots, paths };
485
+ },
486
+ isNode(value) {
487
+ return typeof value === "object" && value !== null && "type" in value && typeof value.type === "string";
488
+ },
489
+ /**
490
+ * Checks whether the resolved variable is allowlisted by its type annotation
491
+ */
492
+ variableIsAllowlistedByType(params) {
493
+ const { allowlistedTypes, variable } = params;
494
+ if (allowlistedTypes.size === 0) return false;
495
+ if (!variable) return false;
496
+ for (const id of variable.identifiers) {
497
+ if (id.typeAnnotation) {
498
+ const typeIdentifiers = /* @__PURE__ */ new Set();
499
+ ExhaustiveDepsUtils.collectTypeIdentifiers(
500
+ id.typeAnnotation.typeAnnotation,
501
+ typeIdentifiers
502
+ );
503
+ for (const typeIdentifier of typeIdentifiers) {
504
+ if (allowlistedTypes.has(typeIdentifier)) return true;
505
+ }
506
+ }
507
+ }
508
+ return false;
509
+ },
510
+ isInstanceOfKind(node) {
511
+ return node.type === AST_NODE_TYPES2.BinaryExpression && node.operator === "instanceof";
512
+ },
513
+ /**
514
+ * Normalizes a chain by removing optional chaining operators
515
+ *
516
+ * Example: `a?.b.c!` -> `a.b.c`
517
+ */
518
+ normalizeChain(text) {
519
+ return text.replace(/(?:\?(\.)|!)/g, "$1");
520
+ },
521
+ /**
522
+ * Computes the reference path for an identifier
523
+ *
524
+ * Example: `a.b.c!` -> `{ path: 'a.b.c', root: 'a' }`
525
+ */
526
+ computeRefPath(params) {
527
+ const { identifier, sourceCode } = params;
528
+ const fullChainNode = ASTUtils.traverseUpOnly(identifier, [
529
+ AST_NODE_TYPES2.MemberExpression,
530
+ AST_NODE_TYPES2.TSNonNullExpression,
531
+ AST_NODE_TYPES2.Identifier
532
+ ]);
533
+ const fullText = ExhaustiveDepsUtils.normalizeChain(
534
+ sourceCode.getText(fullChainNode)
535
+ );
536
+ const parent = fullChainNode.parent;
537
+ let dependencyPath = fullText;
538
+ let coversRootMembers = fullText === identifier.name;
539
+ if (parent && parent.type === AST_NODE_TYPES2.CallExpression && parent.callee === fullChainNode) {
540
+ const segments = fullText.split(".");
541
+ if (segments.length > 1) {
542
+ dependencyPath = segments.slice(0, -1).join(".");
543
+ }
544
+ coversRootMembers = false;
545
+ }
546
+ dependencyPath = dependencyPath.split(".")[0] === "" ? identifier.name : dependencyPath;
547
+ const root = dependencyPath.split(".")[0];
548
+ return {
549
+ path: dependencyPath,
550
+ root: root ?? identifier.name,
551
+ coversRootMembers: coversRootMembers && dependencyPath === root
552
+ };
553
+ },
554
+ collectExternalRefsInFunction(params) {
555
+ const { functionNode, scopeManager } = params;
556
+ const functionScope = scopeManager.acquire(functionNode);
557
+ if (functionScope === null) {
558
+ return [];
559
+ }
560
+ const externalRefs = [];
561
+ function collect(scope) {
562
+ for (const reference of scope.references) {
563
+ if (!reference.isRead() || reference.resolved === null) {
564
+ continue;
565
+ }
566
+ let currentScope = reference.resolved.scope;
567
+ let declaredInsideFunction = false;
568
+ while (currentScope !== null) {
569
+ if (currentScope === functionScope) {
570
+ declaredInsideFunction = true;
571
+ break;
572
+ }
573
+ currentScope = currentScope.upper;
574
+ }
575
+ if (!declaredInsideFunction) {
576
+ externalRefs.push(reference);
577
+ }
578
+ }
579
+ for (const childScope of scope.childScopes) {
580
+ collect(childScope);
581
+ }
582
+ }
583
+ collect(functionScope);
584
+ return externalRefs;
585
+ },
586
+ /**
587
+ * Recursively collects type identifiers from a type annotation
588
+ */
589
+ collectTypeIdentifiers(typeNode, out) {
590
+ switch (typeNode.type) {
591
+ case AST_NODE_TYPES2.TSTypeReference: {
592
+ if (typeNode.typeName.type === AST_NODE_TYPES2.Identifier) {
593
+ out.add(typeNode.typeName.name);
594
+ }
595
+ break;
596
+ }
597
+ case AST_NODE_TYPES2.TSUnionType:
598
+ case AST_NODE_TYPES2.TSIntersectionType: {
599
+ typeNode.types.forEach(
600
+ (t) => ExhaustiveDepsUtils.collectTypeIdentifiers(t, out)
601
+ );
602
+ break;
603
+ }
604
+ case AST_NODE_TYPES2.TSArrayType: {
605
+ ExhaustiveDepsUtils.collectTypeIdentifiers(typeNode.elementType, out);
606
+ break;
607
+ }
608
+ case AST_NODE_TYPES2.TSTupleType: {
609
+ typeNode.elementTypes.forEach(
610
+ (et) => ExhaustiveDepsUtils.collectTypeIdentifiers(et, out)
611
+ );
612
+ break;
613
+ }
614
+ }
615
+ },
616
+ /**
617
+ * Gets the function expression nodes from a queryFn property, handling conditional expressions.
618
+ * When neither branch is skipToken, returns both branches so all deps are scanned.
619
+ */
620
+ getQueryFnNodes(queryFn) {
621
+ if (queryFn.value.type !== AST_NODE_TYPES2.ConditionalExpression) {
622
+ return [queryFn.value];
623
+ }
624
+ if (queryFn.value.consequent.type === AST_NODE_TYPES2.Identifier && queryFn.value.consequent.name === "skipToken") {
625
+ return [queryFn.value.alternate];
626
+ }
627
+ if (queryFn.value.alternate.type === AST_NODE_TYPES2.Identifier && queryFn.value.alternate.name === "skipToken") {
628
+ return [queryFn.value.consequent];
629
+ }
630
+ return [queryFn.value.consequent, queryFn.value.alternate];
631
+ }
632
+ };
633
+
634
+ // src/rules/exhaustive-deps/exhaustive-deps.rule.ts
635
+ var QUERY_KEY = "queryKey";
636
+ var QUERY_FN = "queryFn";
637
+ var name = "exhaustive-deps";
638
+ var createRule = ESLintUtils.RuleCreator(getDocsUrl);
639
+ var rule = createRule({
640
+ name,
641
+ meta: {
642
+ type: "problem",
643
+ docs: {
644
+ description: "Exhaustive deps rule for useQuery",
645
+ recommended: "error"
646
+ },
647
+ messages: {
648
+ missingDeps: `The following dependencies are missing in your queryKey: {{deps}}`,
649
+ fixTo: "Fix to {{result}}"
650
+ },
651
+ hasSuggestions: true,
652
+ fixable: "code",
653
+ schema: [
654
+ {
655
+ type: "object",
656
+ properties: {
657
+ allowlist: {
658
+ type: "object",
659
+ properties: {
660
+ variables: { type: "array", items: { type: "string" } },
661
+ types: { type: "array", items: { type: "string" } }
662
+ },
663
+ additionalProperties: false
664
+ }
665
+ },
666
+ additionalProperties: false
667
+ }
668
+ ]
669
+ },
670
+ defaultOptions: [],
671
+ create: detectTanstackQueryImports((context) => {
672
+ return {
673
+ ObjectExpression: (node) => {
674
+ const scopeManager = context.sourceCode.scopeManager;
675
+ const queryKey = ASTUtils.findPropertyWithIdentifierKey(
676
+ node.properties,
677
+ QUERY_KEY
678
+ );
679
+ const queryFn = ASTUtils.findPropertyWithIdentifierKey(
680
+ node.properties,
681
+ QUERY_FN
682
+ );
683
+ if (scopeManager === null || queryKey === void 0 || queryFn === void 0 || !ASTUtils.isNodeOfOneOf(queryFn.value, [
684
+ AST_NODE_TYPES3.ArrowFunctionExpression,
685
+ AST_NODE_TYPES3.FunctionExpression,
686
+ AST_NODE_TYPES3.ConditionalExpression
687
+ ])) {
688
+ return;
689
+ }
690
+ const queryKeyNode = dereferenceVariablesAndTypeAssertions(
691
+ queryKey.value,
692
+ context
693
+ );
694
+ const queryFnNodes = ExhaustiveDepsUtils.getQueryFnNodes(queryFn);
695
+ const externalRefs = queryFnNodes.flatMap(
696
+ (fnNode) => ASTUtils.getExternalRefs({
697
+ scopeManager,
698
+ sourceCode: context.sourceCode,
699
+ node: fnNode
700
+ })
701
+ );
702
+ const relevantRefs = externalRefs.filter(
703
+ (reference) => queryFnNodes.some(
704
+ (fnNode) => ExhaustiveDepsUtils.isRelevantReference({
705
+ sourceCode: context.sourceCode,
706
+ reference,
707
+ scopeManager,
708
+ node: fnNode,
709
+ filename: context.filename
710
+ })
711
+ )
712
+ );
713
+ const ruleOptions = context.options.at(0);
714
+ const allowlistedVariables = new Set(
715
+ ruleOptions?.allowlist?.variables ?? []
716
+ );
717
+ const allowlistedTypes = new Set(ruleOptions?.allowlist?.types ?? []);
718
+ const requiredRefs = relevantRefs.flatMap((ref) => {
719
+ if (ref.identifier.type !== AST_NODE_TYPES3.Identifier) return [];
720
+ const refPath = ExhaustiveDepsUtils.computeRefPath({
721
+ identifier: ref.identifier,
722
+ sourceCode: context.sourceCode
723
+ });
724
+ if (refPath === null) return [];
725
+ return [
726
+ {
727
+ ...refPath,
728
+ allowlistedByType: ExhaustiveDepsUtils.variableIsAllowlistedByType({
729
+ allowlistedTypes,
730
+ variable: ref.resolved ?? null
731
+ })
732
+ }
733
+ ];
734
+ });
735
+ if (requiredRefs.length === 0) return;
736
+ const queryKeyDeps = ExhaustiveDepsUtils.collectQueryKeyDeps({
737
+ sourceCode: context.sourceCode,
738
+ scopeManager,
739
+ queryKeyNode
740
+ });
741
+ const missingPaths = ExhaustiveDepsUtils.computeFilteredMissingPaths({
742
+ requiredRefs,
743
+ allowlistedVariables,
744
+ existingRootIdentifiers: queryKeyDeps.roots,
745
+ existingFullPaths: queryKeyDeps.paths
746
+ });
747
+ if (missingPaths.length === 0) return;
748
+ const missingAsText = missingPaths.join(", ");
749
+ const suggestions = buildSuggestions({
750
+ queryKeyNode,
751
+ missingPaths,
752
+ missingAsText,
753
+ sourceCode: context.sourceCode
754
+ });
755
+ context.report({
756
+ node,
757
+ messageId: "missingDeps",
758
+ data: { deps: missingAsText },
759
+ suggest: suggestions
760
+ });
761
+ }
762
+ };
763
+ })
764
+ });
765
+ function buildSuggestions(params) {
766
+ const { queryKeyNode, missingPaths, missingAsText, sourceCode } = params;
767
+ if (queryKeyNode.type !== AST_NODE_TYPES3.ArrayExpression) {
768
+ return [];
769
+ }
770
+ const closingBracket = sourceCode.getLastToken(queryKeyNode);
771
+ if (!closingBracket) return [];
772
+ const existingElements = queryKeyNode.elements.filter((el) => el !== null).map((el) => sourceCode.getText(el));
773
+ const resultText = `[${[...existingElements, ...missingPaths].join(", ")}]`;
774
+ if (queryKeyNode.elements.length === 0) {
775
+ return [
776
+ {
777
+ messageId: "fixTo",
778
+ data: { result: resultText },
779
+ fix: (fixer) => fixer.replaceText(queryKeyNode, resultText)
780
+ }
781
+ ];
782
+ }
783
+ const tokenBefore = sourceCode.getTokenBefore(closingBracket);
784
+ const separator = tokenBefore?.value === "," ? " " : ", ";
785
+ return [
786
+ {
787
+ messageId: "fixTo",
788
+ data: { result: resultText },
789
+ fix: (fixer) => fixer.insertTextBefore(closingBracket, `${separator}${missingAsText}`)
790
+ }
791
+ ];
792
+ }
793
+ function dereferenceVariablesAndTypeAssertions(queryKeyNode, context) {
794
+ const visitedNodes = /* @__PURE__ */ new Set();
795
+ for (let i = 0; i < 1 << 8; ++i) {
796
+ if (visitedNodes.has(queryKeyNode)) {
797
+ return queryKeyNode;
798
+ }
799
+ visitedNodes.add(queryKeyNode);
800
+ switch (queryKeyNode.type) {
801
+ case AST_NODE_TYPES3.TSAsExpression:
802
+ queryKeyNode = queryKeyNode.expression;
803
+ break;
804
+ case AST_NODE_TYPES3.Identifier: {
805
+ const expression = ASTUtils.getReferencedExpressionByIdentifier({
806
+ context,
807
+ node: queryKeyNode
808
+ });
809
+ if (expression == null) {
810
+ return queryKeyNode;
811
+ }
812
+ queryKeyNode = expression;
813
+ break;
814
+ }
815
+ default:
816
+ return queryKeyNode;
817
+ }
818
+ }
819
+ return queryKeyNode;
820
+ }
821
+
822
+ // src/rules/stable-query-client/stable-query-client.rule.ts
823
+ import { AST_NODE_TYPES as AST_NODE_TYPES4, ESLintUtils as ESLintUtils2 } from "@typescript-eslint/utils";
824
+ var name2 = "stable-query-client";
825
+ var createRule2 = ESLintUtils2.RuleCreator(getDocsUrl);
826
+ var rule2 = createRule2({
827
+ name: name2,
828
+ meta: {
829
+ type: "problem",
830
+ docs: {
831
+ description: "Makes sure that QueryClient is stable",
832
+ recommended: "error"
833
+ },
834
+ messages: {
835
+ unstable: [
836
+ "QueryClient is not stable. It should be either extracted from the component or wrapped in React.useState.",
837
+ "See https://tkdodo.eu/blog/react-query-fa-qs#2-the-queryclient-is-not-stable"
838
+ ].join("\n"),
839
+ fixTo: "Fix to {{result}}"
840
+ },
841
+ hasSuggestions: true,
842
+ fixable: "code",
843
+ schema: []
844
+ },
845
+ defaultOptions: [],
846
+ create: detectTanstackQueryImports((context, _, helpers) => {
847
+ return {
848
+ NewExpression: (node) => {
849
+ if (node.callee.type !== AST_NODE_TYPES4.Identifier || node.callee.name !== "QueryClient" || node.parent.type !== AST_NODE_TYPES4.VariableDeclarator || !helpers.isSpecificTanstackQueryImport(
850
+ node.callee,
851
+ "@tanstack/react-query"
852
+ )) {
853
+ return;
854
+ }
855
+ const fnAncestor = ASTUtils.getFunctionAncestor(
856
+ context.sourceCode,
857
+ node
858
+ );
859
+ const isReactServerComponent = fnAncestor?.async === true;
860
+ if (!ASTUtils.isValidReactComponentOrHookName(fnAncestor?.id) || isReactServerComponent) {
861
+ return;
862
+ }
863
+ context.report({
864
+ node: node.parent,
865
+ messageId: "unstable",
866
+ fix: (() => {
867
+ const { parent } = node;
868
+ if (parent.id.type !== AST_NODE_TYPES4.Identifier) {
869
+ return;
870
+ }
871
+ const sourceCode = context.sourceCode ?? context.getSourceCode();
872
+ const nodeText = sourceCode.getText(node);
873
+ const variableName = parent.id.name;
874
+ return (fixer) => {
875
+ return fixer.replaceTextRange(
876
+ [parent.range[0], parent.range[1]],
877
+ `[${variableName}] = React.useState(() => ${nodeText})`
878
+ );
879
+ };
880
+ })()
881
+ });
882
+ }
883
+ };
884
+ })
885
+ });
886
+
887
+ // src/rules/no-rest-destructuring/no-rest-destructuring.rule.ts
888
+ import { AST_NODE_TYPES as AST_NODE_TYPES6, ESLintUtils as ESLintUtils3 } from "@typescript-eslint/utils";
889
+
890
+ // src/rules/no-rest-destructuring/no-rest-destructuring.utils.ts
891
+ import { AST_NODE_TYPES as AST_NODE_TYPES5 } from "@typescript-eslint/utils";
892
+ var NoRestDestructuringUtils = {
893
+ isObjectRestDestructuring(node) {
894
+ if (node.type !== AST_NODE_TYPES5.ObjectPattern) {
895
+ return false;
896
+ }
897
+ return node.properties.some((p) => p.type === AST_NODE_TYPES5.RestElement);
898
+ }
899
+ };
900
+
901
+ // src/rules/no-rest-destructuring/no-rest-destructuring.rule.ts
902
+ var name3 = "no-rest-destructuring";
903
+ var queryHooks = [
904
+ "useQuery",
905
+ "useQueries",
906
+ "useInfiniteQuery",
907
+ "useSuspenseQuery",
908
+ "useSuspenseQueries",
909
+ "useSuspenseInfiniteQuery"
910
+ ];
911
+ var createRule3 = ESLintUtils3.RuleCreator(getDocsUrl);
912
+ var rule3 = createRule3({
913
+ name: name3,
914
+ meta: {
915
+ type: "problem",
916
+ docs: {
917
+ description: "Disallows rest destructuring in queries",
918
+ recommended: "warn"
919
+ },
920
+ messages: {
921
+ objectRestDestructure: `Object rest destructuring on a query will observe all changes to the query, leading to excessive re-renders.`
922
+ },
923
+ schema: []
924
+ },
925
+ defaultOptions: [],
926
+ create: detectTanstackQueryImports((context, _, helpers) => {
927
+ const queryResultVariables = /* @__PURE__ */ new Set();
928
+ return {
929
+ CallExpression: (node) => {
930
+ if (!ASTUtils.isIdentifierWithOneOfNames(node.callee, queryHooks) || node.parent.type !== AST_NODE_TYPES6.VariableDeclarator || !helpers.isTanstackQueryImport(node.callee)) {
931
+ return;
932
+ }
933
+ const returnValue = node.parent.id;
934
+ if (node.callee.name !== "useQueries" && node.callee.name !== "useSuspenseQueries") {
935
+ if (NoRestDestructuringUtils.isObjectRestDestructuring(returnValue)) {
936
+ return context.report({
937
+ node: node.parent,
938
+ messageId: "objectRestDestructure"
939
+ });
940
+ }
941
+ if (returnValue.type === AST_NODE_TYPES6.Identifier) {
942
+ queryResultVariables.add(returnValue.name);
943
+ }
944
+ return;
945
+ }
946
+ if (returnValue.type !== AST_NODE_TYPES6.ArrayPattern) {
947
+ if (returnValue.type === AST_NODE_TYPES6.Identifier) {
948
+ queryResultVariables.add(returnValue.name);
949
+ }
950
+ return;
951
+ }
952
+ returnValue.elements.forEach((queryResult) => {
953
+ if (queryResult === null) {
954
+ return;
955
+ }
956
+ if (NoRestDestructuringUtils.isObjectRestDestructuring(queryResult)) {
957
+ context.report({
958
+ node: queryResult,
959
+ messageId: "objectRestDestructure"
960
+ });
961
+ }
962
+ });
963
+ },
964
+ VariableDeclarator: (node) => {
965
+ if (node.init?.type === AST_NODE_TYPES6.Identifier && queryResultVariables.has(node.init.name) && NoRestDestructuringUtils.isObjectRestDestructuring(node.id)) {
966
+ context.report({
967
+ node,
968
+ messageId: "objectRestDestructure"
969
+ });
970
+ }
971
+ },
972
+ SpreadElement: (node) => {
973
+ if (node.argument.type === AST_NODE_TYPES6.Identifier && queryResultVariables.has(node.argument.name)) {
974
+ context.report({
975
+ node,
976
+ messageId: "objectRestDestructure"
977
+ });
978
+ }
979
+ }
980
+ };
981
+ })
982
+ });
983
+
984
+ // src/rules/no-unstable-deps/no-unstable-deps.rule.ts
985
+ import { AST_NODE_TYPES as AST_NODE_TYPES7, ESLintUtils as ESLintUtils4 } from "@typescript-eslint/utils";
986
+ var name4 = "no-unstable-deps";
987
+ var reactHookNames = ["useEffect", "useCallback", "useMemo"];
988
+ var useQueryHookNames = [
989
+ "useQuery",
990
+ "useSuspenseQuery",
991
+ "useQueries",
992
+ "useSuspenseQueries",
993
+ "useInfiniteQuery",
994
+ "useSuspenseInfiniteQuery"
995
+ ];
996
+ var allHookNames = ["useMutation", ...useQueryHookNames];
997
+ var createRule4 = ESLintUtils4.RuleCreator(getDocsUrl);
998
+ var rule4 = createRule4({
999
+ name: name4,
1000
+ meta: {
1001
+ type: "problem",
1002
+ docs: {
1003
+ description: "Disallow putting the result of query hooks directly in a React hook dependency array",
1004
+ recommended: "error"
1005
+ },
1006
+ messages: {
1007
+ noUnstableDeps: `The result of {{queryHook}} is not referentially stable, so don't pass it directly into the dependencies array of {{reactHook}}. Instead, destructure the return value of {{queryHook}} and pass the destructured values into the dependency array of {{reactHook}}.`
1008
+ },
1009
+ schema: []
1010
+ },
1011
+ defaultOptions: [],
1012
+ create: detectTanstackQueryImports((context, _options, helpers) => {
1013
+ const trackedVariables = {};
1014
+ const hookAliasMap = {};
1015
+ function getReactHook(node) {
1016
+ if (node.callee.type === "Identifier") {
1017
+ const calleeName = node.callee.name;
1018
+ if (reactHookNames.includes(calleeName) || calleeName in hookAliasMap) {
1019
+ return calleeName;
1020
+ }
1021
+ } else if (node.callee.type === "MemberExpression" && node.callee.object.type === "Identifier" && node.callee.object.name === "React" && node.callee.property.type === "Identifier" && reactHookNames.includes(node.callee.property.name)) {
1022
+ return node.callee.property.name;
1023
+ }
1024
+ return void 0;
1025
+ }
1026
+ function collectVariableNames(pattern, queryHook) {
1027
+ if (pattern.type === AST_NODE_TYPES7.Identifier) {
1028
+ trackedVariables[pattern.name] = queryHook;
1029
+ }
1030
+ }
1031
+ function hasCombineProperty(callExpression) {
1032
+ if (callExpression.arguments.length === 0) return false;
1033
+ const firstArg = callExpression.arguments[0];
1034
+ if (!firstArg || firstArg.type !== AST_NODE_TYPES7.ObjectExpression)
1035
+ return false;
1036
+ return firstArg.properties.some(
1037
+ (prop) => prop.type === AST_NODE_TYPES7.Property && prop.key.type === AST_NODE_TYPES7.Identifier && prop.key.name === "combine"
1038
+ );
1039
+ }
1040
+ return {
1041
+ ImportDeclaration(node) {
1042
+ if (node.specifiers.length > 0 && node.importKind === "value" && node.source.value === "React") {
1043
+ node.specifiers.forEach((specifier) => {
1044
+ if (specifier.type === AST_NODE_TYPES7.ImportSpecifier && specifier.imported.type === AST_NODE_TYPES7.Identifier && reactHookNames.includes(specifier.imported.name)) {
1045
+ hookAliasMap[specifier.local.name] = specifier.imported.name;
1046
+ }
1047
+ });
1048
+ }
1049
+ },
1050
+ VariableDeclarator(node) {
1051
+ if (node.init !== null && node.init.type === AST_NODE_TYPES7.CallExpression && node.init.callee.type === AST_NODE_TYPES7.Identifier && allHookNames.includes(node.init.callee.name) && helpers.isTanstackQueryImport(node.init.callee)) {
1052
+ if (node.init.callee.name === "useQueries" && hasCombineProperty(node.init)) {
1053
+ return;
1054
+ }
1055
+ collectVariableNames(node.id, node.init.callee.name);
1056
+ }
1057
+ },
1058
+ CallExpression: (node) => {
1059
+ const reactHook = getReactHook(node);
1060
+ if (reactHook !== void 0 && node.arguments.length > 1 && node.arguments[1]?.type === AST_NODE_TYPES7.ArrayExpression) {
1061
+ const depsArray = node.arguments[1].elements;
1062
+ depsArray.forEach((dep) => {
1063
+ if (dep !== null && dep.type === AST_NODE_TYPES7.Identifier && trackedVariables[dep.name] !== void 0) {
1064
+ const queryHook = trackedVariables[dep.name];
1065
+ context.report({
1066
+ node: dep,
1067
+ messageId: "noUnstableDeps",
1068
+ data: {
1069
+ queryHook,
1070
+ reactHook
1071
+ }
1072
+ });
1073
+ }
1074
+ });
1075
+ }
1076
+ }
1077
+ };
1078
+ })
1079
+ });
1080
+
1081
+ // src/utils/create-property-order-rule.ts
1082
+ import { AST_NODE_TYPES as AST_NODE_TYPES8, ESLintUtils as ESLintUtils5 } from "@typescript-eslint/utils";
1083
+
1084
+ // src/utils/sort-data-by-order.ts
1085
+ function sortDataByOrder(data, orderRules, key) {
1086
+ const getSubsetIndex = (item, subsets) => {
1087
+ for (let i = 0; i < subsets.length; i++) {
1088
+ if (subsets[i]?.includes(item)) {
1089
+ return i;
1090
+ }
1091
+ }
1092
+ return null;
1093
+ };
1094
+ const orderSets = orderRules.reduce(
1095
+ (sets, [A, B]) => [...sets, A, B],
1096
+ []
1097
+ );
1098
+ const inOrderArray = data.filter(
1099
+ (item) => getSubsetIndex(item[key], orderSets) !== null
1100
+ );
1101
+ let wasResorted = false;
1102
+ const sortedArray = inOrderArray.sort((a, b) => {
1103
+ const aKey = a[key], bKey = b[key];
1104
+ const aSubsetIndex = getSubsetIndex(aKey, orderSets);
1105
+ const bSubsetIndex = getSubsetIndex(bKey, orderSets);
1106
+ if (aSubsetIndex !== null && bSubsetIndex !== null && aSubsetIndex !== bSubsetIndex) {
1107
+ return aSubsetIndex - bSubsetIndex;
1108
+ }
1109
+ return 0;
1110
+ });
1111
+ const inOrderIterator = sortedArray.values();
1112
+ const result = data.map((item) => {
1113
+ if (getSubsetIndex(item[key], orderSets) !== null) {
1114
+ const sortedItem = inOrderIterator.next().value;
1115
+ if (sortedItem[key] !== item[key]) {
1116
+ wasResorted = true;
1117
+ }
1118
+ return sortedItem;
1119
+ }
1120
+ return item;
1121
+ });
1122
+ if (!wasResorted) {
1123
+ return null;
1124
+ }
1125
+ return result;
1126
+ }
1127
+
1128
+ // src/utils/create-property-order-rule.ts
1129
+ var createRule5 = ESLintUtils5.RuleCreator(getDocsUrl);
1130
+ function createPropertyOrderRule(options, targetFunctions, orderRules) {
1131
+ const targetFunctionSet = new Set(targetFunctions);
1132
+ function isTargetFunction(node) {
1133
+ return targetFunctionSet.has(node);
1134
+ }
1135
+ return createRule5({
1136
+ ...options,
1137
+ create: detectTanstackQueryImports((context) => {
1138
+ return {
1139
+ CallExpression(node) {
1140
+ if (node.callee.type !== AST_NODE_TYPES8.Identifier) {
1141
+ return;
1142
+ }
1143
+ const functions = node.callee.name;
1144
+ if (!isTargetFunction(functions)) {
1145
+ return;
1146
+ }
1147
+ const argument = node.arguments[0];
1148
+ if (argument === void 0 || argument.type !== "ObjectExpression") {
1149
+ return;
1150
+ }
1151
+ const allProperties = argument.properties;
1152
+ if (allProperties.length < 2) {
1153
+ return;
1154
+ }
1155
+ const properties = allProperties.flatMap((p, index) => {
1156
+ if (p.type === AST_NODE_TYPES8.Property && p.key.type === AST_NODE_TYPES8.Identifier) {
1157
+ return { name: p.key.name, property: p };
1158
+ } else return { name: `_property_${index}`, property: p };
1159
+ });
1160
+ const sortedProperties = sortDataByOrder(
1161
+ properties,
1162
+ orderRules,
1163
+ "name"
1164
+ );
1165
+ if (sortedProperties === null) {
1166
+ return;
1167
+ }
1168
+ context.report({
1169
+ node: argument,
1170
+ data: { function: node.callee.name },
1171
+ messageId: "invalidOrder",
1172
+ fix(fixer) {
1173
+ const sourceCode = context.sourceCode;
1174
+ const reorderedText = sortedProperties.reduce(
1175
+ (sourceText, specifier, index) => {
1176
+ let textBetweenProperties = "";
1177
+ if (index < allProperties.length - 1) {
1178
+ textBetweenProperties = sourceCode.getText().slice(
1179
+ allProperties[index].range[1],
1180
+ allProperties[index + 1].range[0]
1181
+ );
1182
+ }
1183
+ return sourceText + sourceCode.getText(specifier.property) + textBetweenProperties;
1184
+ },
1185
+ ""
1186
+ );
1187
+ return fixer.replaceTextRange(
1188
+ [allProperties[0].range[0], allProperties.at(-1).range[1]],
1189
+ reorderedText
1190
+ );
1191
+ }
1192
+ });
1193
+ }
1194
+ };
1195
+ })
1196
+ });
1197
+ }
1198
+
1199
+ // src/rules/infinite-query-property-order/constants.ts
1200
+ var infiniteQueryFunctions = [
1201
+ "infiniteQueryOptions",
1202
+ "useInfiniteQuery",
1203
+ "useSuspenseInfiniteQuery"
1204
+ ];
1205
+ var sortRules = [
1206
+ [["queryFn"], ["getPreviousPageParam", "getNextPageParam"]]
1207
+ ];
1208
+
1209
+ // src/rules/infinite-query-property-order/infinite-query-property-order.rule.ts
1210
+ var name5 = "infinite-query-property-order";
1211
+ var rule5 = createPropertyOrderRule(
1212
+ {
1213
+ name: name5,
1214
+ meta: {
1215
+ type: "problem",
1216
+ docs: {
1217
+ description: "Ensure correct order of inference sensitive properties for infinite queries",
1218
+ recommended: "error"
1219
+ },
1220
+ messages: {
1221
+ invalidOrder: "Invalid order of properties for `{{function}}`."
1222
+ },
1223
+ schema: [],
1224
+ hasSuggestions: true,
1225
+ fixable: "code"
1226
+ },
1227
+ defaultOptions: []
1228
+ },
1229
+ infiniteQueryFunctions,
1230
+ sortRules
1231
+ );
1232
+
1233
+ // src/rules/no-void-query-fn/no-void-query-fn.rule.ts
1234
+ import { ESLintUtils as ESLintUtils6 } from "@typescript-eslint/utils";
1235
+ var TypeFlags = {
1236
+ Void: 16384,
1237
+ Undefined: 32768
1238
+ };
1239
+ var name6 = "no-void-query-fn";
1240
+ var createRule6 = ESLintUtils6.RuleCreator(getDocsUrl);
1241
+ var rule6 = createRule6({
1242
+ name: name6,
1243
+ meta: {
1244
+ type: "problem",
1245
+ docs: {
1246
+ description: "Ensures queryFn returns a non-undefined value",
1247
+ recommended: "error"
1248
+ },
1249
+ messages: {
1250
+ noVoidReturn: "queryFn must return a non-undefined value"
1251
+ },
1252
+ schema: []
1253
+ },
1254
+ defaultOptions: [],
1255
+ create: detectTanstackQueryImports((context) => {
1256
+ return {
1257
+ Property(node) {
1258
+ if (!ASTUtils.isObjectExpression(node.parent) || !ASTUtils.isIdentifierWithName(node.key, "queryFn")) {
1259
+ return;
1260
+ }
1261
+ const parserServices = context.sourceCode.parserServices;
1262
+ if (!parserServices || !parserServices.esTreeNodeToTSNodeMap || !parserServices.program) {
1263
+ return;
1264
+ }
1265
+ const checker = parserServices.program.getTypeChecker();
1266
+ const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node.value);
1267
+ const type = checker.getTypeAtLocation(tsNode);
1268
+ if (type.getCallSignatures().length > 0) {
1269
+ const returnType = type.getCallSignatures()[0]?.getReturnType();
1270
+ if (!returnType) {
1271
+ return;
1272
+ }
1273
+ if (isIllegalReturn(checker, returnType)) {
1274
+ context.report({
1275
+ node: node.value,
1276
+ messageId: "noVoidReturn"
1277
+ });
1278
+ }
1279
+ }
1280
+ }
1281
+ };
1282
+ })
1283
+ });
1284
+ function isIllegalReturn(checker, type) {
1285
+ const awaited = checker.getAwaitedType(type);
1286
+ if (!awaited) return false;
1287
+ if (awaited.isUnion()) {
1288
+ return awaited.types.some((t) => isIllegalReturn(checker, t));
1289
+ }
1290
+ return awaited.flags & (TypeFlags.Void | TypeFlags.Undefined) ? true : false;
1291
+ }
1292
+
1293
+ // src/rules/mutation-property-order/constants.ts
1294
+ var mutationFunctions = ["useMutation"];
1295
+ var sortRules2 = [[["onMutate"], ["onError", "onSettled"]]];
1296
+
1297
+ // src/rules/mutation-property-order/mutation-property-order.rule.ts
1298
+ var name7 = "mutation-property-order";
1299
+ var rule7 = createPropertyOrderRule(
1300
+ {
1301
+ name: name7,
1302
+ meta: {
1303
+ type: "problem",
1304
+ docs: {
1305
+ description: "Ensure correct order of inference-sensitive properties in useMutation()",
1306
+ recommended: "error"
1307
+ },
1308
+ messages: {
1309
+ invalidOrder: "Invalid order of properties for `{{function}}`."
1310
+ },
1311
+ schema: [],
1312
+ hasSuggestions: true,
1313
+ fixable: "code"
1314
+ },
1315
+ defaultOptions: []
1316
+ },
1317
+ mutationFunctions,
1318
+ sortRules2
1319
+ );
1320
+
1321
+ // src/rules.ts
1322
+ var rules = {
1323
+ [name]: rule,
1324
+ [name2]: rule2,
1325
+ [name3]: rule3,
1326
+ [name4]: rule4,
1327
+ [name5]: rule5,
1328
+ [name6]: rule6,
1329
+ [name7]: rule7
1330
+ };
1331
+
1332
+ export {
1333
+ rules
1334
+ };
1335
+ //# sourceMappingURL=chunk-XH4ABCYA.js.map