eslint-plugin-react-naming-convention 5.26.1 → 5.26.3

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 +60 -30
  2. package/package.json +6 -6
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,58 @@ 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.1";
21
+ var version = "5.26.3";
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 && parent.value === node && propertyTarget == null) propertyTarget = getKeyTarget(parent.key, parent.computed);
37
+ switch (parent.type) {
38
+ case AST_NODE_TYPES.VariableDeclarator: {
39
+ const idTarget = getIdentifierTarget(parent.id);
40
+ return idTarget == null ? null : propertyTarget ?? idTarget;
41
+ }
42
+ case AST_NODE_TYPES.AssignmentExpression: {
43
+ const leftTarget = getAssignmentTarget(parent.left);
44
+ return leftTarget == null ? null : propertyTarget ?? leftTarget;
45
+ }
46
+ case AST_NODE_TYPES.PropertyDefinition: {
47
+ const keyTarget = getKeyTarget(parent.key, parent.computed);
48
+ return keyTarget == null ? null : propertyTarget ?? keyTarget;
49
+ }
50
+ case AST_NODE_TYPES.ExportDefaultDeclaration:
51
+ case AST_NODE_TYPES.BlockStatement:
52
+ case AST_NODE_TYPES.Program: return null;
53
+ default: return resolveUpward(parent, propertyTarget);
54
+ }
55
+ }
56
+ function getStaticName(key, computed) {
57
+ if (!computed && key.type === AST_NODE_TYPES.Identifier) return key.name;
58
+ if (key.type === AST_NODE_TYPES.Literal && typeof key.value === "string") return key.value;
59
+ return null;
60
+ }
61
+ function getIdentifierTarget(node) {
62
+ return node.type === AST_NODE_TYPES.Identifier ? [node, node.name] : null;
63
+ }
64
+ function getAssignmentTarget(left) {
65
+ if (left.type === AST_NODE_TYPES.Identifier) return [left, left.name];
66
+ if (left.type === AST_NODE_TYPES.MemberExpression) return getKeyTarget(left.property, left.computed, left);
67
+ return null;
68
+ }
69
+ function getKeyTarget(key, computed, reportNode = key) {
70
+ const name = getStaticName(key, computed);
71
+ return name == null ? null : [reportNode, name];
72
+ }
30
73
  var context_name_default = createRule({
31
74
  meta: {
32
75
  type: "suggestion",
@@ -41,14 +84,9 @@ function create$2(context) {
41
84
  if (!context.sourceCode.text.includes("createContext")) return {};
42
85
  return { CallExpression(node) {
43
86
  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;
87
+ const target = resolveNamingTarget(node);
88
+ if (target == null) return;
89
+ const [id, name] = target;
52
90
  if (core.isFunctionComponentName(name) && name.endsWith("Context")) return;
53
91
  context.report({
54
92
  messageId: "invalid-context-name",
@@ -70,14 +108,9 @@ function create$1(context) {
70
108
  if (!context.sourceCode.text.includes("useId")) return {};
71
109
  return { CallExpression(node) {
72
110
  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;
111
+ const target = resolveNamingTarget(node);
112
+ if (target == null) return;
113
+ const [id, name] = target;
81
114
  if (name.endsWith("Id") || name === "id") return;
82
115
  context.report({
83
116
  messageId: "invalid-id-name",
@@ -99,15 +132,12 @@ function create(context) {
99
132
  const { additionalRefHooks } = getSettingsFromContext(context);
100
133
  return { CallExpression(node) {
101
134
  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;
135
+ let parent = node.parent;
136
+ while (Check.isTypeExpression(parent)) parent = parent.parent;
137
+ if (parent.type === AST_NODE_TYPES.MemberExpression) return;
138
+ const target = resolveNamingTarget(node);
139
+ if (target == null) return;
140
+ const [id, name] = target;
111
141
  if (name.endsWith("Ref") || name === "ref") return;
112
142
  context.report({
113
143
  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.1",
3
+ "version": "5.26.3",
4
4
  "description": "ESLint React's ESLint plugin for naming convention related rules.",
5
5
  "keywords": [
6
6
  "react",
@@ -37,11 +37,11 @@
37
37
  "dist"
38
38
  ],
39
39
  "dependencies": {
40
- "@eslint-react/ast": "5.26.1",
41
- "@eslint-react/core": "5.26.1",
42
- "@eslint-react/eslint": "5.26.1",
43
- "@eslint-react/shared": "5.26.1",
44
- "@eslint-react/var": "5.26.1",
40
+ "@eslint-react/ast": "5.26.3",
41
+ "@eslint-react/core": "5.26.3",
42
+ "@eslint-react/eslint": "5.26.3",
43
+ "@eslint-react/shared": "5.26.3",
44
+ "@eslint-react/var": "5.26.3",
45
45
  "@typescript-eslint/types": "^8.71.1",
46
46
  "@typescript-eslint/utils": "^8.71.1",
47
47
  "ts-pattern": "^5.9.0"