@tanstack/eslint-plugin-query 5.104.1 → 5.104.2
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
|
}
|
|
@@ -97,9 +103,20 @@ const ASTUtils = {
|
|
|
97
103
|
};
|
|
98
104
|
//#endregion
|
|
99
105
|
//#region src/utils/get-docs-url.ts
|
|
106
|
+
/**
|
|
107
|
+
* Builds the URL of a rule's documentation page.
|
|
108
|
+
* @param ruleName - The rule's name, e.g. `exhaustive-deps`.
|
|
109
|
+
* @returns The URL of the rule's page in the ESLint plugin docs.
|
|
110
|
+
*/
|
|
100
111
|
const getDocsUrl = (ruleName) => `https://tanstack.com/query/latest/docs/eslint/${ruleName}`;
|
|
101
112
|
//#endregion
|
|
102
113
|
//#region src/utils/detect-react-query-imports.ts
|
|
114
|
+
/**
|
|
115
|
+
* Wraps a rule's `create` function to track the identifiers imported from TanStack Query packages,
|
|
116
|
+
* and passes helpers that check them.
|
|
117
|
+
* @param create - The rule's `create` function, which also receives the helpers.
|
|
118
|
+
* @returns The wrapped `create` function.
|
|
119
|
+
*/
|
|
103
120
|
function detectTanstackQueryImports(create) {
|
|
104
121
|
return (context, optionsWithDefault) => {
|
|
105
122
|
const tanstackQueryImportSpecifiers = [];
|
|
@@ -172,6 +189,9 @@ const ExhaustiveDepsUtils = {
|
|
|
172
189
|
/**
|
|
173
190
|
* Given required refs and existing queryKey entries, compute missing dependency paths
|
|
174
191
|
* respecting allowlisted variables and types.
|
|
192
|
+
* @param params - The references the query function needs, the allowlisted variables, and the
|
|
193
|
+
* root identifiers and full paths already in the query key.
|
|
194
|
+
* @returns The missing dependency paths, without paths whose root is already missing.
|
|
175
195
|
*/
|
|
176
196
|
computeFilteredMissingPaths(params) {
|
|
177
197
|
const { requiredRefs, allowlistedVariables, existingRootIdentifiers, existingFullPaths } = params;
|
|
@@ -191,21 +211,35 @@ const ExhaustiveDepsUtils = {
|
|
|
191
211
|
},
|
|
192
212
|
/**
|
|
193
213
|
* Extract existing queryKey deps as root identifiers and full member paths.
|
|
214
|
+
* @param params - The source code, the scope manager, and the query key node to scan.
|
|
215
|
+
* @returns The root identifiers that cover all of their members, and every dependency path.
|
|
194
216
|
*/
|
|
195
217
|
collectQueryKeyDeps(params) {
|
|
196
218
|
const { sourceCode, scopeManager, queryKeyNode } = params;
|
|
197
219
|
const roots = /* @__PURE__ */ new Set();
|
|
198
220
|
const paths = /* @__PURE__ */ new Set();
|
|
199
221
|
const visitorKeys = sourceCode.visitorKeys;
|
|
222
|
+
/**
|
|
223
|
+
* Records an identifier that covers all of its members.
|
|
224
|
+
* @param name - The identifier, normalized before it is recorded.
|
|
225
|
+
*/
|
|
200
226
|
function addRoot(name) {
|
|
201
227
|
const cleaned = ExhaustiveDepsUtils.normalizeChain(name);
|
|
202
228
|
roots.add(cleaned);
|
|
203
229
|
paths.add(cleaned);
|
|
204
230
|
}
|
|
231
|
+
/**
|
|
232
|
+
* Records a full member path.
|
|
233
|
+
* @param text - The path, normalized before it is recorded.
|
|
234
|
+
*/
|
|
205
235
|
function addFull(text) {
|
|
206
236
|
const cleaned = ExhaustiveDepsUtils.normalizeChain(text);
|
|
207
237
|
paths.add(cleaned);
|
|
208
238
|
}
|
|
239
|
+
/**
|
|
240
|
+
* Records a computed reference path as a root or a full path.
|
|
241
|
+
* @param refPath - The reference path, or `null` to record nothing.
|
|
242
|
+
*/
|
|
209
243
|
function addRefPath(refPath) {
|
|
210
244
|
if (!refPath) return;
|
|
211
245
|
if (refPath.coversRootMembers) {
|
|
@@ -214,6 +248,10 @@ const ExhaustiveDepsUtils = {
|
|
|
214
248
|
}
|
|
215
249
|
addFull(refPath.path);
|
|
216
250
|
}
|
|
251
|
+
/**
|
|
252
|
+
* Visits every child node of a node.
|
|
253
|
+
* @param node - The node whose children are visited.
|
|
254
|
+
*/
|
|
217
255
|
function visitChildren(node) {
|
|
218
256
|
const keys = visitorKeys[node.type] ?? [];
|
|
219
257
|
for (const key of keys) {
|
|
@@ -225,6 +263,11 @@ const ExhaustiveDepsUtils = {
|
|
|
225
263
|
if (ExhaustiveDepsUtils.isNode(value)) visit(value);
|
|
226
264
|
}
|
|
227
265
|
}
|
|
266
|
+
/**
|
|
267
|
+
* Records the dependencies of a node: identifiers directly, and the external references of
|
|
268
|
+
* functions. Other nodes are visited recursively.
|
|
269
|
+
* @param node - The node to visit.
|
|
270
|
+
*/
|
|
228
271
|
function visit(node) {
|
|
229
272
|
if (!node) return;
|
|
230
273
|
switch (node.type) {
|
|
@@ -276,7 +319,9 @@ const ExhaustiveDepsUtils = {
|
|
|
276
319
|
return typeof value === "object" && value !== null && "type" in value && typeof value.type === "string";
|
|
277
320
|
},
|
|
278
321
|
/**
|
|
279
|
-
* Checks whether the resolved variable is allowlisted by its type annotation
|
|
322
|
+
* Checks whether the resolved variable is allowlisted by its type annotation.
|
|
323
|
+
* @param params - The allowlisted type names, and the variable to check.
|
|
324
|
+
* @returns `true` if a type referenced in the variable's type annotation is allowlisted.
|
|
280
325
|
*/
|
|
281
326
|
variableIsAllowlistedByType(params) {
|
|
282
327
|
const { allowlistedTypes, variable } = params;
|
|
@@ -296,6 +341,8 @@ const ExhaustiveDepsUtils = {
|
|
|
296
341
|
* Normalizes a chain by removing optional chaining operators
|
|
297
342
|
*
|
|
298
343
|
* Example: `a?.b.c!` -> `a.b.c`
|
|
344
|
+
* @param text - The source text of the chain.
|
|
345
|
+
* @returns The chain without optional chaining, non-null assertions, and whitespace.
|
|
299
346
|
*/
|
|
300
347
|
normalizeChain(text) {
|
|
301
348
|
return text.replace(/(?:\?(\.)|!)/g, "$1").replace(/\s+/g, "");
|
|
@@ -304,6 +351,9 @@ const ExhaustiveDepsUtils = {
|
|
|
304
351
|
* Computes the reference path for an identifier
|
|
305
352
|
*
|
|
306
353
|
* Example: `a.b.c!` -> `{ path: 'a.b.c', root: 'a' }`
|
|
354
|
+
* @param params - The identifier, and the source code to read the chain from.
|
|
355
|
+
* @returns The dependency `path`, its `root`, and whether the path is the root itself and covers
|
|
356
|
+
* all of its members. For a method call, the method name is dropped from the path.
|
|
307
357
|
*/
|
|
308
358
|
computeRefPath(params) {
|
|
309
359
|
const { identifier, sourceCode } = params;
|
|
@@ -330,6 +380,11 @@ const ExhaustiveDepsUtils = {
|
|
|
330
380
|
const functionScope = scopeManager.acquire(functionNode);
|
|
331
381
|
if (functionScope === null) return [];
|
|
332
382
|
const externalRefs = [];
|
|
383
|
+
/**
|
|
384
|
+
* Collects the read references of a scope and its child scopes that resolve to variables
|
|
385
|
+
* declared outside the function.
|
|
386
|
+
* @param scope - The scope to collect from.
|
|
387
|
+
*/
|
|
333
388
|
function collect(scope) {
|
|
334
389
|
for (const reference of scope.references) {
|
|
335
390
|
if (!reference.isRead() || reference.resolved === null) continue;
|
|
@@ -350,7 +405,10 @@ const ExhaustiveDepsUtils = {
|
|
|
350
405
|
return externalRefs;
|
|
351
406
|
},
|
|
352
407
|
/**
|
|
353
|
-
* Recursively collects type identifiers from a type annotation
|
|
408
|
+
* Recursively collects type identifiers from a type annotation.
|
|
409
|
+
* @param typeNode - The type to collect from: type references, unions, intersections, arrays, and
|
|
410
|
+
* tuples are handled.
|
|
411
|
+
* @param out - The set the type names are added to.
|
|
354
412
|
*/
|
|
355
413
|
collectTypeIdentifiers(typeNode, out) {
|
|
356
414
|
switch (typeNode.type) {
|
|
@@ -370,6 +428,8 @@ const ExhaustiveDepsUtils = {
|
|
|
370
428
|
/**
|
|
371
429
|
* Gets the function expression nodes from a queryFn property, handling conditional expressions.
|
|
372
430
|
* When neither branch is skipToken, returns both branches so all deps are scanned.
|
|
431
|
+
* @param queryFn - The `queryFn` property.
|
|
432
|
+
* @returns The nodes to scan for dependencies.
|
|
373
433
|
*/
|
|
374
434
|
getQueryFnNodes(queryFn) {
|
|
375
435
|
if (queryFn.value.type !== _typescript_eslint_utils.AST_NODE_TYPES.ConditionalExpression) return [queryFn.value];
|
|
@@ -488,6 +548,11 @@ const rule$7 = _typescript_eslint_utils.ESLintUtils.RuleCreator(getDocsUrl)({
|
|
|
488
548
|
} };
|
|
489
549
|
})
|
|
490
550
|
});
|
|
551
|
+
/**
|
|
552
|
+
* Builds the suggestion that adds the missing dependencies to an array query key.
|
|
553
|
+
* @param params - The query key node, the missing paths and their text, and the source code.
|
|
554
|
+
* @returns The suggestion, or none if the query key is not an array literal.
|
|
555
|
+
*/
|
|
491
556
|
function buildSuggestions(params) {
|
|
492
557
|
const { queryKeyNode, missingPaths, missingAsText, sourceCode } = params;
|
|
493
558
|
if (queryKeyNode.type !== _typescript_eslint_utils.AST_NODE_TYPES.ArrayExpression) return [];
|
|
@@ -507,6 +572,13 @@ function buildSuggestions(params) {
|
|
|
507
572
|
fix: (fixer) => fixer.insertTextBefore(closingBracket, `${separator}${missingAsText}`)
|
|
508
573
|
}];
|
|
509
574
|
}
|
|
575
|
+
/**
|
|
576
|
+
* Follows variable references and `as` assertions from a query key node to the expression it comes
|
|
577
|
+
* from.
|
|
578
|
+
* @param queryKeyNode - The query key node to start from.
|
|
579
|
+
* @param context - The rule context, used to resolve variables.
|
|
580
|
+
* @returns The innermost node that is neither a resolvable variable nor an `as` assertion.
|
|
581
|
+
*/
|
|
510
582
|
function dereferenceVariablesAndTypeAssertions(queryKeyNode, context) {
|
|
511
583
|
const visitedNodes = /* @__PURE__ */ new Set();
|
|
512
584
|
for (let i = 0; i < 256; ++i) {
|
|
@@ -585,6 +657,11 @@ const QUERY_RESULT_TYPE_NAMES = /* @__PURE__ */ new Set([
|
|
|
585
657
|
"QueryObserverResult",
|
|
586
658
|
"InfiniteQueryObserverResult"
|
|
587
659
|
]);
|
|
660
|
+
/**
|
|
661
|
+
* Checks whether a type is one of the query result types, or a union that contains one.
|
|
662
|
+
* @param type - The type to check.
|
|
663
|
+
* @returns `true` if the type's alias or symbol name is a known query result type.
|
|
664
|
+
*/
|
|
588
665
|
function isQueryResultType(type) {
|
|
589
666
|
if (type.aliasSymbol && QUERY_RESULT_TYPE_NAMES.has(type.aliasSymbol.name)) return true;
|
|
590
667
|
const symbol = type.getSymbol();
|
|
@@ -706,12 +783,25 @@ const rule$4 = _typescript_eslint_utils.ESLintUtils.RuleCreator(getDocsUrl)({
|
|
|
706
783
|
const hookAliasMap = Object.create(null);
|
|
707
784
|
const pendingVariableDeclarators = [];
|
|
708
785
|
const pendingDependencyChecks = [];
|
|
786
|
+
/**
|
|
787
|
+
* Returns the name of the React hook a call expression calls, if it is one: a known hook, an
|
|
788
|
+
* alias of one, or `React.<hook>`.
|
|
789
|
+
* @param node - The call expression.
|
|
790
|
+
* @returns The hook name, or `undefined` if the call is not a React hook.
|
|
791
|
+
*/
|
|
709
792
|
function getReactHook(node) {
|
|
710
793
|
if (node.callee.type === "Identifier") {
|
|
711
794
|
const calleeName = node.callee.name;
|
|
712
795
|
if (reactHookNames.includes(calleeName) || calleeName in hookAliasMap) return calleeName;
|
|
713
796
|
} 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;
|
|
714
797
|
}
|
|
798
|
+
/**
|
|
799
|
+
* Tracks the variables bound to a query hook's result, so they can be reported when used as
|
|
800
|
+
* dependencies.
|
|
801
|
+
* @param pattern - The binding: an identifier, or an array pattern whose elements and rest
|
|
802
|
+
* element are tracked.
|
|
803
|
+
* @param queryHook - The name of the query hook the result comes from.
|
|
804
|
+
*/
|
|
715
805
|
function collectVariableNames(pattern, queryHook) {
|
|
716
806
|
if (pattern.type === _typescript_eslint_utils.AST_NODE_TYPES.Identifier) trackedVariables[pattern.name] = queryHook;
|
|
717
807
|
else if (pattern.type === _typescript_eslint_utils.AST_NODE_TYPES.ArrayPattern) for (const element of pattern.elements) {
|
|
@@ -720,25 +810,53 @@ const rule$4 = _typescript_eslint_utils.ESLintUtils.RuleCreator(getDocsUrl)({
|
|
|
720
810
|
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;
|
|
721
811
|
}
|
|
722
812
|
}
|
|
813
|
+
/**
|
|
814
|
+
* Checks whether a name follows the custom hook naming convention.
|
|
815
|
+
* @param hookName - The name to check.
|
|
816
|
+
* @returns `true` if the name starts with `use` followed by an uppercase letter or a digit.
|
|
817
|
+
*/
|
|
723
818
|
function isCustomHookName(hookName) {
|
|
724
819
|
return /^use[A-Z0-9]/.test(hookName);
|
|
725
820
|
}
|
|
821
|
+
/**
|
|
822
|
+
* Checks whether a call passes an object literal with a `combine` property as its first argument.
|
|
823
|
+
* @param callExpression - The call expression to check.
|
|
824
|
+
* @returns `true` if the first argument has a `combine` property.
|
|
825
|
+
*/
|
|
726
826
|
function hasCombineProperty(callExpression) {
|
|
727
827
|
if (callExpression.arguments.length === 0) return false;
|
|
728
828
|
const firstArg = callExpression.arguments[0];
|
|
729
829
|
if (!firstArg || firstArg.type !== _typescript_eslint_utils.AST_NODE_TYPES.ObjectExpression) return false;
|
|
730
830
|
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");
|
|
731
831
|
}
|
|
832
|
+
/**
|
|
833
|
+
* Returns the name of the TanStack Query hook a call expression calls directly. `useQueries` and
|
|
834
|
+
* `useSuspenseQueries` with `combine` are ignored, since their result can be stable.
|
|
835
|
+
* @param callExpression - The call to check.
|
|
836
|
+
* @returns The hook name, or `undefined` if the call is not a tracked query hook.
|
|
837
|
+
*/
|
|
732
838
|
function getDirectQueryHook(callExpression) {
|
|
733
839
|
if (callExpression.callee.type !== _typescript_eslint_utils.AST_NODE_TYPES.Identifier || !allHookNames.includes(callExpression.callee.name) || !helpers.isTanstackQueryImport(callExpression.callee)) return;
|
|
734
840
|
if ((callExpression.callee.name === "useQueries" || callExpression.callee.name === "useSuspenseQueries") && hasCombineProperty(callExpression)) return;
|
|
735
841
|
return callExpression.callee.name;
|
|
736
842
|
}
|
|
843
|
+
/**
|
|
844
|
+
* Returns the query hook behind a call expression: a direct query hook call, or a custom hook that
|
|
845
|
+
* returns one.
|
|
846
|
+
* @param callExpression - The call to check.
|
|
847
|
+
* @returns The query hook name, or `undefined` if there is none.
|
|
848
|
+
*/
|
|
737
849
|
function getTrackedQueryHook(callExpression) {
|
|
738
850
|
const directQueryHook = getDirectQueryHook(callExpression);
|
|
739
851
|
if (directQueryHook !== void 0) return directQueryHook;
|
|
740
852
|
if (callExpression.callee.type === _typescript_eslint_utils.AST_NODE_TYPES.Identifier) return trackedCustomHooks[callExpression.callee.name];
|
|
741
853
|
}
|
|
854
|
+
/**
|
|
855
|
+
* Returns the query hook that a custom hook's body returns: either an expression body that calls
|
|
856
|
+
* one, or a block with a single `return` of such a call.
|
|
857
|
+
* @param body - The body of the custom hook.
|
|
858
|
+
* @returns The query hook name, or `undefined` if the body doesn't return one.
|
|
859
|
+
*/
|
|
742
860
|
function getReturnedQueryHook(body) {
|
|
743
861
|
var _returnStatements$;
|
|
744
862
|
if (body.type === _typescript_eslint_utils.AST_NODE_TYPES.CallExpression) return getDirectQueryHook(body);
|
|
@@ -748,6 +866,11 @@ const rule$4 = _typescript_eslint_utils.ESLintUtils.RuleCreator(getDocsUrl)({
|
|
|
748
866
|
const returnArgument = (_returnStatements$ = returnStatements[0]) === null || _returnStatements$ === void 0 ? void 0 : _returnStatements$.argument;
|
|
749
867
|
if ((returnArgument === null || returnArgument === void 0 ? void 0 : returnArgument.type) === _typescript_eslint_utils.AST_NODE_TYPES.CallExpression) return getDirectQueryHook(returnArgument);
|
|
750
868
|
}
|
|
869
|
+
/**
|
|
870
|
+
* Reports the tracked query results used in a React hook's dependency array.
|
|
871
|
+
* @param reactHook - The name of the React hook.
|
|
872
|
+
* @param depsArray - The dependency array to check.
|
|
873
|
+
*/
|
|
751
874
|
function checkDependencyArray(reactHook, depsArray) {
|
|
752
875
|
depsArray.elements.forEach((dep) => {
|
|
753
876
|
if (dep !== null && dep.type === _typescript_eslint_utils.AST_NODE_TYPES.Identifier && trackedVariables[dep.name] !== void 0) {
|
|
@@ -805,6 +928,15 @@ const rule$4 = _typescript_eslint_utils.ESLintUtils.RuleCreator(getDocsUrl)({
|
|
|
805
928
|
});
|
|
806
929
|
//#endregion
|
|
807
930
|
//#region src/utils/sort-data-by-order.ts
|
|
931
|
+
/**
|
|
932
|
+
* Sorts the items covered by the order rules into the required order, keeping the other items in
|
|
933
|
+
* place.
|
|
934
|
+
* @param data - The items to sort.
|
|
935
|
+
* @param orderRules - Pairs of groups: items whose `key` is in the first group must come before
|
|
936
|
+
* those in the second.
|
|
937
|
+
* @param key - The property of each item to compare against the groups.
|
|
938
|
+
* @returns The sorted items, or `null` if they were already in order.
|
|
939
|
+
*/
|
|
808
940
|
function sortDataByOrder(data, orderRules, key) {
|
|
809
941
|
const getSubsetIndex = (item, subsets) => {
|
|
810
942
|
for (let i = 0; i < subsets.length; i++) {
|
|
@@ -841,8 +973,22 @@ function sortDataByOrder(data, orderRules, key) {
|
|
|
841
973
|
//#endregion
|
|
842
974
|
//#region src/utils/create-property-order-rule.ts
|
|
843
975
|
const createRule$2 = _typescript_eslint_utils.ESLintUtils.RuleCreator(getDocsUrl);
|
|
976
|
+
/**
|
|
977
|
+
* Creates a rule that reports when the properties of the options object passed to the target
|
|
978
|
+
* functions are not in the required order.
|
|
979
|
+
* @param options - The rule's metadata, such as `name`, `meta`, and `defaultOptions`.
|
|
980
|
+
* @param targetFunctions - The names of the functions whose first argument is checked.
|
|
981
|
+
* @param orderRules - Pairs of property groups: the properties of the first group must come before
|
|
982
|
+
* those of the second.
|
|
983
|
+
* @returns An ESLint rule that reports out-of-order properties and fixes them.
|
|
984
|
+
*/
|
|
844
985
|
function createPropertyOrderRule(options, targetFunctions, orderRules) {
|
|
845
986
|
const targetFunctionSet = new Set(targetFunctions);
|
|
987
|
+
/**
|
|
988
|
+
* Checks whether a function name is one of the target functions.
|
|
989
|
+
* @param node - The function name.
|
|
990
|
+
* @returns `true` if the name is a target function.
|
|
991
|
+
*/
|
|
846
992
|
function isTargetFunction(node) {
|
|
847
993
|
return targetFunctionSet.has(node);
|
|
848
994
|
}
|
|
@@ -947,6 +1093,13 @@ const rule$2 = _typescript_eslint_utils.ESLintUtils.RuleCreator(getDocsUrl)({
|
|
|
947
1093
|
} };
|
|
948
1094
|
})
|
|
949
1095
|
});
|
|
1096
|
+
/**
|
|
1097
|
+
* Checks whether a query function's return type resolves to `void` or `undefined`, including in any
|
|
1098
|
+
* member of a union.
|
|
1099
|
+
* @param checker - The TypeScript type checker.
|
|
1100
|
+
* @param type - The return type of the query function.
|
|
1101
|
+
* @returns `true` if the awaited type, or a member of it, is `void` or `undefined`.
|
|
1102
|
+
*/
|
|
950
1103
|
function isIllegalReturn(checker, type) {
|
|
951
1104
|
const awaited = checker.getAwaitedType(type);
|
|
952
1105
|
if (!awaited) return false;
|
|
@@ -1031,12 +1184,20 @@ const rule = _typescript_eslint_utils.ESLintUtils.RuleCreator(getDocsUrl)({
|
|
|
1031
1184
|
},
|
|
1032
1185
|
defaultOptions: [],
|
|
1033
1186
|
create: detectTanstackQueryImports((context, _, helpers) => {
|
|
1187
|
+
/**
|
|
1188
|
+
* Reports an object literal passed as query options that defines `queryKey` or `queryFn` inline.
|
|
1189
|
+
* @param node - The argument passed as query options.
|
|
1190
|
+
*/
|
|
1034
1191
|
function reportInlineQueryOptions(node) {
|
|
1035
1192
|
if (ASTUtils.isObjectExpression(node) && hasInlineQueryOptions(node)) context.report({
|
|
1036
1193
|
node,
|
|
1037
1194
|
messageId: "preferQueryOptions"
|
|
1038
1195
|
});
|
|
1039
1196
|
}
|
|
1197
|
+
/**
|
|
1198
|
+
* Reports an object literal passed as query filters whose `queryKey` is an inline array.
|
|
1199
|
+
* @param node - The filters node.
|
|
1200
|
+
*/
|
|
1040
1201
|
function reportInlineFilterQueryKey(node) {
|
|
1041
1202
|
if (ASTUtils.isObjectExpression(node) && hasInlineFilterQueryKey(node)) context.report({
|
|
1042
1203
|
node,
|
|
@@ -1084,17 +1245,38 @@ const rule = _typescript_eslint_utils.ESLintUtils.RuleCreator(getDocsUrl)({
|
|
|
1084
1245
|
} };
|
|
1085
1246
|
})
|
|
1086
1247
|
});
|
|
1248
|
+
/**
|
|
1249
|
+
* Checks whether an object literal defines `queryKey` or `queryFn` inline.
|
|
1250
|
+
* @param node - The object literal.
|
|
1251
|
+
* @returns `true` if it has a `queryKey` or `queryFn` property.
|
|
1252
|
+
*/
|
|
1087
1253
|
function hasInlineQueryOptions(node) {
|
|
1088
1254
|
return ASTUtils.findPropertyWithIdentifierKey(node.properties, "queryKey") !== void 0 || ASTUtils.findPropertyWithIdentifierKey(node.properties, "queryFn") !== void 0;
|
|
1089
1255
|
}
|
|
1256
|
+
/**
|
|
1257
|
+
* Checks whether an object literal has a `queryKey` written as an inline array.
|
|
1258
|
+
* @param node - The object literal.
|
|
1259
|
+
* @returns `true` if its `queryKey` is an array literal, ignoring type assertions.
|
|
1260
|
+
*/
|
|
1090
1261
|
function hasInlineFilterQueryKey(node) {
|
|
1091
1262
|
var _ASTUtils$findPropert2;
|
|
1092
1263
|
const queryKey = (_ASTUtils$findPropert2 = ASTUtils.findPropertyWithIdentifierKey(node.properties, "queryKey")) === null || _ASTUtils$findPropert2 === void 0 ? void 0 : _ASTUtils$findPropert2.value;
|
|
1093
1264
|
return queryKey !== void 0 && isInlineArrayExpression(queryKey);
|
|
1094
1265
|
}
|
|
1266
|
+
/**
|
|
1267
|
+
* Checks whether a node is an array literal, ignoring type assertions.
|
|
1268
|
+
* @param node - The node to check.
|
|
1269
|
+
* @returns `true` if the node is an array literal.
|
|
1270
|
+
*/
|
|
1095
1271
|
function isInlineArrayExpression(node) {
|
|
1096
1272
|
return unwrapTypeAssertions(node).type === _typescript_eslint_utils.AST_NODE_TYPES.ArrayExpression;
|
|
1097
1273
|
}
|
|
1274
|
+
/**
|
|
1275
|
+
* Collects the object literals a node can evaluate to, following function bodies, `return`
|
|
1276
|
+
* statements, and conditional, logical, and sequence expressions.
|
|
1277
|
+
* @param node - The node to collect from.
|
|
1278
|
+
* @returns The object literals the node can evaluate to.
|
|
1279
|
+
*/
|
|
1098
1280
|
function getReturnedObjectExpressions(node) {
|
|
1099
1281
|
if (ASTUtils.isObjectExpression(node)) return [node];
|
|
1100
1282
|
if (node.type === _typescript_eslint_utils.AST_NODE_TYPES.ArrowFunctionExpression || node.type === _typescript_eslint_utils.AST_NODE_TYPES.FunctionExpression) return getReturnedObjectExpressions(node.body);
|
|
@@ -1107,6 +1289,12 @@ function getReturnedObjectExpressions(node) {
|
|
|
1107
1289
|
if (node.type === _typescript_eslint_utils.AST_NODE_TYPES.SequenceExpression) return node.expressions.flatMap((expression) => getReturnedObjectExpressions(expression));
|
|
1108
1290
|
return [];
|
|
1109
1291
|
}
|
|
1292
|
+
/**
|
|
1293
|
+
* Collects the query option objects of a `queries` array: the object literals in an array literal,
|
|
1294
|
+
* or those returned by the callback of a `.map()` call.
|
|
1295
|
+
* @param node - The `queries` value.
|
|
1296
|
+
* @returns The query option object literals.
|
|
1297
|
+
*/
|
|
1110
1298
|
function getQueryObjects(node) {
|
|
1111
1299
|
if (node.type === _typescript_eslint_utils.AST_NODE_TYPES.ArrayExpression) return node.elements.flatMap((element) => {
|
|
1112
1300
|
if (element !== null && ASTUtils.isObjectExpression(element)) return [element];
|
|
@@ -1118,12 +1306,27 @@ function getQueryObjects(node) {
|
|
|
1118
1306
|
}
|
|
1119
1307
|
return [];
|
|
1120
1308
|
}
|
|
1309
|
+
/**
|
|
1310
|
+
* Checks whether a node refers to a TanStack Query client, created with `useQueryClient()` or
|
|
1311
|
+
* `new QueryClient()`.
|
|
1312
|
+
* @param node - The node to check.
|
|
1313
|
+
* @param context - The rule context, used to resolve variables.
|
|
1314
|
+
* @param helpers - The helpers that check TanStack Query imports.
|
|
1315
|
+
* @returns `true` if the node comes from `useQueryClient()` or `new QueryClient()`.
|
|
1316
|
+
*/
|
|
1121
1317
|
function isTanstackQueryClient(node, context, helpers) {
|
|
1122
1318
|
const source = resolveQueryClientSource(node, context);
|
|
1123
1319
|
if (source.type === _typescript_eslint_utils.AST_NODE_TYPES.CallExpression && ASTUtils.isIdentifier(source.callee)) return getTanstackImportName(context, helpers, source.callee) === "useQueryClient";
|
|
1124
1320
|
if (source.type === _typescript_eslint_utils.AST_NODE_TYPES.NewExpression && ASTUtils.isIdentifier(source.callee)) return getTanstackImportName(context, helpers, source.callee) === "QueryClient";
|
|
1125
1321
|
return false;
|
|
1126
1322
|
}
|
|
1323
|
+
/**
|
|
1324
|
+
* Returns the name an identifier was imported under from TanStack Query.
|
|
1325
|
+
* @param context - The rule context.
|
|
1326
|
+
* @param helpers - The helpers that check TanStack Query imports.
|
|
1327
|
+
* @param node - The identifier.
|
|
1328
|
+
* @returns The imported name, or `null` if the identifier isn't a named TanStack Query import.
|
|
1329
|
+
*/
|
|
1127
1330
|
function getTanstackImportName(context, helpers, node) {
|
|
1128
1331
|
var _context$sourceCode$g;
|
|
1129
1332
|
if (!helpers.isTanstackQueryImport(node)) return null;
|
|
@@ -1131,6 +1334,13 @@ function getTanstackImportName(context, helpers, node) {
|
|
|
1131
1334
|
if ((definition === null || definition === void 0 ? void 0 : definition.type) !== _typescript_eslint_utils.AST_NODE_TYPES.ImportSpecifier || definition.imported.type !== _typescript_eslint_utils.AST_NODE_TYPES.Identifier) return null;
|
|
1132
1335
|
return definition.imported.name;
|
|
1133
1336
|
}
|
|
1337
|
+
/**
|
|
1338
|
+
* Follows variable references, optional chains, and type assertions from a node to the expression it
|
|
1339
|
+
* comes from.
|
|
1340
|
+
* @param node - The node to start from.
|
|
1341
|
+
* @param context - The rule context, used to resolve variables.
|
|
1342
|
+
* @returns The innermost node that can't be resolved further.
|
|
1343
|
+
*/
|
|
1134
1344
|
function resolveQueryClientSource(node, context) {
|
|
1135
1345
|
const visitedNodes = /* @__PURE__ */ new Set();
|
|
1136
1346
|
while (!visitedNodes.has(node)) {
|
|
@@ -1150,6 +1360,11 @@ function resolveQueryClientSource(node, context) {
|
|
|
1150
1360
|
}
|
|
1151
1361
|
return node;
|
|
1152
1362
|
}
|
|
1363
|
+
/**
|
|
1364
|
+
* Removes `as`, `satisfies`, and angle-bracket type assertions around a node.
|
|
1365
|
+
* @param node - The node to unwrap.
|
|
1366
|
+
* @returns The node inside the type assertions.
|
|
1367
|
+
*/
|
|
1153
1368
|
function unwrapTypeAssertions(node) {
|
|
1154
1369
|
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;
|
|
1155
1370
|
return node;
|
|
@@ -1174,4 +1389,4 @@ Object.defineProperty(exports, "rules", {
|
|
|
1174
1389
|
}
|
|
1175
1390
|
});
|
|
1176
1391
|
|
|
1177
|
-
//# sourceMappingURL=rules-
|
|
1392
|
+
//# sourceMappingURL=rules-CzcCSM4O.cjs.map
|