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