eslint-plugin-power-esrules 0.1.13 → 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.
|
@@ -22,25 +22,6 @@ function getImportType(node) {
|
|
|
22
22
|
if (source.startsWith("./") || source.startsWith("../")) {
|
|
23
23
|
return "relative";
|
|
24
24
|
}
|
|
25
|
-
// Старое поведение (оставлено для истории):
|
|
26
|
-
// Относительные импорты (./ или ../) всегда внутренние
|
|
27
|
-
// if (source.startsWith("./") || source.startsWith("../")) {
|
|
28
|
-
// // Проверяем, является ли это компонентом
|
|
29
|
-
// // Компоненты обычно импортируются как default или с большой буквы
|
|
30
|
-
// const isDefaultImport = node.specifiers.some(
|
|
31
|
-
// (spec) => spec.type === "ImportDefaultSpecifier"
|
|
32
|
-
// );
|
|
33
|
-
// const hasComponentName = node.specifiers.some(
|
|
34
|
-
// (spec) =>
|
|
35
|
-
// spec.type === "ImportSpecifier" &&
|
|
36
|
-
// spec.imported &&
|
|
37
|
-
// /^[A-Z]/.test(spec.imported.name)
|
|
38
|
-
// );
|
|
39
|
-
// if (isDefaultImport || hasComponentName) {
|
|
40
|
-
// return "component";
|
|
41
|
-
// }
|
|
42
|
-
// return "internal";
|
|
43
|
-
// }
|
|
44
25
|
|
|
45
26
|
// Сторонние библиотеки (не относительные и не начинаются с внутренних путей)
|
|
46
27
|
const internalPathPatterns = [
|
|
@@ -82,6 +63,7 @@ function getImportType(node) {
|
|
|
82
63
|
if (isUtility) {
|
|
83
64
|
return "internal";
|
|
84
65
|
}
|
|
66
|
+
|
|
85
67
|
// Проверяем, является ли это компонентом
|
|
86
68
|
const isDefaultImport = node.specifiers.some(
|
|
87
69
|
(spec) => spec.type === "ImportDefaultSpecifier",
|
|
@@ -91,8 +73,7 @@ function getImportType(node) {
|
|
|
91
73
|
spec.type === "ImportSpecifier" &&
|
|
92
74
|
spec.imported &&
|
|
93
75
|
/^[A-Z][a-zA-Z]*$/.test(spec.imported.name) && // Имя начинается с большой буквы и не все заглавные
|
|
94
|
-
spec.imported.name !== spec.imported.name.toUpperCase()
|
|
95
|
-
!spec.imported.name.endsWith("Context"), // не содержит ключевого слова Context
|
|
76
|
+
spec.imported.name !== spec.imported.name.toUpperCase(), // Не константа (не все заглавные)
|
|
96
77
|
);
|
|
97
78
|
if (isDefaultImport || hasComponentName) {
|
|
98
79
|
return "component";
|
|
@@ -104,7 +85,8 @@ function getImportType(node) {
|
|
|
104
85
|
spec.type === "ImportSpecifier" &&
|
|
105
86
|
spec.imported &&
|
|
106
87
|
/^[A-Z][a-zA-Z]*$/.test(spec.imported.name) &&
|
|
107
|
-
spec.imported.name !== spec.imported.name.toUpperCase()
|
|
88
|
+
spec.imported.name !== spec.imported.name.toUpperCase() && // Не константа
|
|
89
|
+
!spec.imported.name.endsWith("Context"),
|
|
108
90
|
);
|
|
109
91
|
if (hasComponentName) {
|
|
110
92
|
return "component";
|
|
@@ -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
|
-
|
|
181
|
-
|
|
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
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
const { RuleTester } = require("eslint");
|
|
2
|
+
const rule = require("../lib/rules/import-sorting");
|
|
3
|
+
|
|
4
|
+
const ruleTester = new RuleTester({
|
|
5
|
+
parserOptions: {
|
|
6
|
+
ecmaVersion: 2022,
|
|
7
|
+
sourceType: "module",
|
|
8
|
+
ecmaFeatures: { jsx: true },
|
|
9
|
+
},
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
ruleTester.run("import-sorting", rule, {
|
|
13
|
+
valid: [
|
|
14
|
+
{
|
|
15
|
+
name: "correctly sorted imports with blank lines",
|
|
16
|
+
code: `
|
|
17
|
+
import React from 'react';
|
|
18
|
+
import _ from 'lodash';
|
|
19
|
+
|
|
20
|
+
import { fetchData } from 'api';
|
|
21
|
+
import { calculate } from 'utils';
|
|
22
|
+
|
|
23
|
+
import Button from 'components/Button';
|
|
24
|
+
import Header from 'components/Header';
|
|
25
|
+
|
|
26
|
+
import helper from './helper';
|
|
27
|
+
`,
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
name: "single external import",
|
|
31
|
+
code: `
|
|
32
|
+
import React from 'react';
|
|
33
|
+
`,
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
name: "external and internal imports only",
|
|
37
|
+
code: `
|
|
38
|
+
import React from 'react';
|
|
39
|
+
|
|
40
|
+
import { fetchData } from 'api';
|
|
41
|
+
`,
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
name: "component import after internal",
|
|
45
|
+
code: `
|
|
46
|
+
import React from 'react';
|
|
47
|
+
|
|
48
|
+
import { fetchData } from 'api';
|
|
49
|
+
|
|
50
|
+
import Button from 'components/Button';
|
|
51
|
+
`,
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
name: "relative imports last",
|
|
55
|
+
code: `
|
|
56
|
+
import React from 'react';
|
|
57
|
+
|
|
58
|
+
import { fetchData } from 'api';
|
|
59
|
+
|
|
60
|
+
import Button from 'components/Button';
|
|
61
|
+
|
|
62
|
+
import util from '../util';
|
|
63
|
+
import helper from './helper';
|
|
64
|
+
`,
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
name: "React context import is inner import",
|
|
68
|
+
code: `
|
|
69
|
+
import { fetchData } from 'api';
|
|
70
|
+
import {PageContext} from 'pages/page';
|
|
71
|
+
|
|
72
|
+
import ComponentA from 'components/ComponentA';
|
|
73
|
+
|
|
74
|
+
`,
|
|
75
|
+
},
|
|
76
|
+
],
|
|
77
|
+
|
|
78
|
+
invalid: [
|
|
79
|
+
{
|
|
80
|
+
name: "external import after internal",
|
|
81
|
+
code: `
|
|
82
|
+
import { fetchData } from 'api';
|
|
83
|
+
import React from 'react';
|
|
84
|
+
`,
|
|
85
|
+
output: `
|
|
86
|
+
import React from 'react';
|
|
87
|
+
|
|
88
|
+
import { fetchData } from 'api';
|
|
89
|
+
`,
|
|
90
|
+
errors: [{ messageId: "incorrectOrder" }],
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
name: "component import before internal",
|
|
94
|
+
code: `
|
|
95
|
+
import Button from 'components/Button';
|
|
96
|
+
import { fetchData } from 'api';
|
|
97
|
+
`,
|
|
98
|
+
output: `
|
|
99
|
+
import { fetchData } from 'api';
|
|
100
|
+
|
|
101
|
+
import Button from 'components/Button';
|
|
102
|
+
`,
|
|
103
|
+
errors: [{ messageId: "incorrectOrder" }],
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
name: "missing blank line between groups",
|
|
107
|
+
code: `
|
|
108
|
+
import React from 'react';
|
|
109
|
+
import _ from 'lodash';
|
|
110
|
+
import { fetchData } from 'api';
|
|
111
|
+
`,
|
|
112
|
+
output: `
|
|
113
|
+
import React from 'react';
|
|
114
|
+
import _ from 'lodash';
|
|
115
|
+
|
|
116
|
+
import { fetchData } from 'api';
|
|
117
|
+
`,
|
|
118
|
+
errors: [{ messageId: "missingBlankLine" }],
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
name: "relative import not last",
|
|
122
|
+
code: `
|
|
123
|
+
import helper from './helper';
|
|
124
|
+
import { fetchData } from 'api';
|
|
125
|
+
`,
|
|
126
|
+
output: `
|
|
127
|
+
import { fetchData } from 'api';
|
|
128
|
+
|
|
129
|
+
import helper from './helper';
|
|
130
|
+
`,
|
|
131
|
+
errors: [{ messageId: "incorrectOrder" }],
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
name: "multiple issues at once",
|
|
135
|
+
code: `
|
|
136
|
+
import Button from 'components/Button';
|
|
137
|
+
import React from 'react';
|
|
138
|
+
import { fetchData } from 'api';
|
|
139
|
+
import helper from './helper';
|
|
140
|
+
`,
|
|
141
|
+
output: `
|
|
142
|
+
import React from 'react';
|
|
143
|
+
|
|
144
|
+
import { fetchData } from 'api';
|
|
145
|
+
|
|
146
|
+
import Button from 'components/Button';
|
|
147
|
+
|
|
148
|
+
import helper from './helper';
|
|
149
|
+
`,
|
|
150
|
+
errors: [
|
|
151
|
+
{ messageId: "incorrectOrder" },
|
|
152
|
+
{ messageId: "missingBlankLine" },
|
|
153
|
+
],
|
|
154
|
+
},
|
|
155
|
+
],
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
console.log("OK");
|
|
@@ -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: `
|