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