eslint-plugin-power-esrules 0.1.14 → 0.1.15

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.
@@ -34,6 +34,17 @@ function isPascalCase(name) {
34
34
  function getComponentName(node) {
35
35
  if (!node) return null;
36
36
 
37
+ if (node.type === "ClassDeclaration") {
38
+ return node.id?.name ?? null;
39
+ }
40
+
41
+ if (
42
+ node.type === "ClassExpression" &&
43
+ node.parent?.type === "VariableDeclarator"
44
+ ) {
45
+ return node.parent.id?.name ?? null;
46
+ }
47
+
37
48
  // Функция объявлена напрямую
38
49
  if (node.type === "FunctionDeclaration") {
39
50
  return node.id?.name ?? null;
@@ -48,6 +59,20 @@ function getComponentName(node) {
48
59
  return node.parent.id?.name ?? null;
49
60
  }
50
61
 
62
+ if (
63
+ node.parent?.type === "CallExpression" &&
64
+ node.parent.parent?.type === "VariableDeclarator"
65
+ ) {
66
+ node.parent.parent.id?.name ?? null;
67
+ }
68
+
69
+ if (
70
+ node.type === "CallExpression" &&
71
+ node.parent?.type === "VariableDeclarator"
72
+ ) {
73
+ return node.parent.id?.name ?? null;
74
+ }
75
+
51
76
  // HOC обёртка: memo(() => {}) или forwardRef(() => {}) или observer(() => {})
52
77
  if (node.type === "CallExpression" && node.arguments.length > 0) {
53
78
  return getComponentName(node.arguments[0]);
@@ -66,6 +91,28 @@ function isReactComponent(node) {
66
91
  function getRootJSXFromComponent(fnNode) {
67
92
  if (!fnNode) return null;
68
93
 
94
+ if (fnNode.type === "ClassDeclaration" || fnNode.type === "ClassExpression") {
95
+ const renderMethod = fnNode.body.body.find(
96
+ (member) =>
97
+ member.type === "MethodDefinition" && member.key?.name === "render",
98
+ );
99
+
100
+ if (!renderMethod) return null;
101
+
102
+ const body = renderMethod.value.body.body;
103
+
104
+ for (const stmt of body) {
105
+ if (
106
+ stmt.type === "ReturnStatement" &&
107
+ stmt.argument &&
108
+ (stmt.argument.type === "JSXElement" ||
109
+ stmt.argument.type === "JSXFragment")
110
+ ) {
111
+ return stmt.argument;
112
+ }
113
+ }
114
+ }
115
+
69
116
  // Разворачиваем HOC
70
117
  if (fnNode.type === "CallExpression" && fnNode.arguments.length > 0) {
71
118
  return getRootJSXFromComponent(fnNode.arguments[0]);
@@ -177,8 +224,9 @@ module.exports = {
177
224
  FunctionDeclaration: collectComponent,
178
225
  FunctionExpression: collectComponent,
179
226
  ArrowFunctionExpression: collectComponent,
180
- // CallExpression больше не нужен, HOC разворачиваются внутри getComponentName
181
- //"CallExpression": collectComponent,
227
+ ClassDeclaration: collectComponent,
228
+ ClassExpression: collectComponent,
229
+ CallExpression: collectComponent,
182
230
 
183
231
  "Program:exit"() {
184
232
  for (const component of components) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-power-esrules",
3
- "version": "0.1.14",
3
+ "version": "0.1.15",
4
4
  "description": "custom ESLint rules",
5
5
  "main": "index.js",
6
6
  "peerDependencies": {
@@ -19,6 +19,16 @@ ruleTester.run("require-data-testid", rule, {
19
19
  }
20
20
  `,
21
21
  },
22
+ {
23
+ name: "class component with data-testid",
24
+ code: `
25
+ class MyComponent extends PureComponent {
26
+ render() {
27
+ return <div data-testid="my-component">Hello</div>;
28
+ }
29
+ }
30
+ `,
31
+ },
22
32
  {
23
33
  name: "arrow component with dataTestID",
24
34
  code: `
@@ -67,6 +77,17 @@ ruleTester.run("require-data-testid", rule, {
67
77
  `,
68
78
  errors: [{ messageId: "missingDataTestId" }],
69
79
  },
80
+ {
81
+ name: "class component without data-testid",
82
+ code: `
83
+ class MyComponent extends PureComponent {
84
+ render() {
85
+ return <div>Hello</div>;
86
+ }
87
+ }
88
+ `,
89
+ errors: [{ messageId: "missingDataTestId" }],
90
+ },
70
91
  {
71
92
  name: "arrow component missing data-testid",
72
93
  code: `