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