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