eslint-plugin-react-x 5.30.0-beta.7 → 5.30.0-beta.9

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 +96 -116
  2. package/package.json +7 -7
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@ import { DEFAULT_ESLINT_REACT_SETTINGS, getSettingsFromContext, toRegExp } from
2
2
  import * as ESLintUtils from "@typescript-eslint/utils/eslint-utils";
3
3
  import { Analyze, Check, Collect, Compare, Extract, Traverse } from "@eslint-react/ast";
4
4
  import * as core from "@eslint-react/core";
5
- import { complete, getConstrainedTypeAtLocation, getText, merge, report } from "@eslint-react/eslint";
5
+ import { getConstrainedTypeAtLocation, getText, merge, report } from "@eslint-react/eslint";
6
6
  import { AST_NODE_TYPES } from "@typescript-eslint/types";
7
7
  import { P, isMatching, match } from "ts-pattern";
8
8
  import { isAssignmentTargetEqual, isInitializedFromReact, resolveEnclosingAssignmentTarget, resolveObjectType, resolveOrigin, resolveOriginChain } from "@eslint-react/var";
@@ -130,7 +130,7 @@ const rules$6 = {
130
130
  };
131
131
  // package.json
132
132
  var name$6 = "eslint-plugin-react-x";
133
- var version = "5.30.0-beta.7";
133
+ var version = "5.30.0-beta.9";
134
134
  // src/utils/create-rule.ts
135
135
  function getDocsUrl(ruleName) {
136
136
  return `https://eslint-react.xyz/docs/rules/${ruleName}`;
@@ -2281,24 +2281,6 @@ function isAssignmentToThisState(node) {
2281
2281
  }
2282
2282
  return false;
2283
2283
  }
2284
- // src/utils/jsx-iterator.ts
2285
- const KNOWN_CALLBACK_POSITIONS = /* @__PURE__ */ new Map([
2286
- ["flatMap", 0],
2287
- ["from", 1],
2288
- ["map", 0]
2289
- ]);
2290
- function getIteratorCallback(node) {
2291
- const callee = Extract.unwrap(node.callee);
2292
- if (callee.type !== AST_NODE_TYPES.MemberExpression) return null;
2293
- if (!Check.isIdentifier(callee.property)) return null;
2294
- const position = KNOWN_CALLBACK_POSITIONS.get(callee.property.name);
2295
- if (position == null) return null;
2296
- const argument = node.arguments[position];
2297
- if (argument == null) return null;
2298
- const callback = Extract.unwrap(argument);
2299
- if (!Check.isFunction(callback)) return null;
2300
- return callback;
2301
- }
2302
2284
  var no_duplicate_key_default = createRule({
2303
2285
  meta: {
2304
2286
  type: "problem",
@@ -2311,74 +2293,61 @@ var no_duplicate_key_default = createRule({
2311
2293
  });
2312
2294
  function create$32(context) {
2313
2295
  if (!/\bkey\b/u.test(context.sourceCode.text)) return {};
2314
- let childrenRekeyingDepth = 0;
2315
- const duplicateKeys = /* @__PURE__ */ new Set();
2316
- const siblingKeys = /* @__PURE__ */ new Map();
2317
- function getKeyValue(node) {
2318
- const { value } = node;
2319
- if (value == null) return null;
2320
- return value.type === AST_NODE_TYPES.JSXExpressionContainer ? Extract.unwrap(value.expression) : value;
2321
- }
2322
- function isKeyValueEqual(a, b) {
2323
- const aValue = getKeyValue(a);
2324
- const bValue = getKeyValue(b);
2325
- if (aValue == null || bValue == null) return aValue === bValue;
2326
- return Compare.isEqual(aValue, bValue);
2327
- }
2328
- function isStaticKey(node) {
2329
- const value = getKeyValue(node);
2330
- if (value == null) return true;
2331
- if (value.type === AST_NODE_TYPES.Literal) return typeof value.value === "string" || typeof value.value === "number";
2332
- return value.type === AST_NODE_TYPES.TemplateLiteral && value.expressions.length === 0;
2333
- }
2334
- function isChildrenRekeyingCall(node) {
2335
- return core.isChildrenToArrayCall(context, node) || core.isChildrenMapCall(context, node);
2296
+ const keyGroups = /* @__PURE__ */ new Map();
2297
+ function recordSiblingKey(root, key) {
2298
+ const prevKeys = keyGroups.get(root)?.keys ?? [];
2299
+ keyGroups.set(root, {
2300
+ hasDuplicate: prevKeys.some((prevKey) => isKeyValueEqual(prevKey, key)),
2301
+ keys: [...prevKeys, key]
2302
+ });
2303
+ }
2304
+ function recordMapCallbackKey(call, key) {
2305
+ keyGroups.set(call, {
2306
+ hasDuplicate: key.value?.type === AST_NODE_TYPES.Literal,
2307
+ keys: [key]
2308
+ });
2336
2309
  }
2337
2310
  return {
2338
2311
  "JSXAttribute[name.name='key']"(node) {
2339
- if (childrenRekeyingDepth > 0) return;
2340
2312
  const jsxElement = node.parent.parent;
2341
2313
  switch (jsxElement.parent.type) {
2342
2314
  case AST_NODE_TYPES.ArrayExpression:
2343
2315
  case AST_NODE_TYPES.JSXElement:
2344
- case AST_NODE_TYPES.JSXFragment: {
2345
- const root = jsxElement.parent;
2346
- const prevKeys = siblingKeys.get(root) ?? [];
2347
- for (const prevKey of prevKeys) {
2348
- if (!isKeyValueEqual(prevKey, node)) continue;
2349
- duplicateKeys.add(prevKey);
2350
- duplicateKeys.add(node);
2351
- break;
2352
- }
2353
- siblingKeys.set(root, [...prevKeys, node]);
2354
- break;
2355
- }
2316
+ case AST_NODE_TYPES.JSXFragment:
2317
+ recordSiblingKey(jsxElement.parent, node);
2318
+ return;
2356
2319
  default: {
2357
- const call = Traverse.findParent(jsxElement, (n) => n.type === AST_NODE_TYPES.CallExpression && getIteratorCallback(n) != null);
2320
+ const call = Traverse.findParent(jsxElement, (n) => {
2321
+ if (n.type !== AST_NODE_TYPES.CallExpression) return false;
2322
+ const callee = Extract.unwrap(n.callee);
2323
+ if (callee.type !== AST_NODE_TYPES.MemberExpression) return false;
2324
+ return Check.isIdentifier(callee.property, "map");
2325
+ });
2358
2326
  if (call == null) return;
2359
- const callback = getIteratorCallback(call);
2327
+ const callback = call.arguments[0];
2360
2328
  if (callback == null) return;
2361
- const iter = Traverse.findParent(jsxElement, Check.isFunction, (n) => n === call);
2362
- if (iter == null) return;
2363
- if (callback !== iter) return;
2364
- if (isStaticKey(node)) duplicateKeys.add(node);
2329
+ if (Traverse.findParent(jsxElement, Check.isFunction, (n) => n === call) !== Extract.unwrap(callback)) return;
2330
+ recordMapCallbackKey(call, node);
2365
2331
  }
2366
2332
  }
2367
2333
  },
2368
- CallExpression(node) {
2369
- if (isChildrenRekeyingCall(node)) childrenRekeyingDepth += 1;
2370
- },
2371
- "CallExpression:exit"(node) {
2372
- if (isChildrenRekeyingCall(node)) childrenRekeyingDepth -= 1;
2373
- },
2374
2334
  "Program:exit"() {
2375
- for (const key of duplicateKeys) context.report({
2376
- messageId: "default",
2377
- node: key
2378
- });
2335
+ for (const { hasDuplicate, keys } of keyGroups.values()) {
2336
+ if (!hasDuplicate) continue;
2337
+ for (const key of keys) context.report({
2338
+ messageId: "default",
2339
+ node: key
2340
+ });
2341
+ }
2379
2342
  }
2380
2343
  };
2381
2344
  }
2345
+ function isKeyValueEqual(a, b) {
2346
+ const aValue = a.value;
2347
+ const bValue = b.value;
2348
+ if (aValue == null || bValue == null) return false;
2349
+ return Compare.isEqual(aValue, bValue);
2350
+ }
2382
2351
  var no_forward_ref_default = createRule({
2383
2352
  meta: {
2384
2353
  type: "suggestion",
@@ -2706,6 +2675,7 @@ function create$25(context) {
2706
2675
  if (declaration == null || declaration.type !== AST_NODE_TYPES.VariableDeclaration) return [];
2707
2676
  switch (declaration.parent.type) {
2708
2677
  case AST_NODE_TYPES.BlockStatement:
2678
+ case AST_NODE_TYPES.ExportNamedDeclaration:
2709
2679
  case AST_NODE_TYPES.Program:
2710
2680
  case AST_NODE_TYPES.StaticBlock:
2711
2681
  case AST_NODE_TYPES.SwitchCase:
@@ -2733,6 +2703,24 @@ function create$25(context) {
2733
2703
  }
2734
2704
  };
2735
2705
  }
2706
+ // src/utils/jsx-iterator.ts
2707
+ const KNOWN_CALLBACK_POSITIONS = /* @__PURE__ */ new Map([
2708
+ ["flatMap", 0],
2709
+ ["from", 1],
2710
+ ["map", 0]
2711
+ ]);
2712
+ function getIteratorCallback(node) {
2713
+ const callee = Extract.unwrap(node.callee);
2714
+ if (callee.type !== AST_NODE_TYPES.MemberExpression) return null;
2715
+ if (!Check.isIdentifier(callee.property)) return null;
2716
+ const position = KNOWN_CALLBACK_POSITIONS.get(callee.property.name);
2717
+ if (position == null) return null;
2718
+ const argument = node.arguments[position];
2719
+ if (argument == null) return null;
2720
+ const callback = Extract.unwrap(argument);
2721
+ if (!Check.isFunction(callback)) return null;
2722
+ return callback;
2723
+ }
2736
2724
  var no_missing_key_default = createRule({
2737
2725
  meta: {
2738
2726
  type: "problem",
@@ -3297,31 +3285,28 @@ function getHumanReadableKind(node) {
3297
3285
  }
3298
3286
  return toLowerCase(delimiterCase(node.type, " "));
3299
3287
  }
3300
- // src/rules/no-unstable-default-props/no-unstable-default-props.ts
3301
- const RULE_NAME$12 = "no-unstable-default-props";
3302
- const defaultOptions$1 = [{ safeDefaultProps: [] }];
3303
- const schema$1 = [{
3304
- type: "object",
3305
- additionalProperties: false,
3306
- properties: { safeDefaultProps: {
3307
- type: "array",
3308
- description: "Property names or patterns that are safe to use as default props.",
3309
- items: { type: "string" }
3310
- } }
3311
- }];
3312
3288
  var no_unstable_default_props_default = createRule({
3313
3289
  meta: {
3314
3290
  type: "problem",
3315
- defaultOptions: [...defaultOptions$1],
3291
+ defaultOptions: [{}],
3316
3292
  docs: { description: "Prevents using referential-type values as default props in object destructuring." },
3317
3293
  messages: { default: "A/an '{{kind}}' as default prop. This could lead to potential infinite render loop in React. Use a variable instead of '{{kind}}'." },
3318
- schema: schema$1
3294
+ schema: [{
3295
+ type: "object",
3296
+ additionalProperties: false,
3297
+ properties: { safeDefaultProps: {
3298
+ type: "array",
3299
+ description: "Property names or patterns that are safe to use as default props.",
3300
+ items: { type: "string" }
3301
+ } }
3302
+ }]
3319
3303
  },
3320
- name: RULE_NAME$12,
3304
+ name: "no-unstable-default-props",
3321
3305
  create: create$12
3322
3306
  });
3323
3307
  function create$12(context) {
3324
- const { safeDefaultProps } = complete(context.options[0], defaultOptions$1[0]);
3308
+ const options = { ...context.options[0] ?? {} };
3309
+ const safeDefaultProps = options.safeDefaultProps ??= [];
3325
3310
  const { compilationMode } = getSettingsFromContext(context);
3326
3311
  if (compilationMode === "infer" || compilationMode === "all") return {};
3327
3312
  if (compilationMode === "annotation" && context.sourceCode.ast.body.some((stmt) => Check.isDirective(stmt, "use memo"))) return {};
@@ -8405,48 +8390,43 @@ const LAZY_INIT_ALLOW_LIST = [
8405
8390
  "String",
8406
8391
  "Number"
8407
8392
  ];
8408
- // src/rules/use-state/use-state.ts
8409
- const RULE_NAME = "use-state";
8410
- const defaultOptions = [{
8411
- enforceAssignment: true,
8412
- enforceLazyInitialization: true,
8413
- enforceSetterName: true
8414
- }];
8415
- const schema = [{
8416
- type: "object",
8417
- additionalProperties: false,
8418
- properties: {
8419
- enforceAssignment: {
8420
- type: "boolean",
8421
- description: "Require the useState result to be assigned to a destructured value/setter pair."
8422
- },
8423
- enforceLazyInitialization: {
8424
- type: "boolean",
8425
- description: "Require expensive initializers to be wrapped in a lazy initializer function."
8426
- },
8427
- enforceSetterName: {
8428
- type: "boolean",
8429
- description: "Require the setter name to match the state value name."
8430
- }
8431
- }
8432
- }];
8433
8393
  var use_state_default = createRule({
8434
8394
  meta: {
8435
8395
  type: "suggestion",
8436
- defaultOptions: [...defaultOptions],
8396
+ defaultOptions: [{}],
8437
8397
  docs: { description: "Enforces correct usage of 'useState', including destructuring, symmetric naming of the value and setter, and wrapping expensive initializers in a lazy initializer function." },
8438
8398
  messages: {
8439
8399
  "invalid-initialization": "To prevent re-computation, consider using lazy initial state for useState calls that involve function calls. Ex: 'useState(() => getValue())'.",
8440
8400
  "invalid-assignment": "useState should be destructured into a value and setter pair, e.g., const [state, setState] = useState(...).",
8441
8401
  "invalid-setter-name": "The setter should be named 'set' followed by the capitalized state variable name, e.g., 'setState' for 'state'."
8442
8402
  },
8443
- schema
8403
+ schema: [{
8404
+ type: "object",
8405
+ additionalProperties: false,
8406
+ properties: {
8407
+ enforceAssignment: {
8408
+ type: "boolean",
8409
+ description: "Require the useState result to be assigned to a destructured value/setter pair."
8410
+ },
8411
+ enforceLazyInitialization: {
8412
+ type: "boolean",
8413
+ description: "Require expensive initializers to be wrapped in a lazy initializer function."
8414
+ },
8415
+ enforceSetterName: {
8416
+ type: "boolean",
8417
+ description: "Require the setter name to match the state value name."
8418
+ }
8419
+ }
8420
+ }]
8444
8421
  },
8445
- name: RULE_NAME,
8422
+ name: "use-state",
8446
8423
  create
8447
8424
  });
8448
8425
  function create(context) {
8449
- const { enforceAssignment, enforceLazyInitialization, enforceSetterName } = complete(context.options[0], defaultOptions[0]);
8426
+ const options = { ...context.options[0] ?? {} };
8427
+ const enforceAssignment = options.enforceAssignment ??= true;
8428
+ const enforceLazyInitialization = options.enforceLazyInitialization ??= true;
8429
+ const enforceSetterName = options.enforceSetterName ??= true;
8450
8430
  const { additionalStateHooks } = getSettingsFromContext(context);
8451
8431
  return { CallExpression(node) {
8452
8432
  if (!core.isUseStateLikeCall(node, additionalStateHooks)) return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-react-x",
3
- "version": "5.30.0-beta.7",
3
+ "version": "5.30.0-beta.9",
4
4
  "description": "A set of composable ESLint rules for libraries and frameworks that use React as a UI runtime.",
5
5
  "keywords": [
6
6
  "react",
@@ -36,12 +36,12 @@
36
36
  "dist"
37
37
  ],
38
38
  "dependencies": {
39
- "@eslint-react/ast": "5.30.0-beta.7",
40
- "@eslint-react/core": "5.30.0-beta.7",
41
- "@eslint-react/eslint": "5.30.0-beta.7",
42
- "@eslint-react/jsx": "5.30.0-beta.7",
43
- "@eslint-react/shared": "5.30.0-beta.7",
44
- "@eslint-react/var": "5.30.0-beta.7",
39
+ "@eslint-react/ast": "5.30.0-beta.9",
40
+ "@eslint-react/core": "5.30.0-beta.9",
41
+ "@eslint-react/eslint": "5.30.0-beta.9",
42
+ "@eslint-react/jsx": "5.30.0-beta.9",
43
+ "@eslint-react/shared": "5.30.0-beta.9",
44
+ "@eslint-react/var": "5.30.0-beta.9",
45
45
  "@typescript-eslint/scope-manager": "^8.72.1",
46
46
  "@typescript-eslint/types": "^8.72.1",
47
47
  "@typescript-eslint/utils": "^8.72.1",