@tanstack/eslint-plugin-query 5.104.1 → 5.104.3
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/build/legacy/index.cjs +1 -1
- package/build/legacy/index.cjs.map +1 -1
- package/build/legacy/index.d.cts +11 -0
- package/build/legacy/index.d.cts.map +1 -1
- package/build/legacy/index.d.ts +11 -0
- package/build/legacy/index.d.ts.map +1 -1
- package/build/legacy/index.js +1 -1
- package/build/legacy/index.js.map +1 -1
- package/build/legacy/{rules-Bv1SB3jU.js → rules-CLqlFYyw.js} +218 -3
- package/build/legacy/rules-CLqlFYyw.js.map +1 -0
- package/build/legacy/{rules-ChMfhjqW.cjs → rules-CzcCSM4O.cjs} +218 -3
- package/build/legacy/rules-CzcCSM4O.cjs.map +1 -0
- package/build/legacy/rules.cjs +1 -1
- package/build/legacy/rules.js +1 -1
- package/build/legacy/types.d.cts +8 -0
- package/build/legacy/types.d.cts.map +1 -1
- package/build/legacy/types.d.ts +8 -0
- package/build/legacy/types.d.ts.map +1 -1
- package/build/modern/index.cjs +1 -1
- package/build/modern/index.cjs.map +1 -1
- package/build/modern/index.d.cts +11 -0
- package/build/modern/index.d.cts.map +1 -1
- package/build/modern/index.d.ts +11 -0
- package/build/modern/index.d.ts.map +1 -1
- package/build/modern/index.js +1 -1
- package/build/modern/index.js.map +1 -1
- package/build/modern/{rules-B1yVUvLh.cjs → rules-D_RuMaj7.cjs} +218 -3
- package/build/modern/rules-D_RuMaj7.cjs.map +1 -0
- package/build/modern/{rules-DzixDjO7.js → rules-w_lOoMmF.js} +218 -3
- package/build/modern/rules-w_lOoMmF.js.map +1 -0
- package/build/modern/rules.cjs +1 -1
- package/build/modern/rules.js +1 -1
- package/build/modern/types.d.cts +8 -0
- package/build/modern/types.d.cts.map +1 -1
- package/build/modern/types.d.ts +8 -0
- package/build/modern/types.d.ts.map +1 -1
- package/package.json +6 -6
- package/src/index.ts +11 -0
- package/src/rules/exhaustive-deps/exhaustive-deps.rule.ts +12 -0
- package/src/rules/exhaustive-deps/exhaustive-deps.utils.ts +45 -2
- package/src/rules/infinite-query-property-order/constants.ts +6 -0
- package/src/rules/mutation-property-order/constants.ts +6 -0
- package/src/rules/no-rest-destructuring/no-rest-destructuring.utils.ts +5 -0
- package/src/rules/no-unstable-deps/no-unstable-deps.rule.ts +46 -0
- package/src/rules/no-void-query-fn/no-void-query-fn.rule.ts +7 -0
- package/src/rules/prefer-query-options/prefer-query-options.rule.ts +62 -0
- package/src/rules/stable-query-client/stable-query-client.rule.ts +1 -1
- package/src/types.ts +8 -0
- package/src/utils/ast-utils.ts +2 -2
- package/src/utils/create-property-order-rule.ts +14 -0
- package/src/utils/detect-react-query-imports.ts +7 -1
- package/src/utils/get-docs-url.ts +5 -0
- package/src/utils/sort-data-by-order.ts +9 -0
- package/src/utils/unique-by.ts +6 -0
- package/build/legacy/rules-Bv1SB3jU.js.map +0 -1
- package/build/legacy/rules-ChMfhjqW.cjs.map +0 -1
- package/build/modern/rules-B1yVUvLh.cjs.map +0 -1
- package/build/modern/rules-DzixDjO7.js.map +0 -1
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
let _typescript_eslint_utils = require("@typescript-eslint/utils");
|
|
2
2
|
//#region src/utils/unique-by.ts
|
|
3
|
+
/**
|
|
4
|
+
* Removes duplicate items, keeping the first item for each key.
|
|
5
|
+
* @param arr - The items.
|
|
6
|
+
* @param fn - Returns the key that identifies an item.
|
|
7
|
+
* @returns The items with unique keys, in their original order.
|
|
8
|
+
*/
|
|
3
9
|
function uniqueBy(arr, fn) {
|
|
4
10
|
return arr.filter((x, i, a) => a.findIndex((y) => fn(x) === fn(y)) === i);
|
|
5
11
|
}
|
|
@@ -95,9 +101,20 @@ const ASTUtils = {
|
|
|
95
101
|
};
|
|
96
102
|
//#endregion
|
|
97
103
|
//#region src/utils/get-docs-url.ts
|
|
104
|
+
/**
|
|
105
|
+
* Builds the URL of a rule's documentation page.
|
|
106
|
+
* @param ruleName - The rule's name, e.g. `exhaustive-deps`.
|
|
107
|
+
* @returns The URL of the rule's page in the ESLint plugin docs.
|
|
108
|
+
*/
|
|
98
109
|
const getDocsUrl = (ruleName) => `https://tanstack.com/query/latest/docs/eslint/${ruleName}`;
|
|
99
110
|
//#endregion
|
|
100
111
|
//#region src/utils/detect-react-query-imports.ts
|
|
112
|
+
/**
|
|
113
|
+
* Wraps a rule's `create` function to track the identifiers imported from TanStack Query packages,
|
|
114
|
+
* and passes helpers that check them.
|
|
115
|
+
* @param create - The rule's `create` function, which also receives the helpers.
|
|
116
|
+
* @returns The wrapped `create` function.
|
|
117
|
+
*/
|
|
101
118
|
function detectTanstackQueryImports(create) {
|
|
102
119
|
return (context, optionsWithDefault) => {
|
|
103
120
|
const tanstackQueryImportSpecifiers = [];
|
|
@@ -165,6 +182,9 @@ const ExhaustiveDepsUtils = {
|
|
|
165
182
|
/**
|
|
166
183
|
* Given required refs and existing queryKey entries, compute missing dependency paths
|
|
167
184
|
* respecting allowlisted variables and types.
|
|
185
|
+
* @param params - The references the query function needs, the allowlisted variables, and the
|
|
186
|
+
* root identifiers and full paths already in the query key.
|
|
187
|
+
* @returns The missing dependency paths, without paths whose root is already missing.
|
|
168
188
|
*/
|
|
169
189
|
computeFilteredMissingPaths(params) {
|
|
170
190
|
const { requiredRefs, allowlistedVariables, existingRootIdentifiers, existingFullPaths } = params;
|
|
@@ -184,21 +204,35 @@ const ExhaustiveDepsUtils = {
|
|
|
184
204
|
},
|
|
185
205
|
/**
|
|
186
206
|
* Extract existing queryKey deps as root identifiers and full member paths.
|
|
207
|
+
* @param params - The source code, the scope manager, and the query key node to scan.
|
|
208
|
+
* @returns The root identifiers that cover all of their members, and every dependency path.
|
|
187
209
|
*/
|
|
188
210
|
collectQueryKeyDeps(params) {
|
|
189
211
|
const { sourceCode, scopeManager, queryKeyNode } = params;
|
|
190
212
|
const roots = /* @__PURE__ */ new Set();
|
|
191
213
|
const paths = /* @__PURE__ */ new Set();
|
|
192
214
|
const visitorKeys = sourceCode.visitorKeys;
|
|
215
|
+
/**
|
|
216
|
+
* Records an identifier that covers all of its members.
|
|
217
|
+
* @param name - The identifier, normalized before it is recorded.
|
|
218
|
+
*/
|
|
193
219
|
function addRoot(name) {
|
|
194
220
|
const cleaned = ExhaustiveDepsUtils.normalizeChain(name);
|
|
195
221
|
roots.add(cleaned);
|
|
196
222
|
paths.add(cleaned);
|
|
197
223
|
}
|
|
224
|
+
/**
|
|
225
|
+
* Records a full member path.
|
|
226
|
+
* @param text - The path, normalized before it is recorded.
|
|
227
|
+
*/
|
|
198
228
|
function addFull(text) {
|
|
199
229
|
const cleaned = ExhaustiveDepsUtils.normalizeChain(text);
|
|
200
230
|
paths.add(cleaned);
|
|
201
231
|
}
|
|
232
|
+
/**
|
|
233
|
+
* Records a computed reference path as a root or a full path.
|
|
234
|
+
* @param refPath - The reference path, or `null` to record nothing.
|
|
235
|
+
*/
|
|
202
236
|
function addRefPath(refPath) {
|
|
203
237
|
if (!refPath) return;
|
|
204
238
|
if (refPath.coversRootMembers) {
|
|
@@ -207,6 +241,10 @@ const ExhaustiveDepsUtils = {
|
|
|
207
241
|
}
|
|
208
242
|
addFull(refPath.path);
|
|
209
243
|
}
|
|
244
|
+
/**
|
|
245
|
+
* Visits every child node of a node.
|
|
246
|
+
* @param node - The node whose children are visited.
|
|
247
|
+
*/
|
|
210
248
|
function visitChildren(node) {
|
|
211
249
|
const keys = visitorKeys[node.type] ?? [];
|
|
212
250
|
for (const key of keys) {
|
|
@@ -218,6 +256,11 @@ const ExhaustiveDepsUtils = {
|
|
|
218
256
|
if (ExhaustiveDepsUtils.isNode(value)) visit(value);
|
|
219
257
|
}
|
|
220
258
|
}
|
|
259
|
+
/**
|
|
260
|
+
* Records the dependencies of a node: identifiers directly, and the external references of
|
|
261
|
+
* functions. Other nodes are visited recursively.
|
|
262
|
+
* @param node - The node to visit.
|
|
263
|
+
*/
|
|
221
264
|
function visit(node) {
|
|
222
265
|
if (!node) return;
|
|
223
266
|
switch (node.type) {
|
|
@@ -269,7 +312,9 @@ const ExhaustiveDepsUtils = {
|
|
|
269
312
|
return typeof value === "object" && value !== null && "type" in value && typeof value.type === "string";
|
|
270
313
|
},
|
|
271
314
|
/**
|
|
272
|
-
* Checks whether the resolved variable is allowlisted by its type annotation
|
|
315
|
+
* Checks whether the resolved variable is allowlisted by its type annotation.
|
|
316
|
+
* @param params - The allowlisted type names, and the variable to check.
|
|
317
|
+
* @returns `true` if a type referenced in the variable's type annotation is allowlisted.
|
|
273
318
|
*/
|
|
274
319
|
variableIsAllowlistedByType(params) {
|
|
275
320
|
const { allowlistedTypes, variable } = params;
|
|
@@ -289,6 +334,8 @@ const ExhaustiveDepsUtils = {
|
|
|
289
334
|
* Normalizes a chain by removing optional chaining operators
|
|
290
335
|
*
|
|
291
336
|
* Example: `a?.b.c!` -> `a.b.c`
|
|
337
|
+
* @param text - The source text of the chain.
|
|
338
|
+
* @returns The chain without optional chaining, non-null assertions, and whitespace.
|
|
292
339
|
*/
|
|
293
340
|
normalizeChain(text) {
|
|
294
341
|
return text.replace(/(?:\?(\.)|!)/g, "$1").replace(/\s+/g, "");
|
|
@@ -297,6 +344,9 @@ const ExhaustiveDepsUtils = {
|
|
|
297
344
|
* Computes the reference path for an identifier
|
|
298
345
|
*
|
|
299
346
|
* Example: `a.b.c!` -> `{ path: 'a.b.c', root: 'a' }`
|
|
347
|
+
* @param params - The identifier, and the source code to read the chain from.
|
|
348
|
+
* @returns The dependency `path`, its `root`, and whether the path is the root itself and covers
|
|
349
|
+
* all of its members. For a method call, the method name is dropped from the path.
|
|
300
350
|
*/
|
|
301
351
|
computeRefPath(params) {
|
|
302
352
|
const { identifier, sourceCode } = params;
|
|
@@ -323,6 +373,11 @@ const ExhaustiveDepsUtils = {
|
|
|
323
373
|
const functionScope = scopeManager.acquire(functionNode);
|
|
324
374
|
if (functionScope === null) return [];
|
|
325
375
|
const externalRefs = [];
|
|
376
|
+
/**
|
|
377
|
+
* Collects the read references of a scope and its child scopes that resolve to variables
|
|
378
|
+
* declared outside the function.
|
|
379
|
+
* @param scope - The scope to collect from.
|
|
380
|
+
*/
|
|
326
381
|
function collect(scope) {
|
|
327
382
|
for (const reference of scope.references) {
|
|
328
383
|
if (!reference.isRead() || reference.resolved === null) continue;
|
|
@@ -343,7 +398,10 @@ const ExhaustiveDepsUtils = {
|
|
|
343
398
|
return externalRefs;
|
|
344
399
|
},
|
|
345
400
|
/**
|
|
346
|
-
* Recursively collects type identifiers from a type annotation
|
|
401
|
+
* Recursively collects type identifiers from a type annotation.
|
|
402
|
+
* @param typeNode - The type to collect from: type references, unions, intersections, arrays, and
|
|
403
|
+
* tuples are handled.
|
|
404
|
+
* @param out - The set the type names are added to.
|
|
347
405
|
*/
|
|
348
406
|
collectTypeIdentifiers(typeNode, out) {
|
|
349
407
|
switch (typeNode.type) {
|
|
@@ -363,6 +421,8 @@ const ExhaustiveDepsUtils = {
|
|
|
363
421
|
/**
|
|
364
422
|
* Gets the function expression nodes from a queryFn property, handling conditional expressions.
|
|
365
423
|
* When neither branch is skipToken, returns both branches so all deps are scanned.
|
|
424
|
+
* @param queryFn - The `queryFn` property.
|
|
425
|
+
* @returns The nodes to scan for dependencies.
|
|
366
426
|
*/
|
|
367
427
|
getQueryFnNodes(queryFn) {
|
|
368
428
|
if (queryFn.value.type !== _typescript_eslint_utils.AST_NODE_TYPES.ConditionalExpression) return [queryFn.value];
|
|
@@ -480,6 +540,11 @@ const rule$7 = _typescript_eslint_utils.ESLintUtils.RuleCreator(getDocsUrl)({
|
|
|
480
540
|
} };
|
|
481
541
|
})
|
|
482
542
|
});
|
|
543
|
+
/**
|
|
544
|
+
* Builds the suggestion that adds the missing dependencies to an array query key.
|
|
545
|
+
* @param params - The query key node, the missing paths and their text, and the source code.
|
|
546
|
+
* @returns The suggestion, or none if the query key is not an array literal.
|
|
547
|
+
*/
|
|
483
548
|
function buildSuggestions(params) {
|
|
484
549
|
const { queryKeyNode, missingPaths, missingAsText, sourceCode } = params;
|
|
485
550
|
if (queryKeyNode.type !== _typescript_eslint_utils.AST_NODE_TYPES.ArrayExpression) return [];
|
|
@@ -498,6 +563,13 @@ function buildSuggestions(params) {
|
|
|
498
563
|
fix: (fixer) => fixer.insertTextBefore(closingBracket, `${separator}${missingAsText}`)
|
|
499
564
|
}];
|
|
500
565
|
}
|
|
566
|
+
/**
|
|
567
|
+
* Follows variable references and `as` assertions from a query key node to the expression it comes
|
|
568
|
+
* from.
|
|
569
|
+
* @param queryKeyNode - The query key node to start from.
|
|
570
|
+
* @param context - The rule context, used to resolve variables.
|
|
571
|
+
* @returns The innermost node that is neither a resolvable variable nor an `as` assertion.
|
|
572
|
+
*/
|
|
501
573
|
function dereferenceVariablesAndTypeAssertions(queryKeyNode, context) {
|
|
502
574
|
const visitedNodes = /* @__PURE__ */ new Set();
|
|
503
575
|
for (let i = 0; i < 256; ++i) {
|
|
@@ -576,6 +648,11 @@ const QUERY_RESULT_TYPE_NAMES = /* @__PURE__ */ new Set([
|
|
|
576
648
|
"QueryObserverResult",
|
|
577
649
|
"InfiniteQueryObserverResult"
|
|
578
650
|
]);
|
|
651
|
+
/**
|
|
652
|
+
* Checks whether a type is one of the query result types, or a union that contains one.
|
|
653
|
+
* @param type - The type to check.
|
|
654
|
+
* @returns `true` if the type's alias or symbol name is a known query result type.
|
|
655
|
+
*/
|
|
579
656
|
function isQueryResultType(type) {
|
|
580
657
|
if (type.aliasSymbol && QUERY_RESULT_TYPE_NAMES.has(type.aliasSymbol.name)) return true;
|
|
581
658
|
const symbol = type.getSymbol();
|
|
@@ -696,12 +773,25 @@ const rule$4 = _typescript_eslint_utils.ESLintUtils.RuleCreator(getDocsUrl)({
|
|
|
696
773
|
const hookAliasMap = Object.create(null);
|
|
697
774
|
const pendingVariableDeclarators = [];
|
|
698
775
|
const pendingDependencyChecks = [];
|
|
776
|
+
/**
|
|
777
|
+
* Returns the name of the React hook a call expression calls, if it is one: a known hook, an
|
|
778
|
+
* alias of one, or `React.<hook>`.
|
|
779
|
+
* @param node - The call expression.
|
|
780
|
+
* @returns The hook name, or `undefined` if the call is not a React hook.
|
|
781
|
+
*/
|
|
699
782
|
function getReactHook(node) {
|
|
700
783
|
if (node.callee.type === "Identifier") {
|
|
701
784
|
const calleeName = node.callee.name;
|
|
702
785
|
if (reactHookNames.includes(calleeName) || calleeName in hookAliasMap) return calleeName;
|
|
703
786
|
} 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)) return node.callee.property.name;
|
|
704
787
|
}
|
|
788
|
+
/**
|
|
789
|
+
* Tracks the variables bound to a query hook's result, so they can be reported when used as
|
|
790
|
+
* dependencies.
|
|
791
|
+
* @param pattern - The binding: an identifier, or an array pattern whose elements and rest
|
|
792
|
+
* element are tracked.
|
|
793
|
+
* @param queryHook - The name of the query hook the result comes from.
|
|
794
|
+
*/
|
|
705
795
|
function collectVariableNames(pattern, queryHook) {
|
|
706
796
|
if (pattern.type === _typescript_eslint_utils.AST_NODE_TYPES.Identifier) trackedVariables[pattern.name] = queryHook;
|
|
707
797
|
else if (pattern.type === _typescript_eslint_utils.AST_NODE_TYPES.ArrayPattern) for (const element of pattern.elements) {
|
|
@@ -710,25 +800,53 @@ const rule$4 = _typescript_eslint_utils.ESLintUtils.RuleCreator(getDocsUrl)({
|
|
|
710
800
|
else if (element.type === _typescript_eslint_utils.AST_NODE_TYPES.RestElement && element.argument.type === _typescript_eslint_utils.AST_NODE_TYPES.Identifier) trackedVariables[element.argument.name] = queryHook;
|
|
711
801
|
}
|
|
712
802
|
}
|
|
803
|
+
/**
|
|
804
|
+
* Checks whether a name follows the custom hook naming convention.
|
|
805
|
+
* @param hookName - The name to check.
|
|
806
|
+
* @returns `true` if the name starts with `use` followed by an uppercase letter or a digit.
|
|
807
|
+
*/
|
|
713
808
|
function isCustomHookName(hookName) {
|
|
714
809
|
return /^use[A-Z0-9]/.test(hookName);
|
|
715
810
|
}
|
|
811
|
+
/**
|
|
812
|
+
* Checks whether a call passes an object literal with a `combine` property as its first argument.
|
|
813
|
+
* @param callExpression - The call expression to check.
|
|
814
|
+
* @returns `true` if the first argument has a `combine` property.
|
|
815
|
+
*/
|
|
716
816
|
function hasCombineProperty(callExpression) {
|
|
717
817
|
if (callExpression.arguments.length === 0) return false;
|
|
718
818
|
const firstArg = callExpression.arguments[0];
|
|
719
819
|
if (!firstArg || firstArg.type !== _typescript_eslint_utils.AST_NODE_TYPES.ObjectExpression) return false;
|
|
720
820
|
return firstArg.properties.some((prop) => prop.type === _typescript_eslint_utils.AST_NODE_TYPES.Property && prop.key.type === _typescript_eslint_utils.AST_NODE_TYPES.Identifier && prop.key.name === "combine");
|
|
721
821
|
}
|
|
822
|
+
/**
|
|
823
|
+
* Returns the name of the TanStack Query hook a call expression calls directly. `useQueries` and
|
|
824
|
+
* `useSuspenseQueries` with `combine` are ignored, since their result can be stable.
|
|
825
|
+
* @param callExpression - The call to check.
|
|
826
|
+
* @returns The hook name, or `undefined` if the call is not a tracked query hook.
|
|
827
|
+
*/
|
|
722
828
|
function getDirectQueryHook(callExpression) {
|
|
723
829
|
if (callExpression.callee.type !== _typescript_eslint_utils.AST_NODE_TYPES.Identifier || !allHookNames.includes(callExpression.callee.name) || !helpers.isTanstackQueryImport(callExpression.callee)) return;
|
|
724
830
|
if ((callExpression.callee.name === "useQueries" || callExpression.callee.name === "useSuspenseQueries") && hasCombineProperty(callExpression)) return;
|
|
725
831
|
return callExpression.callee.name;
|
|
726
832
|
}
|
|
833
|
+
/**
|
|
834
|
+
* Returns the query hook behind a call expression: a direct query hook call, or a custom hook that
|
|
835
|
+
* returns one.
|
|
836
|
+
* @param callExpression - The call to check.
|
|
837
|
+
* @returns The query hook name, or `undefined` if there is none.
|
|
838
|
+
*/
|
|
727
839
|
function getTrackedQueryHook(callExpression) {
|
|
728
840
|
const directQueryHook = getDirectQueryHook(callExpression);
|
|
729
841
|
if (directQueryHook !== void 0) return directQueryHook;
|
|
730
842
|
if (callExpression.callee.type === _typescript_eslint_utils.AST_NODE_TYPES.Identifier) return trackedCustomHooks[callExpression.callee.name];
|
|
731
843
|
}
|
|
844
|
+
/**
|
|
845
|
+
* Returns the query hook that a custom hook's body returns: either an expression body that calls
|
|
846
|
+
* one, or a block with a single `return` of such a call.
|
|
847
|
+
* @param body - The body of the custom hook.
|
|
848
|
+
* @returns The query hook name, or `undefined` if the body doesn't return one.
|
|
849
|
+
*/
|
|
732
850
|
function getReturnedQueryHook(body) {
|
|
733
851
|
if (body.type === _typescript_eslint_utils.AST_NODE_TYPES.CallExpression) return getDirectQueryHook(body);
|
|
734
852
|
if (body.type !== _typescript_eslint_utils.AST_NODE_TYPES.BlockStatement) return;
|
|
@@ -737,6 +855,11 @@ const rule$4 = _typescript_eslint_utils.ESLintUtils.RuleCreator(getDocsUrl)({
|
|
|
737
855
|
const returnArgument = returnStatements[0]?.argument;
|
|
738
856
|
if (returnArgument?.type === _typescript_eslint_utils.AST_NODE_TYPES.CallExpression) return getDirectQueryHook(returnArgument);
|
|
739
857
|
}
|
|
858
|
+
/**
|
|
859
|
+
* Reports the tracked query results used in a React hook's dependency array.
|
|
860
|
+
* @param reactHook - The name of the React hook.
|
|
861
|
+
* @param depsArray - The dependency array to check.
|
|
862
|
+
*/
|
|
740
863
|
function checkDependencyArray(reactHook, depsArray) {
|
|
741
864
|
depsArray.elements.forEach((dep) => {
|
|
742
865
|
if (dep !== null && dep.type === _typescript_eslint_utils.AST_NODE_TYPES.Identifier && trackedVariables[dep.name] !== void 0) {
|
|
@@ -792,6 +915,15 @@ const rule$4 = _typescript_eslint_utils.ESLintUtils.RuleCreator(getDocsUrl)({
|
|
|
792
915
|
});
|
|
793
916
|
//#endregion
|
|
794
917
|
//#region src/utils/sort-data-by-order.ts
|
|
918
|
+
/**
|
|
919
|
+
* Sorts the items covered by the order rules into the required order, keeping the other items in
|
|
920
|
+
* place.
|
|
921
|
+
* @param data - The items to sort.
|
|
922
|
+
* @param orderRules - Pairs of groups: items whose `key` is in the first group must come before
|
|
923
|
+
* those in the second.
|
|
924
|
+
* @param key - The property of each item to compare against the groups.
|
|
925
|
+
* @returns The sorted items, or `null` if they were already in order.
|
|
926
|
+
*/
|
|
795
927
|
function sortDataByOrder(data, orderRules, key) {
|
|
796
928
|
const getSubsetIndex = (item, subsets) => {
|
|
797
929
|
for (let i = 0; i < subsets.length; i++) if (subsets[i]?.includes(item)) return i;
|
|
@@ -825,8 +957,22 @@ function sortDataByOrder(data, orderRules, key) {
|
|
|
825
957
|
//#endregion
|
|
826
958
|
//#region src/utils/create-property-order-rule.ts
|
|
827
959
|
const createRule$2 = _typescript_eslint_utils.ESLintUtils.RuleCreator(getDocsUrl);
|
|
960
|
+
/**
|
|
961
|
+
* Creates a rule that reports when the properties of the options object passed to the target
|
|
962
|
+
* functions are not in the required order.
|
|
963
|
+
* @param options - The rule's metadata, such as `name`, `meta`, and `defaultOptions`.
|
|
964
|
+
* @param targetFunctions - The names of the functions whose first argument is checked.
|
|
965
|
+
* @param orderRules - Pairs of property groups: the properties of the first group must come before
|
|
966
|
+
* those of the second.
|
|
967
|
+
* @returns An ESLint rule that reports out-of-order properties and fixes them.
|
|
968
|
+
*/
|
|
828
969
|
function createPropertyOrderRule(options, targetFunctions, orderRules) {
|
|
829
970
|
const targetFunctionSet = new Set(targetFunctions);
|
|
971
|
+
/**
|
|
972
|
+
* Checks whether a function name is one of the target functions.
|
|
973
|
+
* @param node - The function name.
|
|
974
|
+
* @returns `true` if the name is a target function.
|
|
975
|
+
*/
|
|
830
976
|
function isTargetFunction(node) {
|
|
831
977
|
return targetFunctionSet.has(node);
|
|
832
978
|
}
|
|
@@ -930,6 +1076,13 @@ const rule$2 = _typescript_eslint_utils.ESLintUtils.RuleCreator(getDocsUrl)({
|
|
|
930
1076
|
} };
|
|
931
1077
|
})
|
|
932
1078
|
});
|
|
1079
|
+
/**
|
|
1080
|
+
* Checks whether a query function's return type resolves to `void` or `undefined`, including in any
|
|
1081
|
+
* member of a union.
|
|
1082
|
+
* @param checker - The TypeScript type checker.
|
|
1083
|
+
* @param type - The return type of the query function.
|
|
1084
|
+
* @returns `true` if the awaited type, or a member of it, is `void` or `undefined`.
|
|
1085
|
+
*/
|
|
933
1086
|
function isIllegalReturn(checker, type) {
|
|
934
1087
|
const awaited = checker.getAwaitedType(type);
|
|
935
1088
|
if (!awaited) return false;
|
|
@@ -1014,12 +1167,20 @@ const rule = _typescript_eslint_utils.ESLintUtils.RuleCreator(getDocsUrl)({
|
|
|
1014
1167
|
},
|
|
1015
1168
|
defaultOptions: [],
|
|
1016
1169
|
create: detectTanstackQueryImports((context, _, helpers) => {
|
|
1170
|
+
/**
|
|
1171
|
+
* Reports an object literal passed as query options that defines `queryKey` or `queryFn` inline.
|
|
1172
|
+
* @param node - The argument passed as query options.
|
|
1173
|
+
*/
|
|
1017
1174
|
function reportInlineQueryOptions(node) {
|
|
1018
1175
|
if (ASTUtils.isObjectExpression(node) && hasInlineQueryOptions(node)) context.report({
|
|
1019
1176
|
node,
|
|
1020
1177
|
messageId: "preferQueryOptions"
|
|
1021
1178
|
});
|
|
1022
1179
|
}
|
|
1180
|
+
/**
|
|
1181
|
+
* Reports an object literal passed as query filters whose `queryKey` is an inline array.
|
|
1182
|
+
* @param node - The filters node.
|
|
1183
|
+
*/
|
|
1023
1184
|
function reportInlineFilterQueryKey(node) {
|
|
1024
1185
|
if (ASTUtils.isObjectExpression(node) && hasInlineFilterQueryKey(node)) context.report({
|
|
1025
1186
|
node,
|
|
@@ -1066,16 +1227,37 @@ const rule = _typescript_eslint_utils.ESLintUtils.RuleCreator(getDocsUrl)({
|
|
|
1066
1227
|
} };
|
|
1067
1228
|
})
|
|
1068
1229
|
});
|
|
1230
|
+
/**
|
|
1231
|
+
* Checks whether an object literal defines `queryKey` or `queryFn` inline.
|
|
1232
|
+
* @param node - The object literal.
|
|
1233
|
+
* @returns `true` if it has a `queryKey` or `queryFn` property.
|
|
1234
|
+
*/
|
|
1069
1235
|
function hasInlineQueryOptions(node) {
|
|
1070
1236
|
return ASTUtils.findPropertyWithIdentifierKey(node.properties, "queryKey") !== void 0 || ASTUtils.findPropertyWithIdentifierKey(node.properties, "queryFn") !== void 0;
|
|
1071
1237
|
}
|
|
1238
|
+
/**
|
|
1239
|
+
* Checks whether an object literal has a `queryKey` written as an inline array.
|
|
1240
|
+
* @param node - The object literal.
|
|
1241
|
+
* @returns `true` if its `queryKey` is an array literal, ignoring type assertions.
|
|
1242
|
+
*/
|
|
1072
1243
|
function hasInlineFilterQueryKey(node) {
|
|
1073
1244
|
const queryKey = ASTUtils.findPropertyWithIdentifierKey(node.properties, "queryKey")?.value;
|
|
1074
1245
|
return queryKey !== void 0 && isInlineArrayExpression(queryKey);
|
|
1075
1246
|
}
|
|
1247
|
+
/**
|
|
1248
|
+
* Checks whether a node is an array literal, ignoring type assertions.
|
|
1249
|
+
* @param node - The node to check.
|
|
1250
|
+
* @returns `true` if the node is an array literal.
|
|
1251
|
+
*/
|
|
1076
1252
|
function isInlineArrayExpression(node) {
|
|
1077
1253
|
return unwrapTypeAssertions(node).type === _typescript_eslint_utils.AST_NODE_TYPES.ArrayExpression;
|
|
1078
1254
|
}
|
|
1255
|
+
/**
|
|
1256
|
+
* Collects the object literals a node can evaluate to, following function bodies, `return`
|
|
1257
|
+
* statements, and conditional, logical, and sequence expressions.
|
|
1258
|
+
* @param node - The node to collect from.
|
|
1259
|
+
* @returns The object literals the node can evaluate to.
|
|
1260
|
+
*/
|
|
1079
1261
|
function getReturnedObjectExpressions(node) {
|
|
1080
1262
|
if (ASTUtils.isObjectExpression(node)) return [node];
|
|
1081
1263
|
if (node.type === _typescript_eslint_utils.AST_NODE_TYPES.ArrowFunctionExpression || node.type === _typescript_eslint_utils.AST_NODE_TYPES.FunctionExpression) return getReturnedObjectExpressions(node.body);
|
|
@@ -1088,6 +1270,12 @@ function getReturnedObjectExpressions(node) {
|
|
|
1088
1270
|
if (node.type === _typescript_eslint_utils.AST_NODE_TYPES.SequenceExpression) return node.expressions.flatMap((expression) => getReturnedObjectExpressions(expression));
|
|
1089
1271
|
return [];
|
|
1090
1272
|
}
|
|
1273
|
+
/**
|
|
1274
|
+
* Collects the query option objects of a `queries` array: the object literals in an array literal,
|
|
1275
|
+
* or those returned by the callback of a `.map()` call.
|
|
1276
|
+
* @param node - The `queries` value.
|
|
1277
|
+
* @returns The query option object literals.
|
|
1278
|
+
*/
|
|
1091
1279
|
function getQueryObjects(node) {
|
|
1092
1280
|
if (node.type === _typescript_eslint_utils.AST_NODE_TYPES.ArrayExpression) return node.elements.flatMap((element) => {
|
|
1093
1281
|
if (element !== null && ASTUtils.isObjectExpression(element)) return [element];
|
|
@@ -1099,18 +1287,40 @@ function getQueryObjects(node) {
|
|
|
1099
1287
|
}
|
|
1100
1288
|
return [];
|
|
1101
1289
|
}
|
|
1290
|
+
/**
|
|
1291
|
+
* Checks whether a node refers to a TanStack Query client, created with `useQueryClient()` or
|
|
1292
|
+
* `new QueryClient()`.
|
|
1293
|
+
* @param node - The node to check.
|
|
1294
|
+
* @param context - The rule context, used to resolve variables.
|
|
1295
|
+
* @param helpers - The helpers that check TanStack Query imports.
|
|
1296
|
+
* @returns `true` if the node comes from `useQueryClient()` or `new QueryClient()`.
|
|
1297
|
+
*/
|
|
1102
1298
|
function isTanstackQueryClient(node, context, helpers) {
|
|
1103
1299
|
const source = resolveQueryClientSource(node, context);
|
|
1104
1300
|
if (source.type === _typescript_eslint_utils.AST_NODE_TYPES.CallExpression && ASTUtils.isIdentifier(source.callee)) return getTanstackImportName(context, helpers, source.callee) === "useQueryClient";
|
|
1105
1301
|
if (source.type === _typescript_eslint_utils.AST_NODE_TYPES.NewExpression && ASTUtils.isIdentifier(source.callee)) return getTanstackImportName(context, helpers, source.callee) === "QueryClient";
|
|
1106
1302
|
return false;
|
|
1107
1303
|
}
|
|
1304
|
+
/**
|
|
1305
|
+
* Returns the name an identifier was imported under from TanStack Query.
|
|
1306
|
+
* @param context - The rule context.
|
|
1307
|
+
* @param helpers - The helpers that check TanStack Query imports.
|
|
1308
|
+
* @param node - The identifier.
|
|
1309
|
+
* @returns The imported name, or `null` if the identifier isn't a named TanStack Query import.
|
|
1310
|
+
*/
|
|
1108
1311
|
function getTanstackImportName(context, helpers, node) {
|
|
1109
1312
|
if (!helpers.isTanstackQueryImport(node)) return null;
|
|
1110
1313
|
const definition = context.sourceCode.getScope(node).references.find((reference) => reference.identifier === node)?.resolved?.defs[0]?.node;
|
|
1111
1314
|
if (definition?.type !== _typescript_eslint_utils.AST_NODE_TYPES.ImportSpecifier || definition.imported.type !== _typescript_eslint_utils.AST_NODE_TYPES.Identifier) return null;
|
|
1112
1315
|
return definition.imported.name;
|
|
1113
1316
|
}
|
|
1317
|
+
/**
|
|
1318
|
+
* Follows variable references, optional chains, and type assertions from a node to the expression it
|
|
1319
|
+
* comes from.
|
|
1320
|
+
* @param node - The node to start from.
|
|
1321
|
+
* @param context - The rule context, used to resolve variables.
|
|
1322
|
+
* @returns The innermost node that can't be resolved further.
|
|
1323
|
+
*/
|
|
1114
1324
|
function resolveQueryClientSource(node, context) {
|
|
1115
1325
|
const visitedNodes = /* @__PURE__ */ new Set();
|
|
1116
1326
|
while (!visitedNodes.has(node)) {
|
|
@@ -1130,6 +1340,11 @@ function resolveQueryClientSource(node, context) {
|
|
|
1130
1340
|
}
|
|
1131
1341
|
return node;
|
|
1132
1342
|
}
|
|
1343
|
+
/**
|
|
1344
|
+
* Removes `as`, `satisfies`, and angle-bracket type assertions around a node.
|
|
1345
|
+
* @param node - The node to unwrap.
|
|
1346
|
+
* @returns The node inside the type assertions.
|
|
1347
|
+
*/
|
|
1133
1348
|
function unwrapTypeAssertions(node) {
|
|
1134
1349
|
while (node.type === _typescript_eslint_utils.AST_NODE_TYPES.TSAsExpression || node.type === _typescript_eslint_utils.AST_NODE_TYPES.TSSatisfiesExpression || node.type === _typescript_eslint_utils.AST_NODE_TYPES.TSTypeAssertion) node = node.expression;
|
|
1135
1350
|
return node;
|
|
@@ -1155,4 +1370,4 @@ Object.defineProperty(exports, "rules", {
|
|
|
1155
1370
|
});
|
|
1156
1371
|
|
|
1157
1372
|
|
|
1158
|
-
//# sourceMappingURL=rules-
|
|
1373
|
+
//# sourceMappingURL=rules-D_RuMaj7.cjs.map
|