eslint-plugin-react-naming-convention 5.26.2 → 5.26.4

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 +76 -30
  2. package/package.json +8 -8
package/dist/index.js CHANGED
@@ -1,10 +1,8 @@
1
1
  import * as ESLintUtils from "@typescript-eslint/utils/eslint-utils";
2
+ import { AST_NODE_TYPES } from "@typescript-eslint/types";
2
3
  import * as core from "@eslint-react/core";
3
4
  import "@eslint-react/eslint";
4
- import { resolveEnclosingAssignmentTarget } from "@eslint-react/var";
5
- import { AST_NODE_TYPES } from "@typescript-eslint/types";
6
- import { P, match } from "ts-pattern";
7
- import { Extract } from "@eslint-react/ast";
5
+ import { Check } from "@eslint-react/ast";
8
6
  import { getSettingsFromContext } from "@eslint-react/shared";
9
7
  //#region \0rolldown/runtime.js
10
8
  var __defProp = Object.defineProperty;
@@ -20,13 +18,74 @@ var __exportAll = (all, no_symbols) => {
20
18
  //#endregion
21
19
  //#region package.json
22
20
  var name$1 = "eslint-plugin-react-naming-convention";
23
- var version = "5.26.2";
21
+ var version = "5.26.4";
24
22
  //#endregion
25
23
  //#region src/utils/create-rule.ts
26
24
  function getDocsUrl(ruleName) {
27
25
  return `https://eslint-react.xyz/docs/rules/naming-convention-${ruleName}`;
28
26
  }
29
27
  const createRule = ESLintUtils.RuleCreator(getDocsUrl);
28
+ //#endregion
29
+ //#region src/utils/resolve-naming-target.ts
30
+ function resolveNamingTarget(node) {
31
+ return resolveUpward(node, null);
32
+ }
33
+ function resolveUpward(node, propertyTarget) {
34
+ const parent = node.parent;
35
+ if (parent == null) return null;
36
+ if (parent.type === AST_NODE_TYPES.Property) {
37
+ if (parent.value !== node) return null;
38
+ const keyTarget = getKeyTarget(parent.key, parent.computed);
39
+ if (keyTarget == null) return null;
40
+ propertyTarget ??= keyTarget;
41
+ return resolveUpward(parent, propertyTarget);
42
+ }
43
+ switch (parent.type) {
44
+ case AST_NODE_TYPES.VariableDeclarator: {
45
+ const idTarget = getIdentifierTarget(parent.id);
46
+ return idTarget == null ? null : propertyTarget ?? idTarget;
47
+ }
48
+ case AST_NODE_TYPES.AssignmentExpression: {
49
+ const leftTarget = getAssignmentTarget(parent.left);
50
+ return leftTarget == null ? null : propertyTarget ?? leftTarget;
51
+ }
52
+ case AST_NODE_TYPES.PropertyDefinition: {
53
+ const keyTarget = getKeyTarget(parent.key, parent.computed);
54
+ return keyTarget == null ? null : propertyTarget ?? keyTarget;
55
+ }
56
+ case AST_NODE_TYPES.ConditionalExpression:
57
+ case AST_NODE_TYPES.LogicalExpression:
58
+ case AST_NODE_TYPES.AwaitExpression:
59
+ case AST_NODE_TYPES.ChainExpression:
60
+ case AST_NODE_TYPES.TSAsExpression:
61
+ case AST_NODE_TYPES.TSTypeAssertion:
62
+ case AST_NODE_TYPES.TSNonNullExpression:
63
+ case AST_NODE_TYPES.TSSatisfiesExpression:
64
+ case AST_NODE_TYPES.TSInstantiationExpression:
65
+ case AST_NODE_TYPES.ObjectExpression: return resolveUpward(parent, propertyTarget);
66
+ case AST_NODE_TYPES.SequenceExpression: return parent.expressions[parent.expressions.length - 1] === node ? resolveUpward(parent, propertyTarget) : null;
67
+ case AST_NODE_TYPES.CallExpression:
68
+ case AST_NODE_TYPES.NewExpression: return parent.arguments.some((argument) => argument === node) ? resolveUpward(parent, propertyTarget) : null;
69
+ default: return null;
70
+ }
71
+ }
72
+ function getStaticName(key, computed) {
73
+ if (!computed && key.type === AST_NODE_TYPES.Identifier) return key.name;
74
+ if (key.type === AST_NODE_TYPES.Literal && typeof key.value === "string") return key.value;
75
+ return null;
76
+ }
77
+ function getIdentifierTarget(node) {
78
+ return node.type === AST_NODE_TYPES.Identifier ? [node, node.name] : null;
79
+ }
80
+ function getAssignmentTarget(left) {
81
+ if (left.type === AST_NODE_TYPES.Identifier) return [left, left.name];
82
+ if (left.type === AST_NODE_TYPES.MemberExpression) return getKeyTarget(left.property, left.computed, left);
83
+ return null;
84
+ }
85
+ function getKeyTarget(key, computed, reportNode = key) {
86
+ const name = getStaticName(key, computed);
87
+ return name == null ? null : [reportNode, name];
88
+ }
30
89
  var context_name_default = createRule({
31
90
  meta: {
32
91
  type: "suggestion",
@@ -41,14 +100,9 @@ function create$2(context) {
41
100
  if (!context.sourceCode.text.includes("createContext")) return {};
42
101
  return { CallExpression(node) {
43
102
  if (!core.isCreateContextCall(context, node)) return;
44
- const [id, name] = match(resolveEnclosingAssignmentTarget(node)).with({
45
- type: AST_NODE_TYPES.Identifier,
46
- name: P.string
47
- }, (id) => [id, id.name]).with({
48
- type: AST_NODE_TYPES.MemberExpression,
49
- property: { name: P.string }
50
- }, (id) => [id, id.property.name]).otherwise(() => [null, null]);
51
- if (id == null) return;
103
+ const target = resolveNamingTarget(node);
104
+ if (target == null) return;
105
+ const [id, name] = target;
52
106
  if (core.isFunctionComponentName(name) && name.endsWith("Context")) return;
53
107
  context.report({
54
108
  messageId: "invalid-context-name",
@@ -70,14 +124,9 @@ function create$1(context) {
70
124
  if (!context.sourceCode.text.includes("useId")) return {};
71
125
  return { CallExpression(node) {
72
126
  if (!core.isUseIdCall(context, node)) return;
73
- const [id, name] = match(resolveEnclosingAssignmentTarget(node)).with({
74
- type: AST_NODE_TYPES.Identifier,
75
- name: P.string
76
- }, (id) => [id, id.name]).with({
77
- type: AST_NODE_TYPES.MemberExpression,
78
- property: { name: P.string }
79
- }, (id) => [id, id.property.name]).otherwise(() => [null, null]);
80
- if (id == null) return;
127
+ const target = resolveNamingTarget(node);
128
+ if (target == null) return;
129
+ const [id, name] = target;
81
130
  if (name.endsWith("Id") || name === "id") return;
82
131
  context.report({
83
132
  messageId: "invalid-id-name",
@@ -99,15 +148,12 @@ function create(context) {
99
148
  const { additionalRefHooks } = getSettingsFromContext(context);
100
149
  return { CallExpression(node) {
101
150
  if (!core.isUseRefLikeCall(node, additionalRefHooks)) return;
102
- if (Extract.unwrap(node.parent).type === AST_NODE_TYPES.MemberExpression) return;
103
- const [id, name] = match(resolveEnclosingAssignmentTarget(node)).with({
104
- type: AST_NODE_TYPES.Identifier,
105
- name: P.string
106
- }, (id) => [id, id.name]).with({
107
- type: AST_NODE_TYPES.MemberExpression,
108
- property: { name: P.string }
109
- }, (id) => [id, id.property.name]).otherwise(() => [null, null]);
110
- if (id == null) return;
151
+ let parent = node.parent;
152
+ while (Check.isTypeExpression(parent)) parent = parent.parent;
153
+ if (parent.type === AST_NODE_TYPES.MemberExpression) return;
154
+ const target = resolveNamingTarget(node);
155
+ if (target == null) return;
156
+ const [id, name] = target;
111
157
  if (name.endsWith("Ref") || name === "ref") return;
112
158
  context.report({
113
159
  messageId: "invalid-ref-name",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-react-naming-convention",
3
- "version": "5.26.2",
3
+ "version": "5.26.4",
4
4
  "description": "ESLint React's ESLint plugin for naming convention related rules.",
5
5
  "keywords": [
6
6
  "react",
@@ -37,13 +37,13 @@
37
37
  "dist"
38
38
  ],
39
39
  "dependencies": {
40
- "@eslint-react/ast": "5.26.2",
41
- "@eslint-react/core": "5.26.2",
42
- "@eslint-react/eslint": "5.26.2",
43
- "@eslint-react/shared": "5.26.2",
44
- "@eslint-react/var": "5.26.2",
45
- "@typescript-eslint/types": "^8.71.1",
46
- "@typescript-eslint/utils": "^8.71.1",
40
+ "@eslint-react/ast": "5.26.4",
41
+ "@eslint-react/core": "5.26.4",
42
+ "@eslint-react/eslint": "5.26.4",
43
+ "@eslint-react/shared": "5.26.4",
44
+ "@eslint-react/var": "5.26.4",
45
+ "@typescript-eslint/types": "^8.71.2",
46
+ "@typescript-eslint/utils": "^8.71.2",
47
47
  "ts-pattern": "^5.9.0"
48
48
  },
49
49
  "devDependencies": {