@tanstack/eslint-plugin-query 5.95.2 → 5.96.1

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