@tanstack/eslint-plugin-query 5.0.0-alpha.88 → 5.0.0-beta.5

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 (79) hide show
  1. package/build/legacy/configs/index.cjs +46 -0
  2. package/build/legacy/configs/index.cjs.map +1 -0
  3. package/build/legacy/configs/index.d.cts +8 -0
  4. package/build/legacy/configs/index.d.ts +8 -0
  5. package/build/legacy/configs/index.js +21 -0
  6. package/build/legacy/configs/index.js.map +1 -0
  7. package/build/legacy/configs/index.test.cjs +21 -0
  8. package/build/legacy/configs/index.test.cjs.map +1 -0
  9. package/build/legacy/configs/index.test.d.cts +2 -0
  10. package/build/legacy/configs/index.test.d.ts +2 -0
  11. package/build/legacy/configs/index.test.js +19 -0
  12. package/build/legacy/configs/index.test.js.map +1 -0
  13. package/build/legacy/index.cjs +28 -431
  14. package/build/legacy/index.cjs.map +1 -1
  15. package/build/legacy/index.d.cts +3 -14
  16. package/build/legacy/index.d.ts +3 -14
  17. package/build/legacy/index.js +3 -432
  18. package/build/legacy/index.js.map +1 -1
  19. package/build/legacy/rules/exhaustive-deps/exhaustive-deps.rule.cjs +141 -0
  20. package/build/legacy/rules/exhaustive-deps/exhaustive-deps.rule.cjs.map +1 -0
  21. package/build/legacy/rules/exhaustive-deps/exhaustive-deps.rule.d.cts +6 -0
  22. package/build/legacy/rules/exhaustive-deps/exhaustive-deps.rule.d.ts +6 -0
  23. package/build/legacy/rules/exhaustive-deps/exhaustive-deps.rule.js +115 -0
  24. package/build/legacy/rules/exhaustive-deps/exhaustive-deps.rule.js.map +1 -0
  25. package/build/legacy/rules/exhaustive-deps/exhaustive-deps.test.cjs +722 -0
  26. package/build/legacy/rules/exhaustive-deps/exhaustive-deps.test.cjs.map +1 -0
  27. package/build/legacy/rules/exhaustive-deps/exhaustive-deps.test.d.cts +2 -0
  28. package/build/legacy/rules/exhaustive-deps/exhaustive-deps.test.d.ts +2 -0
  29. package/build/legacy/rules/exhaustive-deps/exhaustive-deps.test.js +720 -0
  30. package/build/legacy/rules/exhaustive-deps/exhaustive-deps.test.js.map +1 -0
  31. package/build/legacy/rules/exhaustive-deps/exhaustive-deps.utils.cjs +52 -0
  32. package/build/legacy/rules/exhaustive-deps/exhaustive-deps.utils.cjs.map +1 -0
  33. package/build/legacy/rules/exhaustive-deps/exhaustive-deps.utils.d.cts +12 -0
  34. package/build/legacy/rules/exhaustive-deps/exhaustive-deps.utils.d.ts +12 -0
  35. package/build/legacy/rules/exhaustive-deps/exhaustive-deps.utils.js +27 -0
  36. package/build/legacy/rules/exhaustive-deps/exhaustive-deps.utils.js.map +1 -0
  37. package/build/legacy/rules/index.cjs +44 -0
  38. package/build/legacy/rules/index.cjs.map +1 -0
  39. package/build/legacy/rules/index.d.cts +7 -0
  40. package/build/legacy/rules/index.d.ts +7 -0
  41. package/build/legacy/rules/index.js +9 -0
  42. package/build/legacy/rules/index.js.map +1 -0
  43. package/build/legacy/utils/ast-utils.cjs +247 -0
  44. package/build/legacy/utils/ast-utils.cjs.map +1 -0
  45. package/build/legacy/utils/ast-utils.d.cts +41 -0
  46. package/build/legacy/utils/ast-utils.d.ts +41 -0
  47. package/build/legacy/utils/ast-utils.js +222 -0
  48. package/build/legacy/utils/ast-utils.js.map +1 -0
  49. package/build/legacy/utils/create-rule.cjs +39 -0
  50. package/build/legacy/utils/create-rule.cjs.map +1 -0
  51. package/build/legacy/utils/create-rule.d.cts +10 -0
  52. package/build/legacy/utils/create-rule.d.ts +10 -0
  53. package/build/legacy/utils/create-rule.js +14 -0
  54. package/build/legacy/utils/create-rule.js.map +1 -0
  55. package/build/legacy/utils/detect-react-query-imports.cjs +70 -0
  56. package/build/legacy/utils/detect-react-query-imports.cjs.map +1 -0
  57. package/build/legacy/utils/detect-react-query-imports.d.cts +12 -0
  58. package/build/legacy/utils/detect-react-query-imports.d.ts +12 -0
  59. package/build/legacy/utils/detect-react-query-imports.js +45 -0
  60. package/build/legacy/utils/detect-react-query-imports.js.map +1 -0
  61. package/build/legacy/utils/object-utils.cjs +33 -0
  62. package/build/legacy/utils/object-utils.cjs.map +1 -0
  63. package/build/legacy/utils/object-utils.d.cts +3 -0
  64. package/build/legacy/utils/object-utils.d.ts +3 -0
  65. package/build/legacy/utils/object-utils.js +8 -0
  66. package/build/legacy/utils/object-utils.js.map +1 -0
  67. package/build/legacy/utils/test-utils.cjs +36 -0
  68. package/build/legacy/utils/test-utils.cjs.map +1 -0
  69. package/build/legacy/utils/test-utils.d.cts +3 -0
  70. package/build/legacy/utils/test-utils.d.ts +3 -0
  71. package/build/legacy/utils/test-utils.js +11 -0
  72. package/build/legacy/utils/test-utils.js.map +1 -0
  73. package/build/legacy/utils/unique-by.cjs +33 -0
  74. package/build/legacy/utils/unique-by.cjs.map +1 -0
  75. package/build/legacy/utils/unique-by.d.cts +3 -0
  76. package/build/legacy/utils/unique-by.d.ts +3 -0
  77. package/build/legacy/utils/unique-by.js +8 -0
  78. package/build/legacy/utils/unique-by.js.map +1 -0
  79. package/package.json +2 -2
@@ -0,0 +1,141 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name2 in all)
8
+ __defProp(target, name2, { get: all[name2], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/rules/exhaustive-deps/exhaustive-deps.rule.ts
21
+ var exhaustive_deps_rule_exports = {};
22
+ __export(exhaustive_deps_rule_exports, {
23
+ name: () => name,
24
+ rule: () => rule
25
+ });
26
+ module.exports = __toCommonJS(exhaustive_deps_rule_exports);
27
+ var import_utils = require("@typescript-eslint/utils");
28
+ var import_ast_utils = require("../../utils/ast-utils.cjs");
29
+ var import_create_rule = require("../../utils/create-rule.cjs");
30
+ var import_unique_by = require("../../utils/unique-by.cjs");
31
+ var import_exhaustive_deps = require("./exhaustive-deps.utils.cjs");
32
+ var QUERY_KEY = "queryKey";
33
+ var QUERY_FN = "queryFn";
34
+ var name = "exhaustive-deps";
35
+ var rule = (0, import_create_rule.createRule)({
36
+ name,
37
+ meta: {
38
+ type: "problem",
39
+ docs: {
40
+ description: "Exhaustive deps rule for useQuery",
41
+ recommended: "error"
42
+ },
43
+ messages: {
44
+ missingDeps: `The following dependencies are missing in your queryKey: {{deps}}`,
45
+ fixTo: "Fix to {{result}}"
46
+ },
47
+ hasSuggestions: true,
48
+ fixable: "code",
49
+ schema: []
50
+ },
51
+ defaultOptions: [],
52
+ create(context) {
53
+ return {
54
+ Property(node) {
55
+ if (node.parent === void 0 || !import_ast_utils.ASTUtils.isObjectExpression(node.parent) || !import_ast_utils.ASTUtils.isIdentifierWithName(node.key, QUERY_KEY)) {
56
+ return;
57
+ }
58
+ const scopeManager = context.getSourceCode().scopeManager;
59
+ const queryKey = import_ast_utils.ASTUtils.findPropertyWithIdentifierKey(
60
+ node.parent.properties,
61
+ QUERY_KEY
62
+ );
63
+ const queryFn = import_ast_utils.ASTUtils.findPropertyWithIdentifierKey(
64
+ node.parent.properties,
65
+ QUERY_FN
66
+ );
67
+ if (scopeManager === null || queryKey === void 0 || queryFn === void 0 || queryFn.value.type !== import_utils.AST_NODE_TYPES.ArrowFunctionExpression) {
68
+ return;
69
+ }
70
+ let queryKeyNode = queryKey.value;
71
+ if (queryKeyNode.type === import_utils.AST_NODE_TYPES.TSAsExpression && queryKeyNode.expression.type === import_utils.AST_NODE_TYPES.ArrayExpression) {
72
+ queryKeyNode = queryKeyNode.expression;
73
+ }
74
+ if (queryKeyNode.type === import_utils.AST_NODE_TYPES.Identifier) {
75
+ const expression = import_ast_utils.ASTUtils.getReferencedExpressionByIdentifier({
76
+ context,
77
+ node: queryKeyNode
78
+ });
79
+ if ((expression == null ? void 0 : expression.type) === import_utils.AST_NODE_TYPES.ArrayExpression) {
80
+ queryKeyNode = expression;
81
+ }
82
+ }
83
+ const sourceCode = context.getSourceCode();
84
+ const queryKeyValue = queryKeyNode;
85
+ const externalRefs = import_ast_utils.ASTUtils.getExternalRefs({
86
+ scopeManager,
87
+ sourceCode,
88
+ node: queryFn.value
89
+ });
90
+ const relevantRefs = externalRefs.filter(
91
+ (reference) => import_exhaustive_deps.ExhaustiveDepsUtils.isRelevantReference({
92
+ context,
93
+ reference,
94
+ scopeManager
95
+ })
96
+ );
97
+ const existingKeys = import_ast_utils.ASTUtils.getNestedIdentifiers(queryKeyValue).map(
98
+ (identifier) => import_ast_utils.ASTUtils.mapKeyNodeToText(identifier, sourceCode)
99
+ );
100
+ const missingRefs = relevantRefs.map((ref) => ({
101
+ ref,
102
+ text: import_ast_utils.ASTUtils.mapKeyNodeToText(ref.identifier, sourceCode)
103
+ })).filter(({ ref, text }) => {
104
+ return !ref.isTypeReference && !import_ast_utils.ASTUtils.isAncestorIsCallee(ref.identifier) && !existingKeys.some((existingKey) => existingKey === text) && !existingKeys.includes(text.split(".")[0] ?? "");
105
+ }).map(({ ref, text }) => ({
106
+ identifier: ref.identifier,
107
+ text
108
+ }));
109
+ const uniqueMissingRefs = (0, import_unique_by.uniqueBy)(missingRefs, (x) => x.text);
110
+ if (uniqueMissingRefs.length > 0) {
111
+ const missingAsText = uniqueMissingRefs.map((ref) => import_ast_utils.ASTUtils.mapKeyNodeToText(ref.identifier, sourceCode)).join(", ");
112
+ const existingWithMissing = sourceCode.getText(queryKeyValue).replace(/\]$/, `, ${missingAsText}]`);
113
+ const suggestions = [];
114
+ if (queryKeyNode.type === import_utils.AST_NODE_TYPES.ArrayExpression) {
115
+ suggestions.push({
116
+ messageId: "fixTo",
117
+ data: { result: existingWithMissing },
118
+ fix(fixer) {
119
+ return fixer.replaceText(queryKeyValue, existingWithMissing);
120
+ }
121
+ });
122
+ }
123
+ context.report({
124
+ node,
125
+ messageId: "missingDeps",
126
+ data: {
127
+ deps: uniqueMissingRefs.map((ref) => ref.text).join(", ")
128
+ },
129
+ suggest: suggestions
130
+ });
131
+ }
132
+ }
133
+ };
134
+ }
135
+ });
136
+ // Annotate the CommonJS export names for ESM import in node:
137
+ 0 && (module.exports = {
138
+ name,
139
+ rule
140
+ });
141
+ //# sourceMappingURL=exhaustive-deps.rule.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../src/rules/exhaustive-deps/exhaustive-deps.rule.ts"],"sourcesContent":["import { AST_NODE_TYPES } from '@typescript-eslint/utils'\nimport { ASTUtils } from '../../utils/ast-utils'\nimport { createRule } from '../../utils/create-rule'\nimport { uniqueBy } from '../../utils/unique-by'\nimport { ExhaustiveDepsUtils } from './exhaustive-deps.utils'\nimport type { TSESLint } from '@typescript-eslint/utils'\n\nconst QUERY_KEY = 'queryKey'\nconst QUERY_FN = 'queryFn'\n\nexport const name = 'exhaustive-deps'\n\nexport const rule = createRule({\n name,\n meta: {\n type: 'problem',\n docs: {\n description: 'Exhaustive deps rule for useQuery',\n recommended: 'error',\n },\n messages: {\n missingDeps: `The following dependencies are missing in your queryKey: {{deps}}`,\n fixTo: 'Fix to {{result}}',\n },\n hasSuggestions: true,\n fixable: 'code',\n schema: [],\n },\n defaultOptions: [],\n\n create(context) {\n return {\n Property(node) {\n if (\n node.parent === undefined ||\n !ASTUtils.isObjectExpression(node.parent) ||\n !ASTUtils.isIdentifierWithName(node.key, QUERY_KEY)\n ) {\n return\n }\n\n const scopeManager = context.getSourceCode().scopeManager\n const queryKey = ASTUtils.findPropertyWithIdentifierKey(\n node.parent.properties,\n QUERY_KEY,\n )\n const queryFn = ASTUtils.findPropertyWithIdentifierKey(\n node.parent.properties,\n QUERY_FN,\n )\n\n if (\n scopeManager === null ||\n queryKey === undefined ||\n queryFn === undefined ||\n queryFn.value.type !== AST_NODE_TYPES.ArrowFunctionExpression\n ) {\n return\n }\n\n let queryKeyNode = queryKey.value\n\n if (\n queryKeyNode.type === AST_NODE_TYPES.TSAsExpression &&\n queryKeyNode.expression.type === AST_NODE_TYPES.ArrayExpression\n ) {\n queryKeyNode = queryKeyNode.expression\n }\n\n if (queryKeyNode.type === AST_NODE_TYPES.Identifier) {\n const expression = ASTUtils.getReferencedExpressionByIdentifier({\n context,\n node: queryKeyNode,\n })\n\n if (expression?.type === AST_NODE_TYPES.ArrayExpression) {\n queryKeyNode = expression\n }\n }\n\n const sourceCode = context.getSourceCode()\n const queryKeyValue = queryKeyNode\n const externalRefs = ASTUtils.getExternalRefs({\n scopeManager,\n sourceCode,\n node: queryFn.value,\n })\n\n const relevantRefs = externalRefs.filter((reference) =>\n ExhaustiveDepsUtils.isRelevantReference({\n context,\n reference,\n scopeManager,\n }),\n )\n\n const existingKeys = ASTUtils.getNestedIdentifiers(queryKeyValue).map(\n (identifier) => ASTUtils.mapKeyNodeToText(identifier, sourceCode),\n )\n\n const missingRefs = relevantRefs\n .map((ref) => ({\n ref: ref,\n text: ASTUtils.mapKeyNodeToText(ref.identifier, sourceCode),\n }))\n .filter(({ ref, text }) => {\n return (\n !ref.isTypeReference &&\n !ASTUtils.isAncestorIsCallee(ref.identifier) &&\n !existingKeys.some((existingKey) => existingKey === text) &&\n !existingKeys.includes(text.split('.')[0] ?? '')\n )\n })\n .map(({ ref, text }) => ({\n identifier: ref.identifier,\n text: text,\n }))\n\n const uniqueMissingRefs = uniqueBy(missingRefs, (x) => x.text)\n\n if (uniqueMissingRefs.length > 0) {\n const missingAsText = uniqueMissingRefs\n .map((ref) => ASTUtils.mapKeyNodeToText(ref.identifier, sourceCode))\n .join(', ')\n\n const existingWithMissing = sourceCode\n .getText(queryKeyValue)\n .replace(/\\]$/, `, ${missingAsText}]`)\n\n const suggestions: TSESLint.ReportSuggestionArray<string> = []\n\n if (queryKeyNode.type === AST_NODE_TYPES.ArrayExpression) {\n suggestions.push({\n messageId: 'fixTo',\n data: { result: existingWithMissing },\n fix(fixer) {\n return fixer.replaceText(queryKeyValue, existingWithMissing)\n },\n })\n }\n\n context.report({\n node: node,\n messageId: 'missingDeps',\n data: {\n deps: uniqueMissingRefs.map((ref) => ref.text).join(', '),\n },\n suggest: suggestions,\n })\n }\n },\n }\n },\n})\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAA+B;AAC/B,uBAAyB;AACzB,yBAA2B;AAC3B,uBAAyB;AACzB,6BAAoC;AAGpC,IAAM,YAAY;AAClB,IAAM,WAAW;AAEV,IAAM,OAAO;AAEb,IAAM,WAAO,+BAAW;AAAA,EAC7B;AAAA,EACA,MAAM;AAAA,IACJ,MAAM;AAAA,IACN,MAAM;AAAA,MACJ,aAAa;AAAA,MACb,aAAa;AAAA,IACf;AAAA,IACA,UAAU;AAAA,MACR,aAAa;AAAA,MACb,OAAO;AAAA,IACT;AAAA,IACA,gBAAgB;AAAA,IAChB,SAAS;AAAA,IACT,QAAQ,CAAC;AAAA,EACX;AAAA,EACA,gBAAgB,CAAC;AAAA,EAEjB,OAAO,SAAS;AACd,WAAO;AAAA,MACL,SAAS,MAAM;AACb,YACE,KAAK,WAAW,UAChB,CAAC,0BAAS,mBAAmB,KAAK,MAAM,KACxC,CAAC,0BAAS,qBAAqB,KAAK,KAAK,SAAS,GAClD;AACA;AAAA,QACF;AAEA,cAAM,eAAe,QAAQ,cAAc,EAAE;AAC7C,cAAM,WAAW,0BAAS;AAAA,UACxB,KAAK,OAAO;AAAA,UACZ;AAAA,QACF;AACA,cAAM,UAAU,0BAAS;AAAA,UACvB,KAAK,OAAO;AAAA,UACZ;AAAA,QACF;AAEA,YACE,iBAAiB,QACjB,aAAa,UACb,YAAY,UACZ,QA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AoB,2BAAS,aAAa,CAAC,MAAM,EAAE,IAAI;AAE7D,YAAI,kBAAkB,SAAS,GAAG;AAChC,gBAAM,gBAAgB,kBACnB,IAAI,CAAC,QAAQ,0BAAS,iBAAiB,IAAI,YAAY,UAAU,CAAC,EAClE,KAAK,IAAI;AAEZ,gBAAM,sBAAsB,WACzB,QAAQ,aAAa,EACrB,QAAQ,OAAO,KAAK,aAAa,GAAG;AAEvC,gBAAM,cAAsD,CAAC;AAE7D,cAAI,aAAa,SAAS,4BAAe,iBAAiB;AACxD,wBAAY,KAAK;AAAA,cACf,WAAW;AAAA,cACX,MAAM,EAAE,QAAQ,oBAAoB;AAAA,cACpC,IAAI,OAAO;AACT,uBAAO,MAAM,YAAY,eAAe,mBAAmB;AAAA,cAC7D;AAAA,YACF,CAAC;AAAA,UACH;AAEA,kBAAQ,OAAO;AAAA,YACb;AAAA,YACA,WAAW;AAAA,YACX,MAAM;AAAA,cACJ,MAAM,kBAAkB,IAAI,CAAC,QAAQ,IAAI,IAAI,EAAE,KAAK,IAAI;AAAA,YAC1D;AAAA,YACA,SAAS;AAAA,UACX,CAAC;AAAA,QACH;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF,CAAC;","names":[]}
@@ -0,0 +1,6 @@
1
+ import { TSESLint } from '@typescript-eslint/utils';
2
+
3
+ declare const name = "exhaustive-deps";
4
+ declare const rule: TSESLint.RuleModule<string, readonly unknown[], TSESLint.RuleListener>;
5
+
6
+ export { name, rule };
@@ -0,0 +1,6 @@
1
+ import { TSESLint } from '@typescript-eslint/utils';
2
+
3
+ declare const name = "exhaustive-deps";
4
+ declare const rule: TSESLint.RuleModule<string, readonly unknown[], TSESLint.RuleListener>;
5
+
6
+ export { name, rule };
@@ -0,0 +1,115 @@
1
+ // src/rules/exhaustive-deps/exhaustive-deps.rule.ts
2
+ import { AST_NODE_TYPES } from "@typescript-eslint/utils";
3
+ import { ASTUtils } from "../../utils/ast-utils.js";
4
+ import { createRule } from "../../utils/create-rule.js";
5
+ import { uniqueBy } from "../../utils/unique-by.js";
6
+ import { ExhaustiveDepsUtils } from "./exhaustive-deps.utils.js";
7
+ var QUERY_KEY = "queryKey";
8
+ var QUERY_FN = "queryFn";
9
+ var name = "exhaustive-deps";
10
+ var rule = createRule({
11
+ name,
12
+ meta: {
13
+ type: "problem",
14
+ docs: {
15
+ description: "Exhaustive deps rule for useQuery",
16
+ recommended: "error"
17
+ },
18
+ messages: {
19
+ missingDeps: `The following dependencies are missing in your queryKey: {{deps}}`,
20
+ fixTo: "Fix to {{result}}"
21
+ },
22
+ hasSuggestions: true,
23
+ fixable: "code",
24
+ schema: []
25
+ },
26
+ defaultOptions: [],
27
+ create(context) {
28
+ return {
29
+ Property(node) {
30
+ if (node.parent === void 0 || !ASTUtils.isObjectExpression(node.parent) || !ASTUtils.isIdentifierWithName(node.key, QUERY_KEY)) {
31
+ return;
32
+ }
33
+ const scopeManager = context.getSourceCode().scopeManager;
34
+ const queryKey = ASTUtils.findPropertyWithIdentifierKey(
35
+ node.parent.properties,
36
+ QUERY_KEY
37
+ );
38
+ const queryFn = ASTUtils.findPropertyWithIdentifierKey(
39
+ node.parent.properties,
40
+ QUERY_FN
41
+ );
42
+ if (scopeManager === null || queryKey === void 0 || queryFn === void 0 || queryFn.value.type !== AST_NODE_TYPES.ArrowFunctionExpression) {
43
+ return;
44
+ }
45
+ let queryKeyNode = queryKey.value;
46
+ if (queryKeyNode.type === AST_NODE_TYPES.TSAsExpression && queryKeyNode.expression.type === AST_NODE_TYPES.ArrayExpression) {
47
+ queryKeyNode = queryKeyNode.expression;
48
+ }
49
+ if (queryKeyNode.type === AST_NODE_TYPES.Identifier) {
50
+ const expression = ASTUtils.getReferencedExpressionByIdentifier({
51
+ context,
52
+ node: queryKeyNode
53
+ });
54
+ if ((expression == null ? void 0 : expression.type) === AST_NODE_TYPES.ArrayExpression) {
55
+ queryKeyNode = expression;
56
+ }
57
+ }
58
+ const sourceCode = context.getSourceCode();
59
+ const queryKeyValue = queryKeyNode;
60
+ const externalRefs = ASTUtils.getExternalRefs({
61
+ scopeManager,
62
+ sourceCode,
63
+ node: queryFn.value
64
+ });
65
+ const relevantRefs = externalRefs.filter(
66
+ (reference) => ExhaustiveDepsUtils.isRelevantReference({
67
+ context,
68
+ reference,
69
+ scopeManager
70
+ })
71
+ );
72
+ const existingKeys = ASTUtils.getNestedIdentifiers(queryKeyValue).map(
73
+ (identifier) => ASTUtils.mapKeyNodeToText(identifier, sourceCode)
74
+ );
75
+ const missingRefs = relevantRefs.map((ref) => ({
76
+ ref,
77
+ text: ASTUtils.mapKeyNodeToText(ref.identifier, sourceCode)
78
+ })).filter(({ ref, text }) => {
79
+ return !ref.isTypeReference && !ASTUtils.isAncestorIsCallee(ref.identifier) && !existingKeys.some((existingKey) => existingKey === text) && !existingKeys.includes(text.split(".")[0] ?? "");
80
+ }).map(({ ref, text }) => ({
81
+ identifier: ref.identifier,
82
+ text
83
+ }));
84
+ const uniqueMissingRefs = uniqueBy(missingRefs, (x) => x.text);
85
+ if (uniqueMissingRefs.length > 0) {
86
+ const missingAsText = uniqueMissingRefs.map((ref) => ASTUtils.mapKeyNodeToText(ref.identifier, sourceCode)).join(", ");
87
+ const existingWithMissing = sourceCode.getText(queryKeyValue).replace(/\]$/, `, ${missingAsText}]`);
88
+ const suggestions = [];
89
+ if (queryKeyNode.type === AST_NODE_TYPES.ArrayExpression) {
90
+ suggestions.push({
91
+ messageId: "fixTo",
92
+ data: { result: existingWithMissing },
93
+ fix(fixer) {
94
+ return fixer.replaceText(queryKeyValue, existingWithMissing);
95
+ }
96
+ });
97
+ }
98
+ context.report({
99
+ node,
100
+ messageId: "missingDeps",
101
+ data: {
102
+ deps: uniqueMissingRefs.map((ref) => ref.text).join(", ")
103
+ },
104
+ suggest: suggestions
105
+ });
106
+ }
107
+ }
108
+ };
109
+ }
110
+ });
111
+ export {
112
+ name,
113
+ rule
114
+ };
115
+ //# sourceMappingURL=exhaustive-deps.rule.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../src/rules/exhaustive-deps/exhaustive-deps.rule.ts"],"sourcesContent":["import { AST_NODE_TYPES } from '@typescript-eslint/utils'\nimport { ASTUtils } from '../../utils/ast-utils'\nimport { createRule } from '../../utils/create-rule'\nimport { uniqueBy } from '../../utils/unique-by'\nimport { ExhaustiveDepsUtils } from './exhaustive-deps.utils'\nimport type { TSESLint } from '@typescript-eslint/utils'\n\nconst QUERY_KEY = 'queryKey'\nconst QUERY_FN = 'queryFn'\n\nexport const name = 'exhaustive-deps'\n\nexport const rule = createRule({\n name,\n meta: {\n type: 'problem',\n docs: {\n description: 'Exhaustive deps rule for useQuery',\n recommended: 'error',\n },\n messages: {\n missingDeps: `The following dependencies are missing in your queryKey: {{deps}}`,\n fixTo: 'Fix to {{result}}',\n },\n hasSuggestions: true,\n fixable: 'code',\n schema: [],\n },\n defaultOptions: [],\n\n create(context) {\n return {\n Property(node) {\n if (\n node.parent === undefined ||\n !ASTUtils.isObjectExpression(node.parent) ||\n !ASTUtils.isIdentifierWithName(node.key, QUERY_KEY)\n ) {\n return\n }\n\n const scopeManager = context.getSourceCode().scopeManager\n const queryKey = ASTUtils.findPropertyWithIdentifierKey(\n node.parent.properties,\n QUERY_KEY,\n )\n const queryFn = ASTUtils.findPropertyWithIdentifierKey(\n node.parent.properties,\n QUERY_FN,\n )\n\n if (\n scopeManager === null ||\n queryKey === undefined ||\n queryFn === undefined ||\n queryFn.value.type !== AST_NODE_TYPES.ArrowFunctionExpression\n ) {\n return\n }\n\n let queryKeyNode = queryKey.value\n\n if (\n queryKeyNode.type === AST_NODE_TYPES.TSAsExpression &&\n queryKeyNode.expression.type === AST_NODE_TYPES.ArrayExpression\n ) {\n queryKeyNode = queryKeyNode.expression\n }\n\n if (queryKeyNode.type === AST_NODE_TYPES.Identifier) {\n const expression = ASTUtils.getReferencedExpressionByIdentifier({\n context,\n node: queryKeyNode,\n })\n\n if (expression?.type === AST_NODE_TYPES.ArrayExpression) {\n queryKeyNode = expression\n }\n }\n\n const sourceCode = context.getSourceCode()\n const queryKeyValue = queryKeyNode\n const externalRefs = ASTUtils.getExternalRefs({\n scopeManager,\n sourceCode,\n node: queryFn.value,\n })\n\n const relevantRefs = externalRefs.filter((reference) =>\n ExhaustiveDepsUtils.isRelevantReference({\n context,\n reference,\n scopeManager,\n }),\n )\n\n const existingKeys = ASTUtils.getNestedIdentifiers(queryKeyValue).map(\n (identifier) => ASTUtils.mapKeyNodeToText(identifier, sourceCode),\n )\n\n const missingRefs = relevantRefs\n .map((ref) => ({\n ref: ref,\n text: ASTUtils.mapKeyNodeToText(ref.identifier, sourceCode),\n }))\n .filter(({ ref, text }) => {\n return (\n !ref.isTypeReference &&\n !ASTUtils.isAncestorIsCallee(ref.identifier) &&\n !existingKeys.some((existingKey) => existingKey === text) &&\n !existingKeys.includes(text.split('.')[0] ?? '')\n )\n })\n .map(({ ref, text }) => ({\n identifier: ref.identifier,\n text: text,\n }))\n\n const uniqueMissingRefs = uniqueBy(missingRefs, (x) => x.text)\n\n if (uniqueMissingRefs.length > 0) {\n const missingAsText = uniqueMissingRefs\n .map((ref) => ASTUtils.mapKeyNodeToText(ref.identifier, sourceCode))\n .join(', ')\n\n const existingWithMissing = sourceCode\n .getText(queryKeyValue)\n .replace(/\\]$/, `, ${missingAsText}]`)\n\n const suggestions: TSESLint.ReportSuggestionArray<string> = []\n\n if (queryKeyNode.type === AST_NODE_TYPES.ArrayExpression) {\n suggestions.push({\n messageId: 'fixTo',\n data: { result: existingWithMissing },\n fix(fixer) {\n return fixer.replaceText(queryKeyValue, existingWithMissing)\n },\n })\n }\n\n context.report({\n node: node,\n messageId: 'missingDeps',\n data: {\n deps: uniqueMissingRefs.map((ref) => ref.text).join(', '),\n },\n suggest: suggestions,\n })\n }\n },\n }\n },\n})\n"],"mappings":";AAAA,SAAS,sBAAsB;AAC/B,SAAS,gBAAgB;AACzB,SAAS,kBAAkB;AAC3B,SAAS,gBAAgB;AACzB,SAAS,2BAA2B;AAGpC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