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