eslint-plugin-react-x 2.0.0-beta.55 → 2.0.0-beta.57

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 +23 -21
  2. package/package.json +7 -7
package/dist/index.js CHANGED
@@ -115,7 +115,7 @@ const settings = { ...settings$1 };
115
115
  //#endregion
116
116
  //#region package.json
117
117
  var name = "eslint-plugin-react-x";
118
- var version = "2.0.0-beta.55";
118
+ var version = "2.0.0-beta.57";
119
119
 
120
120
  //#endregion
121
121
  //#region src/utils/create-rule.ts
@@ -591,9 +591,9 @@ function create$59(context) {
591
591
  //#region src/rules/no-array-index-key.ts
592
592
  const RULE_NAME$58 = "no-array-index-key";
593
593
  const RULE_FEATURES$56 = [];
594
- const reactChildrenMethod = ["forEach", "map"];
594
+ const REACT_CHILDREN_METHOD = ["forEach", "map"];
595
595
  function isReactChildrenMethod(name$4) {
596
- return reactChildrenMethod.some((method) => method === name$4);
596
+ return REACT_CHILDREN_METHOD.includes(name$4);
597
597
  }
598
598
  function isUsingReactChildren(context, node) {
599
599
  const { importSource = "react" } = coerceSettings(context.settings);
@@ -3212,20 +3212,19 @@ function create$11(context, [option]) {
3212
3212
  }
3213
3213
  /**
3214
3214
  * Check if a Literal or JSXText node is whitespace
3215
- * @param node The AST node to check
3216
- * @returns boolean `true` if the node is whitespace
3217
3215
  */
3218
3216
  function isWhiteSpace(node) {
3219
3217
  return typeof node.value === "string" && node.raw.trim() === "";
3220
3218
  }
3221
3219
  /**
3222
- * Check if a Literal or JSXText node is padding spaces
3223
- * @param node The AST node to check
3224
- * @returns boolean
3220
+ * Check if a node is padding spaces (whitespace with line breaks)
3225
3221
  */
3226
3222
  function isPaddingSpaces(node) {
3227
3223
  return ER.isJsxText(node) && isWhiteSpace(node) && node.raw.includes("\n");
3228
3224
  }
3225
+ /**
3226
+ * Trim whitespace like React would in JSX
3227
+ */
3229
3228
  function trimLikeReact(text) {
3230
3229
  const leadingSpaces = /^\s*/.exec(text)?.[0] ?? "";
3231
3230
  const trailingSpaces = /\s*$/.exec(text)?.[0] ?? "";
@@ -3233,6 +3232,9 @@ function trimLikeReact(text) {
3233
3232
  const end = trailingSpaces.includes("\n") ? text.length - trailingSpaces.length : text.length;
3234
3233
  return text.slice(start, end);
3235
3234
  }
3235
+ /**
3236
+ * Check if a fragment node is useless and should be reported
3237
+ */
3236
3238
  function checkNode(context, node, allowExpressions) {
3237
3239
  const initialScope = context.sourceCode.getScope(node);
3238
3240
  if (node.type === AST_NODE_TYPES.JSXElement && ER.hasAttribute(context, "key", node.openingElement.attributes, initialScope)) return;
@@ -3273,18 +3275,16 @@ function checkNode(context, node, allowExpressions) {
3273
3275
  }
3274
3276
  const nonPaddingChildren = node.children.filter((child) => !isPaddingSpaces(child));
3275
3277
  const firstNonPaddingChild = nonPaddingChildren.at(0);
3276
- switch (true) {
3277
- case nonPaddingChildren.length === 0:
3278
- case nonPaddingChildren.length === 1 && firstNonPaddingChild?.type !== AST_NODE_TYPES.JSXExpressionContainer:
3279
- context.report({
3280
- messageId: "uselessFragment",
3281
- node,
3282
- data: { reason: "contains less than two children" },
3283
- fix: getFix(context, node)
3284
- });
3285
- return;
3286
- }
3278
+ if (nonPaddingChildren.length === 0 || nonPaddingChildren.length === 1 && firstNonPaddingChild?.type !== AST_NODE_TYPES.JSXExpressionContainer) context.report({
3279
+ messageId: "uselessFragment",
3280
+ node,
3281
+ data: { reason: "contains less than two children" },
3282
+ fix: getFix(context, node)
3283
+ });
3287
3284
  }
3285
+ /**
3286
+ * Generate fix for removing useless fragment
3287
+ */
3288
3288
  function getFix(context, node) {
3289
3289
  if (!canFix(context, node)) return null;
3290
3290
  return (fixer) => {
@@ -3294,11 +3294,13 @@ function getFix(context, node) {
3294
3294
  return fixer.replaceText(node, trimLikeReact(childrenText));
3295
3295
  };
3296
3296
  }
3297
+ /**
3298
+ * Check if it's safe to automatically fix the fragment
3299
+ */
3297
3300
  function canFix(context, node) {
3298
3301
  if (node.parent.type === AST_NODE_TYPES.JSXElement || node.parent.type === AST_NODE_TYPES.JSXFragment) return ER.isHostElement(context, node.parent);
3299
3302
  if (node.children.length === 0) return false;
3300
- if (node.children.some((child) => ER.isJsxText(child) && !isWhiteSpace(child) || AST.is(AST_NODE_TYPES.JSXExpressionContainer)(child))) return false;
3301
- return true;
3303
+ return !node.children.some((child) => ER.isJsxText(child) && !isWhiteSpace(child) || AST.is(AST_NODE_TYPES.JSXExpressionContainer)(child));
3302
3304
  }
3303
3305
 
3304
3306
  //#endregion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-react-x",
3
- "version": "2.0.0-beta.55",
3
+ "version": "2.0.0-beta.57",
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",
@@ -42,12 +42,12 @@
42
42
  "is-immutable-type": "^5.0.1",
43
43
  "string-ts": "^2.2.1",
44
44
  "ts-pattern": "^5.8.0",
45
- "@eslint-react/ast": "2.0.0-beta.55",
46
- "@eslint-react/eff": "2.0.0-beta.55",
47
- "@eslint-react/kit": "2.0.0-beta.55",
48
- "@eslint-react/shared": "2.0.0-beta.55",
49
- "@eslint-react/var": "2.0.0-beta.55",
50
- "@eslint-react/core": "2.0.0-beta.55"
45
+ "@eslint-react/ast": "2.0.0-beta.57",
46
+ "@eslint-react/core": "2.0.0-beta.57",
47
+ "@eslint-react/kit": "2.0.0-beta.57",
48
+ "@eslint-react/eff": "2.0.0-beta.57",
49
+ "@eslint-react/shared": "2.0.0-beta.57",
50
+ "@eslint-react/var": "2.0.0-beta.57"
51
51
  },
52
52
  "devDependencies": {
53
53
  "@types/react": "^19.1.12",