eslint-plugin-react-x 2.7.0 → 2.7.1-beta.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.
Files changed (2) hide show
  1. package/dist/index.js +70 -71
  2. package/package.json +6 -6
package/dist/index.js CHANGED
@@ -68,7 +68,7 @@ const rules$7 = {
68
68
  //#endregion
69
69
  //#region package.json
70
70
  var name$6 = "eslint-plugin-react-x";
71
- var version = "2.7.0";
71
+ var version = "2.7.1-beta.1";
72
72
 
73
73
  //#endregion
74
74
  //#region src/utils/create-rule.ts
@@ -657,22 +657,22 @@ function create$53(context) {
657
657
  //#region src/rules/no-array-index-key.ts
658
658
  const RULE_NAME$52 = "no-array-index-key";
659
659
  const REACT_CHILDREN_METHOD = ["forEach", "map"];
660
- const arrayIndexParamPosition = new Map([
661
- ["every", 1],
662
- ["filter", 1],
663
- ["find", 1],
664
- ["findIndex", 1],
665
- ["findLast", 1],
666
- ["findLastIndex", 1],
667
- ["flatMap", 1],
668
- ["forEach", 1],
669
- ["map", 1],
670
- ["reduce", 2],
671
- ["reduceRight", 2],
672
- ["some", 1]
673
- ]);
674
- function getArrayIndexParamPosition(methodName) {
675
- return arrayIndexParamPosition.get(methodName) ?? -1;
660
+ function getIndexParamPosition(methodName) {
661
+ switch (methodName) {
662
+ case "every":
663
+ case "filter":
664
+ case "find":
665
+ case "findIndex":
666
+ case "findLast":
667
+ case "findLastIndex":
668
+ case "flatMap":
669
+ case "forEach":
670
+ case "map":
671
+ case "some": return 1;
672
+ case "reduce":
673
+ case "reduceRight": return 2;
674
+ default: return -1;
675
+ }
676
676
  }
677
677
  function isReactChildrenMethod(name$9) {
678
678
  return REACT_CHILDREN_METHOD.includes(name$9);
@@ -692,7 +692,7 @@ function getMapIndexParamName(context, node) {
692
692
  if (callee.type !== AST_NODE_TYPES.MemberExpression) return unit;
693
693
  if (callee.property.type !== AST_NODE_TYPES.Identifier) return unit;
694
694
  const { name: name$9 } = callee.property;
695
- const indexPosition = getArrayIndexParamPosition(name$9);
695
+ const indexPosition = getIndexParamPosition(name$9);
696
696
  if (indexPosition === -1) return unit;
697
697
  const callbackArg = node.arguments[isUsingReactChildren(context, node) ? 1 : 0];
698
698
  if (callbackArg == null) return unit;
@@ -1248,7 +1248,7 @@ function create$36(context) {
1248
1248
  break;
1249
1249
  }
1250
1250
  default: {
1251
- const call = AST.findParentNode(jsxElement, AST.isArrayMapCall);
1251
+ const call = AST.findParentNode(jsxElement, (n) => n.type === AST_NODE_TYPES.CallExpression && n.callee.type === AST_NODE_TYPES.MemberExpression && n.callee.property.type === AST_NODE_TYPES.Identifier && n.callee.property.name === "map");
1252
1252
  const iter = AST.findParentNode(jsxElement, (n) => n === call || AST.isFunction(n));
1253
1253
  if (!AST.isFunction(iter)) return;
1254
1254
  const arg0 = call?.arguments[0];
@@ -1596,75 +1596,61 @@ var no_missing_key_default = createRule({
1596
1596
  create: create$30,
1597
1597
  defaultOptions: []
1598
1598
  });
1599
- function create$30(context) {
1600
- const state = { isWithinChildrenToArray: false };
1601
- function checkIteratorElement(node) {
1602
- switch (node.type) {
1603
- case AST_NODE_TYPES.JSXElement:
1604
- if (getJsxAttribute(context, node)("key") == null) return {
1605
- messageId: "missingKey",
1606
- node
1607
- };
1608
- return null;
1609
- case AST_NODE_TYPES.JSXFragment: return {
1610
- messageId: "unexpectedFragmentSyntax",
1611
- node
1612
- };
1613
- default: return null;
1614
- }
1599
+ function create$30(ctx) {
1600
+ let inChildrenToArray = false;
1601
+ function check(node) {
1602
+ if (node.type === AST_NODE_TYPES.JSXElement) return getJsxAttribute(ctx, node)("key") == null ? {
1603
+ messageId: "missingKey",
1604
+ node
1605
+ } : null;
1606
+ if (node.type === AST_NODE_TYPES.JSXFragment) return {
1607
+ messageId: "unexpectedFragmentSyntax",
1608
+ node
1609
+ };
1610
+ return null;
1615
1611
  }
1616
- function checkExpression(node) {
1612
+ function checkExpr(node) {
1617
1613
  switch (node.type) {
1618
- case AST_NODE_TYPES.ConditionalExpression:
1619
- if ("consequent" in node) return checkIteratorElement(node.consequent) ?? checkIteratorElement(node.alternate);
1620
- return null;
1614
+ case AST_NODE_TYPES.ConditionalExpression: return check(node.consequent) ?? check(node.alternate);
1615
+ case AST_NODE_TYPES.LogicalExpression: return check(node.left) ?? check(node.right);
1621
1616
  case AST_NODE_TYPES.JSXElement:
1622
- case AST_NODE_TYPES.JSXFragment: return checkIteratorElement(node);
1623
- case AST_NODE_TYPES.LogicalExpression:
1624
- if ("left" in node) return checkIteratorElement(node.left) ?? checkIteratorElement(node.right);
1625
- return null;
1617
+ case AST_NODE_TYPES.JSXFragment: return check(node);
1626
1618
  default: return null;
1627
1619
  }
1628
1620
  }
1629
- function checkBlockStatement(node) {
1630
- const descriptors = [];
1631
- for (const statement of AST.getNestedReturnStatements(node)) {
1632
- if (statement.argument == null) continue;
1633
- const descriptor = checkIteratorElement(statement.argument);
1634
- if (descriptor != null) descriptors.push(descriptor);
1635
- }
1636
- return descriptors;
1621
+ function checkBlock(node) {
1622
+ return AST.getNestedReturnStatements(node).filter((stmt) => stmt.argument != null).map((stmt) => check(stmt.argument)).filter((d) => d != null);
1637
1623
  }
1638
1624
  return {
1639
1625
  ArrayExpression(node) {
1640
- if (state.isWithinChildrenToArray) return;
1626
+ if (inChildrenToArray) return;
1641
1627
  const elements = node.elements.filter(AST.is(AST_NODE_TYPES.JSXElement));
1642
1628
  if (elements.length === 0) return;
1643
- const initialScope = context.sourceCode.getScope(node);
1644
- for (const element of elements) if (getJsxAttribute(context, element, initialScope)("key") == null) context.report({
1629
+ const scope = ctx.sourceCode.getScope(node);
1630
+ for (const el of elements) if (getJsxAttribute(ctx, el, scope)("key") == null) ctx.report({
1645
1631
  messageId: "missingKey",
1646
- node: element
1632
+ node: el
1647
1633
  });
1648
1634
  },
1649
1635
  CallExpression(node) {
1650
- state.isWithinChildrenToArray ||= isChildrenToArrayCall(context, node);
1651
- if (state.isWithinChildrenToArray) return;
1652
- const callback = match(node).when(AST.isArrayMapCall, (n) => n.arguments[0]).when(AST.isArrayFromCall, (n) => n.arguments[1]).otherwise(() => null);
1653
- if (!AST.isFunction(callback)) return;
1654
- const body = callback.body;
1655
- if (body.type === AST_NODE_TYPES.BlockStatement) {
1656
- checkBlockStatement(body).forEach(report(context));
1657
- return;
1658
- }
1659
- report(context)(checkExpression(body));
1636
+ inChildrenToArray ||= isChildrenToArrayCall(ctx, node);
1637
+ if (inChildrenToArray) return;
1638
+ if (node.callee.type !== AST_NODE_TYPES.MemberExpression) return;
1639
+ if (node.callee.property.type !== AST_NODE_TYPES.Identifier) return;
1640
+ const name$9 = node.callee.property.name;
1641
+ const idx = name$9 === "from" ? 1 : name$9 === "map" ? 0 : -1;
1642
+ if (idx < 0) return;
1643
+ const cb = node.arguments[idx];
1644
+ if (!AST.isFunction(cb)) return;
1645
+ if (cb.body.type === AST_NODE_TYPES.BlockStatement) checkBlock(cb.body).forEach(report(ctx));
1646
+ else report(ctx)(checkExpr(cb.body));
1660
1647
  },
1661
1648
  "CallExpression:exit"(node) {
1662
- if (!isChildrenToArrayCall(context, node)) return;
1663
- state.isWithinChildrenToArray = false;
1649
+ if (isChildrenToArrayCall(ctx, node)) inChildrenToArray = false;
1664
1650
  },
1665
1651
  JSXFragment(node) {
1666
- if (state.isWithinChildrenToArray) return;
1667
- if (node.parent.type === AST_NODE_TYPES.ArrayExpression) context.report({
1652
+ if (inChildrenToArray) return;
1653
+ if (node.parent.type === AST_NODE_TYPES.ArrayExpression) ctx.report({
1668
1654
  messageId: "unexpectedFragmentSyntax",
1669
1655
  node
1670
1656
  });
@@ -2165,11 +2151,24 @@ function create$20(context) {
2165
2151
  }
2166
2152
  };
2167
2153
  }
2154
+ function getArrayMethodCallbackPosition(methodName) {
2155
+ switch (methodName) {
2156
+ case "filter":
2157
+ case "flatMap":
2158
+ case "forEach":
2159
+ case "map":
2160
+ case "reduce":
2161
+ case "reduceRight": return 0;
2162
+ case "from":
2163
+ case "fromAsync": return 1;
2164
+ default: return -1;
2165
+ }
2166
+ }
2168
2167
  function isArrayMethodCallback(node) {
2169
2168
  const parent = node.parent;
2170
2169
  if (parent?.type !== AST_NODE_TYPES.CallExpression) return false;
2171
- if (!AST.isArrayMapCall(parent) && !AST.isArrayFromCall(parent)) return false;
2172
- return AST.isOneOf([AST_NODE_TYPES.ArrowFunctionExpression, AST_NODE_TYPES.FunctionExpression])(AST.getUnderlyingExpression(node));
2170
+ if (parent.callee.type !== AST_NODE_TYPES.MemberExpression || parent.callee.property.type !== AST_NODE_TYPES.Identifier) return false;
2171
+ return parent.arguments[getArrayMethodCallbackPosition(parent.callee.property.name)] === node;
2173
2172
  }
2174
2173
 
2175
2174
  //#endregion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-react-x",
3
- "version": "2.7.0",
3
+ "version": "2.7.1-beta.1",
4
4
  "description": "A set of composable ESLint rules for for libraries and frameworks that use React as a UI runtime.",
5
5
  "keywords": [
6
6
  "react",
@@ -46,11 +46,11 @@
46
46
  "string-ts": "^2.3.1",
47
47
  "ts-api-utils": "^2.4.0",
48
48
  "ts-pattern": "^5.9.0",
49
- "@eslint-react/eff": "2.7.0",
50
- "@eslint-react/ast": "2.7.0",
51
- "@eslint-react/shared": "2.7.0",
52
- "@eslint-react/core": "2.7.0",
53
- "@eslint-react/var": "2.7.0"
49
+ "@eslint-react/ast": "2.7.1-beta.1",
50
+ "@eslint-react/eff": "2.7.1-beta.1",
51
+ "@eslint-react/core": "2.7.1-beta.1",
52
+ "@eslint-react/shared": "2.7.1-beta.1",
53
+ "@eslint-react/var": "2.7.1-beta.1"
54
54
  },
55
55
  "devDependencies": {
56
56
  "@types/react": "^19.2.8",