@tanstack/eslint-plugin-query 5.95.1 → 5.96.0

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.
@@ -4,8 +4,8 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
6
  var __export = (target, all) => {
7
- for (var name8 in all)
8
- __defProp(target, name8, { get: all[name8], enumerable: true });
7
+ for (var name9 in all)
8
+ __defProp(target, name9, { get: all[name9], enumerable: true });
9
9
  };
10
10
  var __copyProps = (to, from, except, desc) => {
11
11
  if (from && typeof from === "object" || typeof from === "function") {
@@ -43,11 +43,11 @@ var ASTUtils = {
43
43
  isIdentifier(node) {
44
44
  return node.type === import_utils.AST_NODE_TYPES.Identifier;
45
45
  },
46
- isIdentifierWithName(node, name8) {
47
- return ASTUtils.isIdentifier(node) && node.name === name8;
46
+ isIdentifierWithName(node, name9) {
47
+ return ASTUtils.isIdentifier(node) && node.name === name9;
48
48
  },
49
- isIdentifierWithOneOfNames(node, name8) {
50
- return ASTUtils.isIdentifier(node) && name8.includes(node.name);
49
+ isIdentifierWithOneOfNames(node, name9) {
50
+ return ASTUtils.isIdentifier(node) && name9.includes(node.name);
51
51
  },
52
52
  isProperty(node) {
53
53
  return node.type === import_utils.AST_NODE_TYPES.Property;
@@ -421,8 +421,8 @@ var ExhaustiveDepsUtils = {
421
421
  const roots = /* @__PURE__ */ new Set();
422
422
  const paths = /* @__PURE__ */ new Set();
423
423
  const visitorKeys = sourceCode.visitorKeys;
424
- function addRoot(name8) {
425
- const cleaned = ExhaustiveDepsUtils.normalizeChain(name8);
424
+ function addRoot(name9) {
425
+ const cleaned = ExhaustiveDepsUtils.normalizeChain(name9);
426
426
  roots.add(cleaned);
427
427
  paths.add(cleaned);
428
428
  }
@@ -1353,6 +1353,260 @@ var rule7 = createPropertyOrderRule(
1353
1353
  sortRules2
1354
1354
  );
1355
1355
 
1356
+ // src/rules/prefer-query-options/prefer-query-options.rule.ts
1357
+ var import_utils11 = require("@typescript-eslint/utils");
1358
+ var name8 = "prefer-query-options";
1359
+ var queryHooks2 = [
1360
+ "useQuery",
1361
+ "useInfiniteQuery",
1362
+ "useSuspenseQuery",
1363
+ "useSuspenseInfiniteQuery",
1364
+ "usePrefetchQuery",
1365
+ "usePrefetchInfiniteQuery"
1366
+ ];
1367
+ var queriesHooks = ["useQueries", "useSuspenseQueries"];
1368
+ var filterHooks = ["useIsFetching"];
1369
+ var queryClientOptionMethods = [
1370
+ "fetchQuery",
1371
+ "prefetchQuery",
1372
+ "fetchInfiniteQuery",
1373
+ "prefetchInfiniteQuery",
1374
+ "ensureQueryData",
1375
+ "ensureInfiniteQueryData"
1376
+ ];
1377
+ var queryClientQueryKeyMethods = [
1378
+ "getQueryData",
1379
+ "setQueryData",
1380
+ "getQueryState",
1381
+ "setQueryDefaults",
1382
+ "getQueryDefaults"
1383
+ ];
1384
+ var queryClientFilterMethods = [
1385
+ "invalidateQueries",
1386
+ "cancelQueries",
1387
+ "refetchQueries",
1388
+ "removeQueries",
1389
+ "resetQueries",
1390
+ "isFetching",
1391
+ "getQueriesData",
1392
+ "setQueriesData"
1393
+ ];
1394
+ var queryOptionsBuilders = ["queryOptions", "infiniteQueryOptions"];
1395
+ var createRule7 = import_utils11.ESLintUtils.RuleCreator(getDocsUrl);
1396
+ var rule8 = createRule7({
1397
+ name: name8,
1398
+ meta: {
1399
+ type: "problem",
1400
+ docs: {
1401
+ description: "Prefer using queryOptions() to co-locate queryKey and queryFn",
1402
+ recommended: "strict"
1403
+ },
1404
+ messages: {
1405
+ preferQueryOptions: "Prefer using queryOptions() or infiniteQueryOptions() to co-locate queryKey and queryFn.",
1406
+ preferQueryOptionsQueryKey: "Prefer referencing a queryKey from a queryOptions() result instead of typing it manually."
1407
+ },
1408
+ schema: []
1409
+ },
1410
+ defaultOptions: [],
1411
+ create: detectTanstackQueryImports((context, _, helpers) => {
1412
+ function reportInlineQueryOptions(node) {
1413
+ if (ASTUtils.isObjectExpression(node) && hasInlineQueryOptions(node)) {
1414
+ context.report({
1415
+ node,
1416
+ messageId: "preferQueryOptions"
1417
+ });
1418
+ }
1419
+ }
1420
+ function reportInlineFilterQueryKey(node) {
1421
+ if (ASTUtils.isObjectExpression(node) && hasInlineFilterQueryKey(node)) {
1422
+ context.report({
1423
+ node,
1424
+ messageId: "preferQueryOptionsQueryKey"
1425
+ });
1426
+ }
1427
+ }
1428
+ return {
1429
+ CallExpression: (node) => {
1430
+ var _a;
1431
+ if (ASTUtils.isIdentifier(node.callee)) {
1432
+ const importedName = getTanstackImportName(
1433
+ context,
1434
+ helpers,
1435
+ node.callee
1436
+ );
1437
+ if (importedName === null) {
1438
+ return;
1439
+ }
1440
+ if (queryOptionsBuilders.includes(importedName)) {
1441
+ return;
1442
+ }
1443
+ const options2 = node.arguments[0];
1444
+ if (options2 === void 0) {
1445
+ return;
1446
+ }
1447
+ if (queryHooks2.includes(importedName)) {
1448
+ reportInlineQueryOptions(options2);
1449
+ return;
1450
+ }
1451
+ if (queriesHooks.includes(importedName) && ASTUtils.isObjectExpression(options2)) {
1452
+ const queries = (_a = ASTUtils.findPropertyWithIdentifierKey(
1453
+ options2.properties,
1454
+ "queries"
1455
+ )) == null ? void 0 : _a.value;
1456
+ if (queries !== void 0) {
1457
+ getQueryObjects(queries).forEach((query) => {
1458
+ reportInlineQueryOptions(query);
1459
+ });
1460
+ }
1461
+ return;
1462
+ }
1463
+ if (filterHooks.includes(importedName)) {
1464
+ reportInlineFilterQueryKey(options2);
1465
+ }
1466
+ return;
1467
+ }
1468
+ if (node.callee.type !== import_utils11.AST_NODE_TYPES.MemberExpression || !ASTUtils.isIdentifier(node.callee.property) || !isTanstackQueryClient(node.callee.object, context, helpers)) {
1469
+ return;
1470
+ }
1471
+ const method = node.callee.property.name;
1472
+ const options = node.arguments[0];
1473
+ if (options === void 0) {
1474
+ return;
1475
+ }
1476
+ if (queryClientOptionMethods.includes(method)) {
1477
+ reportInlineQueryOptions(options);
1478
+ return;
1479
+ }
1480
+ if (queryClientQueryKeyMethods.includes(method) && isInlineArrayExpression(options)) {
1481
+ context.report({
1482
+ node: options,
1483
+ messageId: "preferQueryOptionsQueryKey"
1484
+ });
1485
+ return;
1486
+ }
1487
+ if (queryClientFilterMethods.includes(method)) {
1488
+ reportInlineFilterQueryKey(options);
1489
+ }
1490
+ }
1491
+ };
1492
+ })
1493
+ });
1494
+ function hasInlineQueryOptions(node) {
1495
+ return ASTUtils.findPropertyWithIdentifierKey(node.properties, "queryKey") !== void 0 || ASTUtils.findPropertyWithIdentifierKey(node.properties, "queryFn") !== void 0;
1496
+ }
1497
+ function hasInlineFilterQueryKey(node) {
1498
+ var _a;
1499
+ const queryKey = (_a = ASTUtils.findPropertyWithIdentifierKey(
1500
+ node.properties,
1501
+ "queryKey"
1502
+ )) == null ? void 0 : _a.value;
1503
+ return queryKey !== void 0 && isInlineArrayExpression(queryKey);
1504
+ }
1505
+ function isInlineArrayExpression(node) {
1506
+ return unwrapTypeAssertions(node).type === import_utils11.AST_NODE_TYPES.ArrayExpression;
1507
+ }
1508
+ function getReturnedObjectExpressions(node) {
1509
+ if (ASTUtils.isObjectExpression(node)) {
1510
+ return [node];
1511
+ }
1512
+ if (node.type === import_utils11.AST_NODE_TYPES.ArrowFunctionExpression || node.type === import_utils11.AST_NODE_TYPES.FunctionExpression) {
1513
+ return getReturnedObjectExpressions(node.body);
1514
+ }
1515
+ if (node.type === import_utils11.AST_NODE_TYPES.BlockStatement) {
1516
+ return node.body.flatMap((statement) => {
1517
+ if (statement.type === import_utils11.AST_NODE_TYPES.ReturnStatement && statement.argument !== null) {
1518
+ return getReturnedObjectExpressions(statement.argument);
1519
+ }
1520
+ return [];
1521
+ });
1522
+ }
1523
+ if (node.type === import_utils11.AST_NODE_TYPES.ConditionalExpression) {
1524
+ return [
1525
+ ...getReturnedObjectExpressions(node.consequent),
1526
+ ...getReturnedObjectExpressions(node.alternate)
1527
+ ];
1528
+ }
1529
+ if (node.type === import_utils11.AST_NODE_TYPES.LogicalExpression) {
1530
+ return [
1531
+ ...getReturnedObjectExpressions(node.left),
1532
+ ...getReturnedObjectExpressions(node.right)
1533
+ ];
1534
+ }
1535
+ if (node.type === import_utils11.AST_NODE_TYPES.SequenceExpression) {
1536
+ return node.expressions.flatMap(
1537
+ (expression) => getReturnedObjectExpressions(expression)
1538
+ );
1539
+ }
1540
+ return [];
1541
+ }
1542
+ function getQueryObjects(node) {
1543
+ if (node.type === import_utils11.AST_NODE_TYPES.ArrayExpression) {
1544
+ return node.elements.flatMap((element) => {
1545
+ if (element !== null && ASTUtils.isObjectExpression(element)) {
1546
+ return [element];
1547
+ }
1548
+ return [];
1549
+ });
1550
+ }
1551
+ if (node.type === import_utils11.AST_NODE_TYPES.CallExpression && node.callee.type === import_utils11.AST_NODE_TYPES.MemberExpression && ASTUtils.isIdentifierWithName(node.callee.property, "map")) {
1552
+ const mapper = node.arguments[0];
1553
+ if ((mapper == null ? void 0 : mapper.type) === import_utils11.AST_NODE_TYPES.ArrowFunctionExpression || (mapper == null ? void 0 : mapper.type) === import_utils11.AST_NODE_TYPES.FunctionExpression) {
1554
+ return getReturnedObjectExpressions(mapper);
1555
+ }
1556
+ }
1557
+ return [];
1558
+ }
1559
+ function isTanstackQueryClient(node, context, helpers) {
1560
+ const source = resolveQueryClientSource(node, context);
1561
+ if (source.type === import_utils11.AST_NODE_TYPES.CallExpression && ASTUtils.isIdentifier(source.callee)) {
1562
+ return getTanstackImportName(context, helpers, source.callee) === "useQueryClient";
1563
+ }
1564
+ if (source.type === import_utils11.AST_NODE_TYPES.NewExpression && ASTUtils.isIdentifier(source.callee)) {
1565
+ return getTanstackImportName(context, helpers, source.callee) === "QueryClient";
1566
+ }
1567
+ return false;
1568
+ }
1569
+ function getTanstackImportName(context, helpers, node) {
1570
+ var _a, _b, _c;
1571
+ if (!helpers.isTanstackQueryImport(node)) {
1572
+ return null;
1573
+ }
1574
+ const definition = (_c = (_b = (_a = context.sourceCode.getScope(node).references.find((reference) => reference.identifier === node)) == null ? void 0 : _a.resolved) == null ? void 0 : _b.defs[0]) == null ? void 0 : _c.node;
1575
+ if ((definition == null ? void 0 : definition.type) !== import_utils11.AST_NODE_TYPES.ImportSpecifier || definition.imported.type !== import_utils11.AST_NODE_TYPES.Identifier) {
1576
+ return null;
1577
+ }
1578
+ return definition.imported.name;
1579
+ }
1580
+ function resolveQueryClientSource(node, context) {
1581
+ const visitedNodes = /* @__PURE__ */ new Set();
1582
+ while (!visitedNodes.has(node)) {
1583
+ visitedNodes.add(node);
1584
+ if (node.type === import_utils11.AST_NODE_TYPES.ChainExpression) {
1585
+ node = node.expression;
1586
+ continue;
1587
+ }
1588
+ node = unwrapTypeAssertions(node);
1589
+ if (node.type !== import_utils11.AST_NODE_TYPES.Identifier) {
1590
+ return node;
1591
+ }
1592
+ const expression = ASTUtils.getReferencedExpressionByIdentifier({
1593
+ context,
1594
+ node
1595
+ });
1596
+ if (expression === null) {
1597
+ return node;
1598
+ }
1599
+ node = expression;
1600
+ }
1601
+ return node;
1602
+ }
1603
+ function unwrapTypeAssertions(node) {
1604
+ while (node.type === import_utils11.AST_NODE_TYPES.TSAsExpression || node.type === import_utils11.AST_NODE_TYPES.TSSatisfiesExpression || node.type === import_utils11.AST_NODE_TYPES.TSTypeAssertion) {
1605
+ node = node.expression;
1606
+ }
1607
+ return node;
1608
+ }
1609
+
1356
1610
  // src/rules.ts
1357
1611
  var rules = {
1358
1612
  [name]: rule,
@@ -1361,7 +1615,8 @@ var rules = {
1361
1615
  [name4]: rule4,
1362
1616
  [name5]: rule5,
1363
1617
  [name6]: rule6,
1364
- [name7]: rule7
1618
+ [name7]: rule7,
1619
+ [name8]: rule8
1365
1620
  };
1366
1621
  // Annotate the CommonJS export names for ESM import in node:
1367
1622
  0 && (module.exports = {