eslint-plugin-react-naming-convention 1.28.1-next.4 → 1.28.1-next.6

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.
package/dist/index.d.mts CHANGED
@@ -34,7 +34,7 @@ declare const _default: {
34
34
  extensions?: readonly string[];
35
35
  ignoreFilesWithoutCode?: boolean;
36
36
  } | undefined], unknown, _typescript_eslint_utils_ts_eslint.RuleListener>;
37
- readonly "use-state": _typescript_eslint_utils_ts_eslint.RuleModule<"unexpected", [], unknown, _typescript_eslint_utils_ts_eslint.RuleListener>;
37
+ readonly "use-state": _typescript_eslint_utils_ts_eslint.RuleModule<"badValueOrSetterName", [], unknown, _typescript_eslint_utils_ts_eslint.RuleListener>;
38
38
  };
39
39
  };
40
40
  };
@@ -68,7 +68,7 @@ declare const _default: {
68
68
  extensions?: readonly string[];
69
69
  ignoreFilesWithoutCode?: boolean;
70
70
  } | undefined], unknown, _typescript_eslint_utils_ts_eslint.RuleListener>;
71
- readonly "use-state": _typescript_eslint_utils_ts_eslint.RuleModule<"unexpected", [], unknown, _typescript_eslint_utils_ts_eslint.RuleListener>;
71
+ readonly "use-state": _typescript_eslint_utils_ts_eslint.RuleModule<"badValueOrSetterName", [], unknown, _typescript_eslint_utils_ts_eslint.RuleListener>;
72
72
  };
73
73
  };
74
74
 
package/dist/index.d.ts CHANGED
@@ -34,7 +34,7 @@ declare const _default: {
34
34
  extensions?: readonly string[];
35
35
  ignoreFilesWithoutCode?: boolean;
36
36
  } | undefined], unknown, _typescript_eslint_utils_ts_eslint.RuleListener>;
37
- readonly "use-state": _typescript_eslint_utils_ts_eslint.RuleModule<"unexpected", [], unknown, _typescript_eslint_utils_ts_eslint.RuleListener>;
37
+ readonly "use-state": _typescript_eslint_utils_ts_eslint.RuleModule<"badValueOrSetterName", [], unknown, _typescript_eslint_utils_ts_eslint.RuleListener>;
38
38
  };
39
39
  };
40
40
  };
@@ -68,7 +68,7 @@ declare const _default: {
68
68
  extensions?: readonly string[];
69
69
  ignoreFilesWithoutCode?: boolean;
70
70
  } | undefined], unknown, _typescript_eslint_utils_ts_eslint.RuleListener>;
71
- readonly "use-state": _typescript_eslint_utils_ts_eslint.RuleModule<"unexpected", [], unknown, _typescript_eslint_utils_ts_eslint.RuleListener>;
71
+ readonly "use-state": _typescript_eslint_utils_ts_eslint.RuleModule<"badValueOrSetterName", [], unknown, _typescript_eslint_utils_ts_eslint.RuleListener>;
72
72
  };
73
73
  };
74
74
 
package/dist/index.js CHANGED
@@ -51,7 +51,7 @@ var rules = {};
51
51
 
52
52
  // package.json
53
53
  var name2 = "eslint-plugin-react-naming-convention";
54
- var version = "1.28.1-next.4";
54
+ var version = "1.28.1-next.6";
55
55
  var createRule = shared.createRuleForPlugin("naming-convention");
56
56
 
57
57
  // src/rules/component-name.ts
@@ -391,10 +391,6 @@ var RULE_NAME4 = "use-state";
391
391
  var RULE_FEATURES = [
392
392
  "CHK"
393
393
  ];
394
- function isSetterNameLoose(name3) {
395
- const fourthChar = [...name3][3];
396
- return name3.startsWith("set") && fourthChar === fourthChar?.toUpperCase();
397
- }
398
394
  var use_state_default = createRule({
399
395
  meta: {
400
396
  type: "problem",
@@ -403,65 +399,44 @@ var use_state_default = createRule({
403
399
  [Symbol.for("rule_features")]: RULE_FEATURES
404
400
  },
405
401
  messages: {
406
- unexpected: "An useState call is not destructured into value + setter pair."
402
+ badValueOrSetterName: "An useState call is not destructured into value + setter pair."
407
403
  },
408
404
  schema: []
409
405
  },
410
406
  name: RULE_NAME4,
411
407
  create(context) {
412
- const alias = shared.getSettingsFromContext(context).additionalHooks.useState ?? [];
413
- const {
414
- ctx,
415
- listeners
416
- } = core.useComponentCollector(
417
- context,
418
- {
419
- collectDisplayName: false,
420
- collectHookCalls: true,
421
- hint: core.DEFAULT_COMPONENT_HINT
422
- }
423
- );
424
408
  return {
425
- ...listeners,
426
- "Program:exit"(node) {
427
- const components = ctx.getAllComponents(node);
428
- for (const { hookCalls } of components.values()) {
429
- if (hookCalls.length === 0) {
430
- continue;
431
- }
432
- for (const hookCall of hookCalls) {
433
- if (!core.isUseStateCall(context, hookCall) && !alias.some(core.isReactHookCallWithNameLoose(hookCall))) {
434
- continue;
435
- }
436
- if (hookCall.parent.type !== types.AST_NODE_TYPES.VariableDeclarator) {
437
- continue;
438
- }
439
- const { id } = hookCall.parent;
440
- switch (id.type) {
441
- case types.AST_NODE_TYPES.Identifier: {
442
- context.report({ messageId: "unexpected", node: id });
443
- break;
444
- }
445
- case types.AST_NODE_TYPES.ArrayPattern: {
446
- const [state, setState] = id.elements;
447
- if (state?.type === types.AST_NODE_TYPES.ObjectPattern && setState?.type === types.AST_NODE_TYPES.Identifier) {
448
- if (!isSetterNameLoose(setState.name)) {
449
- context.report({ messageId: "unexpected", node: id });
450
- }
451
- break;
452
- }
453
- if (state?.type !== types.AST_NODE_TYPES.Identifier || setState?.type !== types.AST_NODE_TYPES.Identifier) {
454
- return;
455
- }
456
- const [stateName, setStateName] = [state.name, setState.name];
457
- const expectedSetterName = `set${stringTs.capitalize(stateName)}`;
458
- if (setStateName === expectedSetterName) {
459
- return;
460
- }
461
- context.report({ messageId: "unexpected", node: id });
462
- }
409
+ "CallExpression[callee.name='useState']"(node) {
410
+ if (node.parent.type !== types.AST_NODE_TYPES.VariableDeclarator) {
411
+ context.report({ messageId: "badValueOrSetterName", node });
412
+ }
413
+ const id = core.getInstanceId(node);
414
+ if (id?.type !== types.AST_NODE_TYPES.ArrayPattern) {
415
+ context.report({ messageId: "badValueOrSetterName", node });
416
+ return;
417
+ }
418
+ const [value, setter] = id.elements;
419
+ if (value == null || setter == null) {
420
+ context.report({ messageId: "badValueOrSetterName", node });
421
+ return;
422
+ }
423
+ const setterName = tsPattern.match(setter).with({ type: types.AST_NODE_TYPES.Identifier }, (id2) => id2.name).otherwise(() => eff._);
424
+ if (setterName == null || !setterName.startsWith("set")) {
425
+ context.report({ messageId: "badValueOrSetterName", node });
426
+ return;
427
+ }
428
+ const valueName = tsPattern.match(value).with({ type: types.AST_NODE_TYPES.Identifier }, (id2) => id2.name).with({ type: types.AST_NODE_TYPES.ObjectPattern }, ({ properties }) => {
429
+ const values = properties.reduce((acc, prop) => {
430
+ if (prop.type === types.AST_NODE_TYPES.Property && prop.key.type === types.AST_NODE_TYPES.Identifier) {
431
+ return [...acc, prop.key.name];
463
432
  }
464
- }
433
+ return acc;
434
+ }, []);
435
+ return values.join("_");
436
+ }).otherwise(() => eff._);
437
+ if (valueName == null || `set_${valueName}` !== stringTs.snakeCase(setterName)) {
438
+ context.report({ messageId: "badValueOrSetterName", node });
439
+ return;
465
440
  }
466
441
  }
467
442
  };
package/dist/index.mjs CHANGED
@@ -1,11 +1,11 @@
1
1
  import * as AST from '@eslint-react/ast';
2
- import { useComponentCollector, useComponentCollectorLegacy, DEFAULT_COMPONENT_HINT, isUseStateCall, isReactHookCallWithNameLoose } from '@eslint-react/core';
2
+ import { useComponentCollector, useComponentCollectorLegacy, getInstanceId } from '@eslint-react/core';
3
3
  import { isObject, _ } from '@eslint-react/eff';
4
4
  import * as JSX from '@eslint-react/jsx';
5
- import { createRuleForPlugin, getSettingsFromContext, RE_CONSTANT_CASE, RE_PASCAL_CASE, RE_CAMEL_CASE, RE_KEBAB_CASE, RE_SNAKE_CASE } from '@eslint-react/shared';
5
+ import { createRuleForPlugin, RE_CONSTANT_CASE, RE_PASCAL_CASE, RE_CAMEL_CASE, RE_KEBAB_CASE, RE_SNAKE_CASE } from '@eslint-react/shared';
6
6
  import { match } from 'ts-pattern';
7
7
  import path from 'node:path';
8
- import { capitalize, pascalCase, camelCase, kebabCase, snakeCase } from 'string-ts';
8
+ import { snakeCase, pascalCase, camelCase, kebabCase } from 'string-ts';
9
9
  import { AST_NODE_TYPES } from '@typescript-eslint/types';
10
10
 
11
11
  var __defProp = Object.defineProperty;
@@ -25,7 +25,7 @@ var rules = {};
25
25
 
26
26
  // package.json
27
27
  var name2 = "eslint-plugin-react-naming-convention";
28
- var version = "1.28.1-next.4";
28
+ var version = "1.28.1-next.6";
29
29
  var createRule = createRuleForPlugin("naming-convention");
30
30
 
31
31
  // src/rules/component-name.ts
@@ -365,10 +365,6 @@ var RULE_NAME4 = "use-state";
365
365
  var RULE_FEATURES = [
366
366
  "CHK"
367
367
  ];
368
- function isSetterNameLoose(name3) {
369
- const fourthChar = [...name3][3];
370
- return name3.startsWith("set") && fourthChar === fourthChar?.toUpperCase();
371
- }
372
368
  var use_state_default = createRule({
373
369
  meta: {
374
370
  type: "problem",
@@ -377,65 +373,44 @@ var use_state_default = createRule({
377
373
  [Symbol.for("rule_features")]: RULE_FEATURES
378
374
  },
379
375
  messages: {
380
- unexpected: "An useState call is not destructured into value + setter pair."
376
+ badValueOrSetterName: "An useState call is not destructured into value + setter pair."
381
377
  },
382
378
  schema: []
383
379
  },
384
380
  name: RULE_NAME4,
385
381
  create(context) {
386
- const alias = getSettingsFromContext(context).additionalHooks.useState ?? [];
387
- const {
388
- ctx,
389
- listeners
390
- } = useComponentCollector(
391
- context,
392
- {
393
- collectDisplayName: false,
394
- collectHookCalls: true,
395
- hint: DEFAULT_COMPONENT_HINT
396
- }
397
- );
398
382
  return {
399
- ...listeners,
400
- "Program:exit"(node) {
401
- const components = ctx.getAllComponents(node);
402
- for (const { hookCalls } of components.values()) {
403
- if (hookCalls.length === 0) {
404
- continue;
405
- }
406
- for (const hookCall of hookCalls) {
407
- if (!isUseStateCall(context, hookCall) && !alias.some(isReactHookCallWithNameLoose(hookCall))) {
408
- continue;
409
- }
410
- if (hookCall.parent.type !== AST_NODE_TYPES.VariableDeclarator) {
411
- continue;
412
- }
413
- const { id } = hookCall.parent;
414
- switch (id.type) {
415
- case AST_NODE_TYPES.Identifier: {
416
- context.report({ messageId: "unexpected", node: id });
417
- break;
418
- }
419
- case AST_NODE_TYPES.ArrayPattern: {
420
- const [state, setState] = id.elements;
421
- if (state?.type === AST_NODE_TYPES.ObjectPattern && setState?.type === AST_NODE_TYPES.Identifier) {
422
- if (!isSetterNameLoose(setState.name)) {
423
- context.report({ messageId: "unexpected", node: id });
424
- }
425
- break;
426
- }
427
- if (state?.type !== AST_NODE_TYPES.Identifier || setState?.type !== AST_NODE_TYPES.Identifier) {
428
- return;
429
- }
430
- const [stateName, setStateName] = [state.name, setState.name];
431
- const expectedSetterName = `set${capitalize(stateName)}`;
432
- if (setStateName === expectedSetterName) {
433
- return;
434
- }
435
- context.report({ messageId: "unexpected", node: id });
436
- }
383
+ "CallExpression[callee.name='useState']"(node) {
384
+ if (node.parent.type !== AST_NODE_TYPES.VariableDeclarator) {
385
+ context.report({ messageId: "badValueOrSetterName", node });
386
+ }
387
+ const id = getInstanceId(node);
388
+ if (id?.type !== AST_NODE_TYPES.ArrayPattern) {
389
+ context.report({ messageId: "badValueOrSetterName", node });
390
+ return;
391
+ }
392
+ const [value, setter] = id.elements;
393
+ if (value == null || setter == null) {
394
+ context.report({ messageId: "badValueOrSetterName", node });
395
+ return;
396
+ }
397
+ const setterName = match(setter).with({ type: AST_NODE_TYPES.Identifier }, (id2) => id2.name).otherwise(() => _);
398
+ if (setterName == null || !setterName.startsWith("set")) {
399
+ context.report({ messageId: "badValueOrSetterName", node });
400
+ return;
401
+ }
402
+ const valueName = match(value).with({ type: AST_NODE_TYPES.Identifier }, (id2) => id2.name).with({ type: AST_NODE_TYPES.ObjectPattern }, ({ properties }) => {
403
+ const values = properties.reduce((acc, prop) => {
404
+ if (prop.type === AST_NODE_TYPES.Property && prop.key.type === AST_NODE_TYPES.Identifier) {
405
+ return [...acc, prop.key.name];
437
406
  }
438
- }
407
+ return acc;
408
+ }, []);
409
+ return values.join("_");
410
+ }).otherwise(() => _);
411
+ if (valueName == null || `set_${valueName}` !== snakeCase(setterName)) {
412
+ context.report({ messageId: "badValueOrSetterName", node });
413
+ return;
439
414
  }
440
415
  }
441
416
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-react-naming-convention",
3
- "version": "1.28.1-next.4",
3
+ "version": "1.28.1-next.6",
4
4
  "description": "ESLint React's ESLint plugin for naming convention related rules.",
5
5
  "keywords": [
6
6
  "react",
@@ -49,11 +49,11 @@
49
49
  "@typescript-eslint/utils": "^8.25.0",
50
50
  "string-ts": "^2.2.1",
51
51
  "ts-pattern": "^5.6.2",
52
- "@eslint-react/core": "1.28.1-next.4",
53
- "@eslint-react/jsx": "1.28.1-next.4",
54
- "@eslint-react/ast": "1.28.1-next.4",
55
- "@eslint-react/shared": "1.28.1-next.4",
56
- "@eslint-react/eff": "1.28.1-next.4"
52
+ "@eslint-react/ast": "1.28.1-next.6",
53
+ "@eslint-react/core": "1.28.1-next.6",
54
+ "@eslint-react/jsx": "1.28.1-next.6",
55
+ "@eslint-react/shared": "1.28.1-next.6",
56
+ "@eslint-react/eff": "1.28.1-next.6"
57
57
  },
58
58
  "devDependencies": {
59
59
  "@types/react": "^19.0.10",