@tanstack/eslint-plugin-query 5.72.0 → 5.72.2

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 (62) hide show
  1. package/{dist/index.js → build/legacy/chunk-4ISZM335.js} +20 -51
  2. package/build/legacy/chunk-4ISZM335.js.map +1 -0
  3. package/build/legacy/index.cjs +1013 -0
  4. package/build/legacy/index.cjs.map +1 -0
  5. package/{dist → build/legacy}/index.d.cts +4 -11
  6. package/{dist → build/legacy}/index.d.ts +4 -11
  7. package/build/legacy/index.js +47 -0
  8. package/build/legacy/index.js.map +1 -0
  9. package/build/legacy/rules.cjs +973 -0
  10. package/build/legacy/rules.cjs.map +1 -0
  11. package/build/legacy/rules.d.cts +6 -0
  12. package/build/legacy/rules.d.ts +6 -0
  13. package/build/legacy/rules.js +7 -0
  14. package/build/legacy/rules.js.map +1 -0
  15. package/build/legacy/types.cjs +19 -0
  16. package/build/legacy/types.cjs.map +1 -0
  17. package/build/legacy/types.d.cts +5 -0
  18. package/build/legacy/types.d.ts +5 -0
  19. package/build/legacy/types.js +1 -0
  20. package/build/legacy/types.js.map +1 -0
  21. package/{dist/rules.js → build/modern/chunk-6ZHBB3IQ.js} +2 -1
  22. package/build/modern/chunk-6ZHBB3IQ.js.map +1 -0
  23. package/{dist → build/modern}/index.cjs +10 -5
  24. package/build/modern/index.cjs.map +1 -0
  25. package/build/modern/index.d.cts +17 -0
  26. package/build/modern/index.d.ts +17 -0
  27. package/build/modern/index.js +47 -0
  28. package/build/modern/index.js.map +1 -0
  29. package/build/modern/rules.cjs.map +1 -0
  30. package/build/modern/rules.d.cts +6 -0
  31. package/build/modern/rules.d.ts +6 -0
  32. package/build/modern/rules.js +7 -0
  33. package/build/modern/rules.js.map +1 -0
  34. package/build/modern/types.cjs +19 -0
  35. package/build/modern/types.cjs.map +1 -0
  36. package/build/modern/types.d.cts +5 -0
  37. package/build/modern/types.d.ts +5 -0
  38. package/build/modern/types.js +1 -0
  39. package/build/modern/types.js.map +1 -0
  40. package/package.json +12 -10
  41. package/src/index.ts +54 -0
  42. package/src/rules/exhaustive-deps/exhaustive-deps.rule.ts +184 -0
  43. package/src/rules/exhaustive-deps/exhaustive-deps.utils.ts +41 -0
  44. package/src/rules/infinite-query-property-order/constants.ts +17 -0
  45. package/src/rules/infinite-query-property-order/infinite-query-property-order.rule.ts +107 -0
  46. package/src/rules/infinite-query-property-order/infinite-query-property-order.utils.ts +67 -0
  47. package/src/rules/no-rest-destructuring/no-rest-destructuring.rule.ts +115 -0
  48. package/src/rules/no-rest-destructuring/no-rest-destructuring.utils.ts +11 -0
  49. package/src/rules/no-unstable-deps/no-unstable-deps.rule.ts +129 -0
  50. package/src/rules/no-void-query-fn/no-void-query-fn.rule.ts +84 -0
  51. package/src/rules/stable-query-client/stable-query-client.rule.ts +88 -0
  52. package/src/rules.ts +25 -0
  53. package/src/types.ts +3 -0
  54. package/src/utils/ast-utils.ts +391 -0
  55. package/src/utils/detect-react-query-imports.ts +97 -0
  56. package/src/utils/get-docs-url.ts +2 -0
  57. package/src/utils/unique-by.ts +3 -0
  58. package/dist/index.cjs.map +0 -1
  59. package/dist/index.js.map +0 -1
  60. package/dist/rules.cjs.map +0 -1
  61. package/dist/rules.js.map +0 -1
  62. /package/{dist → build/modern}/rules.cjs +0 -0
@@ -0,0 +1,973 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name7 in all)
10
+ __defProp(target, name7, { get: all[name7], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/rules.ts
31
+ var rules_exports = {};
32
+ __export(rules_exports, {
33
+ rules: () => rules
34
+ });
35
+ module.exports = __toCommonJS(rules_exports);
36
+
37
+ // src/rules/exhaustive-deps/exhaustive-deps.rule.ts
38
+ var import_utils4 = require("@typescript-eslint/utils");
39
+
40
+ // src/utils/ast-utils.ts
41
+ var import_utils = require("@typescript-eslint/utils");
42
+
43
+ // src/utils/unique-by.ts
44
+ function uniqueBy(arr, fn) {
45
+ return arr.filter((x, i, a) => a.findIndex((y) => fn(x) === fn(y)) === i);
46
+ }
47
+
48
+ // src/utils/ast-utils.ts
49
+ var ASTUtils = {
50
+ isNodeOfOneOf(node, types) {
51
+ return types.includes(node.type);
52
+ },
53
+ isIdentifier(node) {
54
+ return node.type === import_utils.AST_NODE_TYPES.Identifier;
55
+ },
56
+ isIdentifierWithName(node, name7) {
57
+ return ASTUtils.isIdentifier(node) && node.name === name7;
58
+ },
59
+ isIdentifierWithOneOfNames(node, name7) {
60
+ return ASTUtils.isIdentifier(node) && name7.includes(node.name);
61
+ },
62
+ isProperty(node) {
63
+ return node.type === import_utils.AST_NODE_TYPES.Property;
64
+ },
65
+ isObjectExpression(node) {
66
+ return node.type === import_utils.AST_NODE_TYPES.ObjectExpression;
67
+ },
68
+ isPropertyWithIdentifierKey(node, key) {
69
+ return ASTUtils.isProperty(node) && ASTUtils.isIdentifierWithName(node.key, key);
70
+ },
71
+ findPropertyWithIdentifierKey(properties, key) {
72
+ return properties.find(
73
+ (x) => ASTUtils.isPropertyWithIdentifierKey(x, key)
74
+ );
75
+ },
76
+ getNestedIdentifiers(node) {
77
+ const identifiers = [];
78
+ if (ASTUtils.isIdentifier(node)) {
79
+ identifiers.push(node);
80
+ }
81
+ if ("arguments" in node) {
82
+ node.arguments.forEach((x) => {
83
+ identifiers.push(...ASTUtils.getNestedIdentifiers(x));
84
+ });
85
+ }
86
+ if ("elements" in node) {
87
+ node.elements.forEach((x) => {
88
+ if (x !== null) {
89
+ identifiers.push(...ASTUtils.getNestedIdentifiers(x));
90
+ }
91
+ });
92
+ }
93
+ if ("properties" in node) {
94
+ node.properties.forEach((x) => {
95
+ identifiers.push(...ASTUtils.getNestedIdentifiers(x));
96
+ });
97
+ }
98
+ if ("expressions" in node) {
99
+ node.expressions.forEach((x) => {
100
+ identifiers.push(...ASTUtils.getNestedIdentifiers(x));
101
+ });
102
+ }
103
+ if ("left" in node) {
104
+ identifiers.push(...ASTUtils.getNestedIdentifiers(node.left));
105
+ }
106
+ if ("right" in node) {
107
+ identifiers.push(...ASTUtils.getNestedIdentifiers(node.right));
108
+ }
109
+ if (node.type === import_utils.AST_NODE_TYPES.Property) {
110
+ identifiers.push(...ASTUtils.getNestedIdentifiers(node.value));
111
+ }
112
+ if (node.type === import_utils.AST_NODE_TYPES.SpreadElement) {
113
+ identifiers.push(...ASTUtils.getNestedIdentifiers(node.argument));
114
+ }
115
+ if (node.type === import_utils.AST_NODE_TYPES.MemberExpression) {
116
+ identifiers.push(...ASTUtils.getNestedIdentifiers(node.object));
117
+ }
118
+ if (node.type === import_utils.AST_NODE_TYPES.UnaryExpression) {
119
+ identifiers.push(...ASTUtils.getNestedIdentifiers(node.argument));
120
+ }
121
+ if (node.type === import_utils.AST_NODE_TYPES.ChainExpression) {
122
+ identifiers.push(...ASTUtils.getNestedIdentifiers(node.expression));
123
+ }
124
+ if (node.type === import_utils.AST_NODE_TYPES.TSNonNullExpression) {
125
+ identifiers.push(...ASTUtils.getNestedIdentifiers(node.expression));
126
+ }
127
+ if (node.type === import_utils.AST_NODE_TYPES.ArrowFunctionExpression) {
128
+ identifiers.push(...ASTUtils.getNestedIdentifiers(node.body));
129
+ }
130
+ if (node.type === import_utils.AST_NODE_TYPES.FunctionExpression) {
131
+ identifiers.push(...ASTUtils.getNestedIdentifiers(node.body));
132
+ }
133
+ if (node.type === import_utils.AST_NODE_TYPES.BlockStatement) {
134
+ identifiers.push(
135
+ ...node.body.map((body) => ASTUtils.getNestedIdentifiers(body)).flat()
136
+ );
137
+ }
138
+ if (node.type === import_utils.AST_NODE_TYPES.ReturnStatement && node.argument) {
139
+ identifiers.push(...ASTUtils.getNestedIdentifiers(node.argument));
140
+ }
141
+ return identifiers;
142
+ },
143
+ isAncestorIsCallee(identifier) {
144
+ let previousNode = identifier;
145
+ let currentNode = identifier.parent;
146
+ while (currentNode !== void 0) {
147
+ if (currentNode.type === import_utils.AST_NODE_TYPES.CallExpression && currentNode.callee === previousNode) {
148
+ return true;
149
+ }
150
+ if (currentNode.type !== import_utils.AST_NODE_TYPES.MemberExpression) {
151
+ return false;
152
+ }
153
+ previousNode = currentNode;
154
+ currentNode = currentNode.parent;
155
+ }
156
+ return false;
157
+ },
158
+ traverseUpOnly(identifier, allowedNodeTypes) {
159
+ const parent = identifier.parent;
160
+ if (parent !== void 0 && allowedNodeTypes.includes(parent.type)) {
161
+ return ASTUtils.traverseUpOnly(parent, allowedNodeTypes);
162
+ }
163
+ return identifier;
164
+ },
165
+ isDeclaredInNode(params) {
166
+ const { functionNode, reference, scopeManager } = params;
167
+ const scope = scopeManager.acquire(functionNode);
168
+ if (scope === null) {
169
+ return false;
170
+ }
171
+ return scope.set.has(reference.identifier.name);
172
+ },
173
+ getExternalRefs(params) {
174
+ const { scopeManager, sourceCode, node } = params;
175
+ const scope = scopeManager.acquire(node);
176
+ if (scope === null) {
177
+ return [];
178
+ }
179
+ const references = scope.references.filter((x) => x.isRead() && !scope.set.has(x.identifier.name)).map((x) => {
180
+ const referenceNode = ASTUtils.traverseUpOnly(x.identifier, [
181
+ import_utils.AST_NODE_TYPES.MemberExpression,
182
+ import_utils.AST_NODE_TYPES.Identifier
183
+ ]);
184
+ return {
185
+ variable: x,
186
+ node: referenceNode,
187
+ text: sourceCode.getText(referenceNode)
188
+ };
189
+ });
190
+ const localRefIds = new Set(
191
+ [...scope.set.values()].map((x) => sourceCode.getText(x.identifiers[0]))
192
+ );
193
+ const externalRefs = references.filter(
194
+ (x) => x.variable.resolved === null || !localRefIds.has(x.text)
195
+ );
196
+ return uniqueBy(externalRefs, (x) => x.text).map((x) => x.variable);
197
+ },
198
+ mapKeyNodeToText(node, sourceCode) {
199
+ return sourceCode.getText(
200
+ ASTUtils.traverseUpOnly(node, [
201
+ import_utils.AST_NODE_TYPES.MemberExpression,
202
+ import_utils.AST_NODE_TYPES.TSNonNullExpression,
203
+ import_utils.AST_NODE_TYPES.Identifier
204
+ ])
205
+ );
206
+ },
207
+ mapKeyNodeToBaseText(node, sourceCode) {
208
+ return ASTUtils.mapKeyNodeToText(node, sourceCode).replace(
209
+ /(?:\?(\.)|!)/g,
210
+ "$1"
211
+ );
212
+ },
213
+ isValidReactComponentOrHookName(identifier) {
214
+ return identifier !== null && identifier !== void 0 && /^(use|[A-Z])/.test(identifier.name);
215
+ },
216
+ getFunctionAncestor(sourceCode, node) {
217
+ var _a;
218
+ for (const ancestor of sourceCode.getAncestors(node)) {
219
+ if (ASTUtils.isNodeOfOneOf(ancestor, [
220
+ import_utils.AST_NODE_TYPES.FunctionDeclaration,
221
+ import_utils.AST_NODE_TYPES.FunctionExpression,
222
+ import_utils.AST_NODE_TYPES.ArrowFunctionExpression
223
+ ])) {
224
+ return ancestor;
225
+ }
226
+ if (((_a = ancestor.parent) == null ? void 0 : _a.type) === import_utils.AST_NODE_TYPES.VariableDeclarator && ancestor.parent.id.type === import_utils.AST_NODE_TYPES.Identifier && ASTUtils.isNodeOfOneOf(ancestor, [
227
+ import_utils.AST_NODE_TYPES.FunctionDeclaration,
228
+ import_utils.AST_NODE_TYPES.FunctionExpression,
229
+ import_utils.AST_NODE_TYPES.ArrowFunctionExpression
230
+ ])) {
231
+ return ancestor;
232
+ }
233
+ }
234
+ return void 0;
235
+ },
236
+ getReferencedExpressionByIdentifier(params) {
237
+ var _a, _b, _c;
238
+ const { node, context } = params;
239
+ const sourceCode = context.sourceCode ?? context.getSourceCode();
240
+ const scope = context.sourceCode.getScope(node) ? sourceCode.getScope(node) : context.getScope();
241
+ const resolvedNode = (_c = (_b = (_a = scope.references.find((ref) => ref.identifier === node)) == null ? void 0 : _a.resolved) == null ? void 0 : _b.defs[0]) == null ? void 0 : _c.node;
242
+ if ((resolvedNode == null ? void 0 : resolvedNode.type) !== import_utils.AST_NODE_TYPES.VariableDeclarator) {
243
+ return null;
244
+ }
245
+ return resolvedNode.init;
246
+ },
247
+ getClosestVariableDeclarator(node) {
248
+ let currentNode = node;
249
+ while (currentNode.type !== import_utils.AST_NODE_TYPES.Program) {
250
+ if (currentNode.type === import_utils.AST_NODE_TYPES.VariableDeclarator) {
251
+ return currentNode;
252
+ }
253
+ currentNode = currentNode.parent;
254
+ }
255
+ return void 0;
256
+ },
257
+ getNestedReturnStatements(node) {
258
+ const returnStatements = [];
259
+ if (node.type === import_utils.AST_NODE_TYPES.ReturnStatement) {
260
+ returnStatements.push(node);
261
+ }
262
+ if ("body" in node && node.body !== void 0 && node.body !== null) {
263
+ Array.isArray(node.body) ? node.body.forEach((x) => {
264
+ returnStatements.push(...ASTUtils.getNestedReturnStatements(x));
265
+ }) : returnStatements.push(
266
+ ...ASTUtils.getNestedReturnStatements(node.body)
267
+ );
268
+ }
269
+ if ("consequent" in node) {
270
+ Array.isArray(node.consequent) ? node.consequent.forEach((x) => {
271
+ returnStatements.push(...ASTUtils.getNestedReturnStatements(x));
272
+ }) : returnStatements.push(
273
+ ...ASTUtils.getNestedReturnStatements(node.consequent)
274
+ );
275
+ }
276
+ if ("alternate" in node && node.alternate !== null) {
277
+ Array.isArray(node.alternate) ? node.alternate.forEach((x) => {
278
+ returnStatements.push(...ASTUtils.getNestedReturnStatements(x));
279
+ }) : returnStatements.push(
280
+ ...ASTUtils.getNestedReturnStatements(node.alternate)
281
+ );
282
+ }
283
+ if ("cases" in node) {
284
+ node.cases.forEach((x) => {
285
+ returnStatements.push(...ASTUtils.getNestedReturnStatements(x));
286
+ });
287
+ }
288
+ if ("block" in node) {
289
+ returnStatements.push(...ASTUtils.getNestedReturnStatements(node.block));
290
+ }
291
+ if ("handler" in node && node.handler !== null) {
292
+ returnStatements.push(...ASTUtils.getNestedReturnStatements(node.handler));
293
+ }
294
+ if ("finalizer" in node && node.finalizer !== null) {
295
+ returnStatements.push(
296
+ ...ASTUtils.getNestedReturnStatements(node.finalizer)
297
+ );
298
+ }
299
+ if ("expression" in node && node.expression !== true && node.expression !== false) {
300
+ returnStatements.push(
301
+ ...ASTUtils.getNestedReturnStatements(node.expression)
302
+ );
303
+ }
304
+ if ("test" in node && node.test !== null) {
305
+ returnStatements.push(...ASTUtils.getNestedReturnStatements(node.test));
306
+ }
307
+ return returnStatements;
308
+ }
309
+ };
310
+
311
+ // src/utils/get-docs-url.ts
312
+ var getDocsUrl = (ruleName) => `https://tanstack.com/query/latest/docs/eslint/${ruleName}`;
313
+
314
+ // src/utils/detect-react-query-imports.ts
315
+ var import_utils2 = require("@typescript-eslint/utils");
316
+ function detectTanstackQueryImports(create) {
317
+ return (context, optionsWithDefault) => {
318
+ const tanstackQueryImportSpecifiers = [];
319
+ const helpers = {
320
+ isSpecificTanstackQueryImport(node, source) {
321
+ return !!tanstackQueryImportSpecifiers.find((specifier) => {
322
+ if (specifier.type === import_utils2.TSESTree.AST_NODE_TYPES.ImportSpecifier && specifier.parent.type === import_utils2.TSESTree.AST_NODE_TYPES.ImportDeclaration && specifier.parent.source.value === source) {
323
+ return node.name === specifier.local.name;
324
+ }
325
+ return false;
326
+ });
327
+ },
328
+ isTanstackQueryImport(node) {
329
+ return !!tanstackQueryImportSpecifiers.find((specifier) => {
330
+ if (specifier.type === import_utils2.TSESTree.AST_NODE_TYPES.ImportSpecifier) {
331
+ return node.name === specifier.local.name;
332
+ }
333
+ return false;
334
+ });
335
+ }
336
+ };
337
+ const detectionInstructions = {
338
+ ImportDeclaration(node) {
339
+ if (node.specifiers.length > 0 && // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
340
+ (node.importKind === "value" || node.importKind === void 0) && node.source.value.startsWith("@tanstack/") && node.source.value.endsWith("-query")) {
341
+ tanstackQueryImportSpecifiers.push(...node.specifiers);
342
+ }
343
+ }
344
+ };
345
+ const ruleInstructions = create(context, optionsWithDefault, helpers);
346
+ const enhancedRuleInstructions = {};
347
+ const allKeys = new Set(
348
+ Object.keys(detectionInstructions).concat(Object.keys(ruleInstructions))
349
+ );
350
+ allKeys.forEach((instruction) => {
351
+ enhancedRuleInstructions[instruction] = (node) => {
352
+ var _a;
353
+ if (instruction in detectionInstructions) {
354
+ (_a = detectionInstructions[instruction]) == null ? void 0 : _a.call(detectionInstructions, node);
355
+ }
356
+ const ruleInstruction = ruleInstructions[instruction];
357
+ if (ruleInstruction) {
358
+ return ruleInstruction(node);
359
+ }
360
+ return void 0;
361
+ };
362
+ });
363
+ return enhancedRuleInstructions;
364
+ };
365
+ }
366
+
367
+ // src/rules/exhaustive-deps/exhaustive-deps.utils.ts
368
+ var import_utils3 = require("@typescript-eslint/utils");
369
+ var ExhaustiveDepsUtils = {
370
+ isRelevantReference(params) {
371
+ const { sourceCode, reference, scopeManager, node } = params;
372
+ const component = ASTUtils.getFunctionAncestor(sourceCode, node);
373
+ if (component === void 0) {
374
+ return false;
375
+ }
376
+ if (!ASTUtils.isDeclaredInNode({
377
+ scopeManager,
378
+ reference,
379
+ functionNode: component
380
+ })) {
381
+ return false;
382
+ }
383
+ return reference.identifier.name !== "undefined" && reference.identifier.parent.type !== import_utils3.AST_NODE_TYPES.NewExpression && !ExhaustiveDepsUtils.isInstanceOfKind(reference.identifier.parent);
384
+ },
385
+ isInstanceOfKind(node) {
386
+ return node.type === import_utils3.AST_NODE_TYPES.BinaryExpression && node.operator === "instanceof";
387
+ }
388
+ };
389
+
390
+ // src/rules/exhaustive-deps/exhaustive-deps.rule.ts
391
+ var QUERY_KEY = "queryKey";
392
+ var QUERY_FN = "queryFn";
393
+ var name = "exhaustive-deps";
394
+ var createRule = import_utils4.ESLintUtils.RuleCreator(getDocsUrl);
395
+ var rule = createRule({
396
+ name,
397
+ meta: {
398
+ type: "problem",
399
+ docs: {
400
+ description: "Exhaustive deps rule for useQuery",
401
+ recommended: "error"
402
+ },
403
+ messages: {
404
+ missingDeps: `The following dependencies are missing in your queryKey: {{deps}}`,
405
+ fixTo: "Fix to {{result}}"
406
+ },
407
+ hasSuggestions: true,
408
+ fixable: "code",
409
+ schema: []
410
+ },
411
+ defaultOptions: [],
412
+ create: detectTanstackQueryImports((context) => {
413
+ return {
414
+ Property: (node) => {
415
+ if (!ASTUtils.isObjectExpression(node.parent) || !ASTUtils.isIdentifierWithName(node.key, QUERY_KEY)) {
416
+ return;
417
+ }
418
+ const scopeManager = context.sourceCode.scopeManager;
419
+ const queryKey = ASTUtils.findPropertyWithIdentifierKey(
420
+ node.parent.properties,
421
+ QUERY_KEY
422
+ );
423
+ const queryFn = ASTUtils.findPropertyWithIdentifierKey(
424
+ node.parent.properties,
425
+ QUERY_FN
426
+ );
427
+ if (scopeManager === null || queryKey === void 0 || queryFn === void 0 || !ASTUtils.isNodeOfOneOf(queryFn.value, [
428
+ import_utils4.AST_NODE_TYPES.ArrowFunctionExpression,
429
+ import_utils4.AST_NODE_TYPES.FunctionExpression,
430
+ import_utils4.AST_NODE_TYPES.ConditionalExpression
431
+ ])) {
432
+ return;
433
+ }
434
+ let queryKeyNode = queryKey.value;
435
+ if (queryKeyNode.type === import_utils4.AST_NODE_TYPES.TSAsExpression && queryKeyNode.expression.type === import_utils4.AST_NODE_TYPES.ArrayExpression) {
436
+ queryKeyNode = queryKeyNode.expression;
437
+ }
438
+ if (queryKeyNode.type === import_utils4.AST_NODE_TYPES.Identifier) {
439
+ const expression = ASTUtils.getReferencedExpressionByIdentifier({
440
+ context,
441
+ node: queryKeyNode
442
+ });
443
+ if ((expression == null ? void 0 : expression.type) === import_utils4.AST_NODE_TYPES.ArrayExpression) {
444
+ queryKeyNode = expression;
445
+ }
446
+ }
447
+ const externalRefs = ASTUtils.getExternalRefs({
448
+ scopeManager,
449
+ sourceCode: context.sourceCode,
450
+ node: getQueryFnRelevantNode(queryFn)
451
+ });
452
+ const relevantRefs = externalRefs.filter(
453
+ (reference) => ExhaustiveDepsUtils.isRelevantReference({
454
+ sourceCode: context.sourceCode,
455
+ reference,
456
+ scopeManager,
457
+ node: getQueryFnRelevantNode(queryFn)
458
+ })
459
+ );
460
+ const existingKeys = ASTUtils.getNestedIdentifiers(queryKeyNode).map(
461
+ (identifier) => ASTUtils.mapKeyNodeToBaseText(identifier, context.sourceCode)
462
+ );
463
+ const missingRefs = relevantRefs.map((ref) => ({
464
+ ref,
465
+ text: ASTUtils.mapKeyNodeToBaseText(
466
+ ref.identifier,
467
+ context.sourceCode
468
+ )
469
+ })).filter(({ ref, text }) => {
470
+ return !ref.isTypeReference && !ASTUtils.isAncestorIsCallee(ref.identifier) && !existingKeys.some((existingKey) => existingKey === text) && !existingKeys.includes(text.split(/[?.]/)[0] ?? "");
471
+ }).map(({ ref, text }) => ({
472
+ identifier: ref.identifier,
473
+ text
474
+ }));
475
+ const uniqueMissingRefs = uniqueBy(missingRefs, (x) => x.text);
476
+ if (uniqueMissingRefs.length > 0) {
477
+ const missingAsText = uniqueMissingRefs.map(
478
+ (ref) => ASTUtils.mapKeyNodeToText(ref.identifier, context.sourceCode)
479
+ ).join(", ");
480
+ const queryKeyValue = context.sourceCode.getText(queryKeyNode);
481
+ const existingWithMissing = queryKeyValue === "[]" ? `[${missingAsText}]` : queryKeyValue.replace(/\]$/, `, ${missingAsText}]`);
482
+ const suggestions = [];
483
+ if (queryKeyNode.type === import_utils4.AST_NODE_TYPES.ArrayExpression) {
484
+ suggestions.push({
485
+ messageId: "fixTo",
486
+ data: { result: existingWithMissing },
487
+ fix(fixer) {
488
+ return fixer.replaceText(queryKeyNode, existingWithMissing);
489
+ }
490
+ });
491
+ }
492
+ context.report({
493
+ node,
494
+ messageId: "missingDeps",
495
+ data: {
496
+ deps: uniqueMissingRefs.map((ref) => ref.text).join(", ")
497
+ },
498
+ suggest: suggestions
499
+ });
500
+ }
501
+ }
502
+ };
503
+ })
504
+ });
505
+ function getQueryFnRelevantNode(queryFn) {
506
+ if (queryFn.value.type !== import_utils4.AST_NODE_TYPES.ConditionalExpression) {
507
+ return queryFn.value;
508
+ }
509
+ if (queryFn.value.consequent.type === import_utils4.AST_NODE_TYPES.Identifier && queryFn.value.consequent.name === "skipToken") {
510
+ return queryFn.value.alternate;
511
+ }
512
+ return queryFn.value.consequent;
513
+ }
514
+
515
+ // src/rules/stable-query-client/stable-query-client.rule.ts
516
+ var import_utils5 = require("@typescript-eslint/utils");
517
+ var name2 = "stable-query-client";
518
+ var createRule2 = import_utils5.ESLintUtils.RuleCreator(getDocsUrl);
519
+ var rule2 = createRule2({
520
+ name: name2,
521
+ meta: {
522
+ type: "problem",
523
+ docs: {
524
+ description: "Makes sure that QueryClient is stable",
525
+ recommended: "error"
526
+ },
527
+ messages: {
528
+ unstable: [
529
+ "QueryClient is not stable. It should be either extracted from the component or wrapped in React.useState.",
530
+ "See https://tkdodo.eu/blog/react-query-fa-qs#2-the-queryclient-is-not-stable"
531
+ ].join("\n"),
532
+ fixTo: "Fix to {{result}}"
533
+ },
534
+ hasSuggestions: true,
535
+ fixable: "code",
536
+ schema: []
537
+ },
538
+ defaultOptions: [],
539
+ create: detectTanstackQueryImports((context, _, helpers) => {
540
+ return {
541
+ NewExpression: (node) => {
542
+ if (node.callee.type !== import_utils5.AST_NODE_TYPES.Identifier || node.callee.name !== "QueryClient" || node.parent.type !== import_utils5.AST_NODE_TYPES.VariableDeclarator || !helpers.isSpecificTanstackQueryImport(
543
+ node.callee,
544
+ "@tanstack/react-query"
545
+ )) {
546
+ return;
547
+ }
548
+ const fnAncestor = ASTUtils.getFunctionAncestor(
549
+ context.sourceCode,
550
+ node
551
+ );
552
+ const isReactServerComponent = (fnAncestor == null ? void 0 : fnAncestor.async) === true;
553
+ if (!ASTUtils.isValidReactComponentOrHookName(fnAncestor == null ? void 0 : fnAncestor.id) || isReactServerComponent) {
554
+ return;
555
+ }
556
+ context.report({
557
+ node: node.parent,
558
+ messageId: "unstable",
559
+ fix: (() => {
560
+ const { parent } = node;
561
+ if (parent.id.type !== import_utils5.AST_NODE_TYPES.Identifier) {
562
+ return;
563
+ }
564
+ const sourceCode = context.sourceCode ?? context.getSourceCode();
565
+ const nodeText = sourceCode.getText(node);
566
+ const variableName = parent.id.name;
567
+ return (fixer) => {
568
+ return fixer.replaceTextRange(
569
+ [parent.range[0], parent.range[1]],
570
+ `[${variableName}] = React.useState(() => ${nodeText})`
571
+ );
572
+ };
573
+ })()
574
+ });
575
+ }
576
+ };
577
+ })
578
+ });
579
+
580
+ // src/rules/no-rest-destructuring/no-rest-destructuring.rule.ts
581
+ var import_utils7 = require("@typescript-eslint/utils");
582
+
583
+ // src/rules/no-rest-destructuring/no-rest-destructuring.utils.ts
584
+ var import_utils6 = require("@typescript-eslint/utils");
585
+ var NoRestDestructuringUtils = {
586
+ isObjectRestDestructuring(node) {
587
+ if (node.type !== import_utils6.AST_NODE_TYPES.ObjectPattern) {
588
+ return false;
589
+ }
590
+ return node.properties.some((p) => p.type === import_utils6.AST_NODE_TYPES.RestElement);
591
+ }
592
+ };
593
+
594
+ // src/rules/no-rest-destructuring/no-rest-destructuring.rule.ts
595
+ var name3 = "no-rest-destructuring";
596
+ var queryHooks = [
597
+ "useQuery",
598
+ "useQueries",
599
+ "useInfiniteQuery",
600
+ "useSuspenseQuery",
601
+ "useSuspenseQueries",
602
+ "useSuspenseInfiniteQuery"
603
+ ];
604
+ var createRule3 = import_utils7.ESLintUtils.RuleCreator(getDocsUrl);
605
+ var rule3 = createRule3({
606
+ name: name3,
607
+ meta: {
608
+ type: "problem",
609
+ docs: {
610
+ description: "Disallows rest destructuring in queries",
611
+ recommended: "warn"
612
+ },
613
+ messages: {
614
+ objectRestDestructure: `Object rest destructuring on a query will observe all changes to the query, leading to excessive re-renders.`
615
+ },
616
+ schema: []
617
+ },
618
+ defaultOptions: [],
619
+ create: detectTanstackQueryImports((context, _, helpers) => {
620
+ const queryResultVariables = /* @__PURE__ */ new Set();
621
+ return {
622
+ CallExpression: (node) => {
623
+ if (!ASTUtils.isIdentifierWithOneOfNames(node.callee, queryHooks) || node.parent.type !== import_utils7.AST_NODE_TYPES.VariableDeclarator || !helpers.isTanstackQueryImport(node.callee)) {
624
+ return;
625
+ }
626
+ const returnValue = node.parent.id;
627
+ if (node.callee.name !== "useQueries" && node.callee.name !== "useSuspenseQueries") {
628
+ if (NoRestDestructuringUtils.isObjectRestDestructuring(returnValue)) {
629
+ return context.report({
630
+ node: node.parent,
631
+ messageId: "objectRestDestructure"
632
+ });
633
+ }
634
+ if (returnValue.type === import_utils7.AST_NODE_TYPES.Identifier) {
635
+ queryResultVariables.add(returnValue.name);
636
+ }
637
+ return;
638
+ }
639
+ if (returnValue.type !== import_utils7.AST_NODE_TYPES.ArrayPattern) {
640
+ if (returnValue.type === import_utils7.AST_NODE_TYPES.Identifier) {
641
+ queryResultVariables.add(returnValue.name);
642
+ }
643
+ return;
644
+ }
645
+ returnValue.elements.forEach((queryResult) => {
646
+ if (queryResult === null) {
647
+ return;
648
+ }
649
+ if (NoRestDestructuringUtils.isObjectRestDestructuring(queryResult)) {
650
+ context.report({
651
+ node: queryResult,
652
+ messageId: "objectRestDestructure"
653
+ });
654
+ }
655
+ });
656
+ },
657
+ VariableDeclarator: (node) => {
658
+ var _a;
659
+ if (((_a = node.init) == null ? void 0 : _a.type) === import_utils7.AST_NODE_TYPES.Identifier && queryResultVariables.has(node.init.name) && NoRestDestructuringUtils.isObjectRestDestructuring(node.id)) {
660
+ context.report({
661
+ node,
662
+ messageId: "objectRestDestructure"
663
+ });
664
+ }
665
+ },
666
+ SpreadElement: (node) => {
667
+ if (node.argument.type === import_utils7.AST_NODE_TYPES.Identifier && queryResultVariables.has(node.argument.name)) {
668
+ context.report({
669
+ node,
670
+ messageId: "objectRestDestructure"
671
+ });
672
+ }
673
+ }
674
+ };
675
+ })
676
+ });
677
+
678
+ // src/rules/no-unstable-deps/no-unstable-deps.rule.ts
679
+ var import_utils8 = require("@typescript-eslint/utils");
680
+ var name4 = "no-unstable-deps";
681
+ var reactHookNames = ["useEffect", "useCallback", "useMemo"];
682
+ var useQueryHookNames = [
683
+ "useQuery",
684
+ "useSuspenseQuery",
685
+ "useQueries",
686
+ "useSuspenseQueries",
687
+ "useInfiniteQuery",
688
+ "useSuspenseInfiniteQuery"
689
+ ];
690
+ var allHookNames = ["useMutation", ...useQueryHookNames];
691
+ var createRule4 = import_utils8.ESLintUtils.RuleCreator(getDocsUrl);
692
+ var rule4 = createRule4({
693
+ name: name4,
694
+ meta: {
695
+ type: "problem",
696
+ docs: {
697
+ description: "Disallow putting the result of query hooks directly in a React hook dependency array",
698
+ recommended: "error"
699
+ },
700
+ messages: {
701
+ 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}}.`
702
+ },
703
+ schema: []
704
+ },
705
+ defaultOptions: [],
706
+ create: detectTanstackQueryImports((context) => {
707
+ const trackedVariables = {};
708
+ const hookAliasMap = {};
709
+ function getReactHook(node) {
710
+ if (node.callee.type === "Identifier") {
711
+ const calleeName = node.callee.name;
712
+ if (reactHookNames.includes(calleeName) || calleeName in hookAliasMap) {
713
+ return calleeName;
714
+ }
715
+ } 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)) {
716
+ return node.callee.property.name;
717
+ }
718
+ return void 0;
719
+ }
720
+ function collectVariableNames(pattern, queryHook) {
721
+ if (pattern.type === import_utils8.AST_NODE_TYPES.Identifier) {
722
+ trackedVariables[pattern.name] = queryHook;
723
+ }
724
+ }
725
+ return {
726
+ ImportDeclaration(node) {
727
+ if (node.specifiers.length > 0 && node.importKind === "value" && node.source.value === "React") {
728
+ node.specifiers.forEach((specifier) => {
729
+ if (specifier.type === import_utils8.AST_NODE_TYPES.ImportSpecifier && specifier.imported.type === import_utils8.AST_NODE_TYPES.Identifier && reactHookNames.includes(specifier.imported.name)) {
730
+ hookAliasMap[specifier.local.name] = specifier.imported.name;
731
+ }
732
+ });
733
+ }
734
+ },
735
+ VariableDeclarator(node) {
736
+ if (node.init !== null && node.init.type === import_utils8.AST_NODE_TYPES.CallExpression && node.init.callee.type === import_utils8.AST_NODE_TYPES.Identifier && allHookNames.includes(node.init.callee.name)) {
737
+ collectVariableNames(node.id, node.init.callee.name);
738
+ }
739
+ },
740
+ CallExpression: (node) => {
741
+ var _a;
742
+ const reactHook = getReactHook(node);
743
+ if (reactHook !== void 0 && node.arguments.length > 1 && ((_a = node.arguments[1]) == null ? void 0 : _a.type) === import_utils8.AST_NODE_TYPES.ArrayExpression) {
744
+ const depsArray = node.arguments[1].elements;
745
+ depsArray.forEach((dep) => {
746
+ if (dep !== null && dep.type === import_utils8.AST_NODE_TYPES.Identifier && trackedVariables[dep.name] !== void 0) {
747
+ const queryHook = trackedVariables[dep.name];
748
+ context.report({
749
+ node: dep,
750
+ messageId: "noUnstableDeps",
751
+ data: {
752
+ queryHook,
753
+ reactHook
754
+ }
755
+ });
756
+ }
757
+ });
758
+ }
759
+ }
760
+ };
761
+ })
762
+ });
763
+
764
+ // src/rules/infinite-query-property-order/infinite-query-property-order.rule.ts
765
+ var import_utils9 = require("@typescript-eslint/utils");
766
+
767
+ // src/rules/infinite-query-property-order/infinite-query-property-order.utils.ts
768
+ function sortDataByOrder(data, orderRules, key) {
769
+ const getSubsetIndex = (item, subsets) => {
770
+ var _a;
771
+ for (let i = 0; i < subsets.length; i++) {
772
+ if ((_a = subsets[i]) == null ? void 0 : _a.includes(item)) {
773
+ return i;
774
+ }
775
+ }
776
+ return null;
777
+ };
778
+ const orderSets = orderRules.reduce(
779
+ (sets, [A, B]) => [...sets, A, B],
780
+ []
781
+ );
782
+ const inOrderArray = data.filter(
783
+ (item) => getSubsetIndex(item[key], orderSets) !== null
784
+ );
785
+ let wasResorted = false;
786
+ const sortedArray = inOrderArray.sort((a, b) => {
787
+ const aKey = a[key], bKey = b[key];
788
+ const aSubsetIndex = getSubsetIndex(aKey, orderSets);
789
+ const bSubsetIndex = getSubsetIndex(bKey, orderSets);
790
+ if (aSubsetIndex !== null && bSubsetIndex !== null && aSubsetIndex !== bSubsetIndex) {
791
+ return aSubsetIndex - bSubsetIndex;
792
+ }
793
+ return 0;
794
+ });
795
+ const inOrderIterator = sortedArray.values();
796
+ const result = data.map((item) => {
797
+ if (getSubsetIndex(item[key], orderSets) !== null) {
798
+ const sortedItem = inOrderIterator.next().value;
799
+ if (sortedItem[key] !== item[key]) {
800
+ wasResorted = true;
801
+ }
802
+ return sortedItem;
803
+ }
804
+ return item;
805
+ });
806
+ if (!wasResorted) {
807
+ return null;
808
+ }
809
+ return result;
810
+ }
811
+
812
+ // src/rules/infinite-query-property-order/constants.ts
813
+ var infiniteQueryFunctions = [
814
+ "infiniteQueryOptions",
815
+ "useInfiniteQuery",
816
+ "useSuspenseInfiniteQuery"
817
+ ];
818
+ var sortRules = [
819
+ [["queryFn"], ["getPreviousPageParam", "getNextPageParam"]]
820
+ ];
821
+
822
+ // src/rules/infinite-query-property-order/infinite-query-property-order.rule.ts
823
+ var createRule5 = import_utils9.ESLintUtils.RuleCreator(getDocsUrl);
824
+ var infiniteQueryFunctionsSet = new Set(infiniteQueryFunctions);
825
+ function isInfiniteQueryFunction(node) {
826
+ return infiniteQueryFunctionsSet.has(node);
827
+ }
828
+ var name5 = "infinite-query-property-order";
829
+ var rule5 = createRule5({
830
+ name: name5,
831
+ meta: {
832
+ type: "problem",
833
+ docs: {
834
+ description: "Ensure correct order of inference sensitive properties for infinite queries",
835
+ recommended: "error"
836
+ },
837
+ messages: {
838
+ invalidOrder: "Invalid order of properties for `{{function}}`."
839
+ },
840
+ schema: [],
841
+ hasSuggestions: true,
842
+ fixable: "code"
843
+ },
844
+ defaultOptions: [],
845
+ create: detectTanstackQueryImports((context) => {
846
+ return {
847
+ CallExpression(node) {
848
+ if (node.callee.type !== import_utils9.AST_NODE_TYPES.Identifier) {
849
+ return;
850
+ }
851
+ const infiniteQueryFunction = node.callee.name;
852
+ if (!isInfiniteQueryFunction(infiniteQueryFunction)) {
853
+ return;
854
+ }
855
+ const argument = node.arguments[0];
856
+ if (argument === void 0 || argument.type !== "ObjectExpression") {
857
+ return;
858
+ }
859
+ const allProperties = argument.properties;
860
+ if (allProperties.length < 2) {
861
+ return;
862
+ }
863
+ const properties = allProperties.flatMap((p, index) => {
864
+ if (p.type === import_utils9.AST_NODE_TYPES.Property && p.key.type === import_utils9.AST_NODE_TYPES.Identifier) {
865
+ return { name: p.key.name, property: p };
866
+ } else return { name: `_property_${index}`, property: p };
867
+ });
868
+ const sortedProperties = sortDataByOrder(properties, sortRules, "name");
869
+ if (sortedProperties === null) {
870
+ return;
871
+ }
872
+ context.report({
873
+ node: argument,
874
+ data: { function: node.callee.name },
875
+ messageId: "invalidOrder",
876
+ fix(fixer) {
877
+ const sourceCode = context.sourceCode;
878
+ const reorderedText = sortedProperties.reduce(
879
+ (sourceText, specifier, index) => {
880
+ let textBetweenProperties = "";
881
+ if (index < allProperties.length - 1) {
882
+ textBetweenProperties = sourceCode.getText().slice(
883
+ allProperties[index].range[1],
884
+ allProperties[index + 1].range[0]
885
+ );
886
+ }
887
+ return sourceText + sourceCode.getText(specifier.property) + textBetweenProperties;
888
+ },
889
+ ""
890
+ );
891
+ return fixer.replaceTextRange(
892
+ [allProperties[0].range[0], allProperties.at(-1).range[1]],
893
+ reorderedText
894
+ );
895
+ }
896
+ });
897
+ }
898
+ };
899
+ })
900
+ });
901
+
902
+ // src/rules/no-void-query-fn/no-void-query-fn.rule.ts
903
+ var import_utils10 = require("@typescript-eslint/utils");
904
+ var import_typescript = __toESM(require("typescript"), 1);
905
+ var name6 = "no-void-query-fn";
906
+ var createRule6 = import_utils10.ESLintUtils.RuleCreator(getDocsUrl);
907
+ var rule6 = createRule6({
908
+ name: name6,
909
+ meta: {
910
+ type: "problem",
911
+ docs: {
912
+ description: "Ensures queryFn returns a non-undefined value",
913
+ recommended: "error"
914
+ },
915
+ messages: {
916
+ noVoidReturn: "queryFn must return a non-undefined value"
917
+ },
918
+ schema: []
919
+ },
920
+ defaultOptions: [],
921
+ create: detectTanstackQueryImports((context) => {
922
+ return {
923
+ Property(node) {
924
+ var _a;
925
+ if (!ASTUtils.isObjectExpression(node.parent) || !ASTUtils.isIdentifierWithName(node.key, "queryFn")) {
926
+ return;
927
+ }
928
+ const parserServices = context.sourceCode.parserServices;
929
+ if (!parserServices || !parserServices.esTreeNodeToTSNodeMap || !parserServices.program) {
930
+ return;
931
+ }
932
+ const checker = parserServices.program.getTypeChecker();
933
+ const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node.value);
934
+ const type = checker.getTypeAtLocation(tsNode);
935
+ if (type.getCallSignatures().length > 0) {
936
+ const returnType = (_a = type.getCallSignatures()[0]) == null ? void 0 : _a.getReturnType();
937
+ if (!returnType) {
938
+ return;
939
+ }
940
+ if (isIllegalReturn(checker, returnType)) {
941
+ context.report({
942
+ node: node.value,
943
+ messageId: "noVoidReturn"
944
+ });
945
+ }
946
+ }
947
+ }
948
+ };
949
+ })
950
+ });
951
+ function isIllegalReturn(checker, type) {
952
+ const awaited = checker.getAwaitedType(type);
953
+ if (!awaited) return false;
954
+ if (awaited.isUnion()) {
955
+ return awaited.types.some((t) => isIllegalReturn(checker, t));
956
+ }
957
+ return awaited.flags & (import_typescript.default.TypeFlags.Void | import_typescript.default.TypeFlags.Undefined) ? true : false;
958
+ }
959
+
960
+ // src/rules.ts
961
+ var rules = {
962
+ [name]: rule,
963
+ [name2]: rule2,
964
+ [name3]: rule3,
965
+ [name4]: rule4,
966
+ [name5]: rule5,
967
+ [name6]: rule6
968
+ };
969
+ // Annotate the CommonJS export names for ESM import in node:
970
+ 0 && (module.exports = {
971
+ rules
972
+ });
973
+ //# sourceMappingURL=rules.cjs.map