@tanstack/eslint-plugin-query 5.91.5 → 5.94.4

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