eslint-plugin-import-path-correct 0.0.4 → 0.0.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.
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
("use strict");
|
|
2
|
+
const micromatch = require("micromatch");
|
|
3
|
+
const path = require("path");
|
|
4
|
+
const { isPathRelative } = require("../helpers");
|
|
5
|
+
//------------------------------------------------------------------------------
|
|
6
|
+
// Rule Definition
|
|
7
|
+
//------------------------------------------------------------------------------
|
|
8
|
+
|
|
9
|
+
/** @type {import('eslint').Rule.RuleModule} */
|
|
10
|
+
module.exports = {
|
|
11
|
+
meta: {
|
|
12
|
+
type: "layout", // `problem`, `suggestion`, or `layout`
|
|
13
|
+
docs: {
|
|
14
|
+
description: "layter imports",
|
|
15
|
+
recommended: false,
|
|
16
|
+
url: null, // URL to the documentation page for this rule
|
|
17
|
+
},
|
|
18
|
+
fixable: null, // Or `code` or `whitespace`
|
|
19
|
+
schema: [
|
|
20
|
+
{
|
|
21
|
+
type: "object",
|
|
22
|
+
properties: {
|
|
23
|
+
alias: { type: "string", description: "some" },
|
|
24
|
+
ignoreImportPatterns: {
|
|
25
|
+
type: "array",
|
|
26
|
+
description: "description",
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
additionalProperties: false,
|
|
30
|
+
},
|
|
31
|
+
],
|
|
32
|
+
defaultOptions: [
|
|
33
|
+
{
|
|
34
|
+
alias: "",
|
|
35
|
+
ignoreImportPatterns: [],
|
|
36
|
+
},
|
|
37
|
+
],
|
|
38
|
+
messages: {
|
|
39
|
+
needCurrentImport:
|
|
40
|
+
"Слой может импортировать в себя только нижележащие слои (shared, entities, features, widgets, pages, app)",
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
|
|
44
|
+
create(context) {
|
|
45
|
+
const layers = {
|
|
46
|
+
app: ["pages", "widgets", "features", "shared", "entites"],
|
|
47
|
+
pages: ["widgets", "features", "shared", "entites"],
|
|
48
|
+
widgets: ["features", "shared", "entites"],
|
|
49
|
+
features: ["shared", "entites"],
|
|
50
|
+
entites: ["shared", "entites"],
|
|
51
|
+
shared: ["shared"],
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const availableLayers = {
|
|
55
|
+
app: "app",
|
|
56
|
+
entites: "entites",
|
|
57
|
+
features: "features",
|
|
58
|
+
shared: "shared",
|
|
59
|
+
pages: "pages",
|
|
60
|
+
widgets: "widgets",
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const { alias = "", ignoreImportPatterns = [] } = context.options[0] ?? {};
|
|
64
|
+
|
|
65
|
+
const getCurrentFileLayer = () => {
|
|
66
|
+
const currentFilePath = context.getFilename();
|
|
67
|
+
|
|
68
|
+
const normalizedPath = path.toNamespacedPath(currentFilePath);
|
|
69
|
+
const projectPath = normalizedPath?.split("src")[1];
|
|
70
|
+
const segments = projectPath?.split("\\");
|
|
71
|
+
|
|
72
|
+
return segments?.[1];
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const getImportLayer = (value) => {
|
|
76
|
+
const importPath = alias ? value.replace(`${alias}/`, "") : value;
|
|
77
|
+
const segments = importPath?.split("/");
|
|
78
|
+
|
|
79
|
+
return segments?.[0];
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
ImportDeclaration(node) {
|
|
84
|
+
const importPath = node.source.value;
|
|
85
|
+
const currentFileLayer = getCurrentFileLayer();
|
|
86
|
+
const importLayer = getImportLayer(importPath);
|
|
87
|
+
|
|
88
|
+
if (isPathRelative(importPath)) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (
|
|
93
|
+
!availableLayers[importLayer] ||
|
|
94
|
+
!availableLayers[currentFileLayer]
|
|
95
|
+
) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const isIgnored = ignoreImportPatterns.some((pattern) => {
|
|
100
|
+
return micromatch.isMatch(importPath, pattern);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
if (isIgnored) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (!layers[currentFileLayer]?.includes(importLayer)) {
|
|
108
|
+
context.report({ node, messageId: "needCurrentImport" });
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
},
|
|
113
|
+
};
|
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
const { isPathRelative } = require("../helpers");
|
|
4
4
|
const micromatch = require("micromatch");
|
|
5
5
|
|
|
6
|
+
const PUBLIC_ERROR = "PUBLIC_ERROR";
|
|
7
|
+
const TESTING_PUBLIC_API = "TESTING_PUBLIC_API";
|
|
8
|
+
|
|
6
9
|
/** @type {import('eslint').Rule.RuleModule} */
|
|
7
10
|
module.exports = {
|
|
8
11
|
meta: {
|
|
@@ -12,7 +15,7 @@ module.exports = {
|
|
|
12
15
|
recommended: false,
|
|
13
16
|
url: null, // URL to the documentation page for this rule
|
|
14
17
|
},
|
|
15
|
-
fixable:
|
|
18
|
+
fixable: "code", // Or `code` or `whitespace`
|
|
16
19
|
schema: [
|
|
17
20
|
{
|
|
18
21
|
type: "object",
|
|
@@ -34,9 +37,9 @@ module.exports = {
|
|
|
34
37
|
},
|
|
35
38
|
],
|
|
36
39
|
messages: {
|
|
37
|
-
|
|
40
|
+
PUBLIC_ERROR:
|
|
38
41
|
"Абсолютный импорт разрешен только из Publick API(index.ts)",
|
|
39
|
-
|
|
42
|
+
TESTING_PUBLIC_API:
|
|
40
43
|
"Тестовые данных необходимо импортировать из publicApi/testing.ts",
|
|
41
44
|
},
|
|
42
45
|
},
|
|
@@ -64,6 +67,8 @@ module.exports = {
|
|
|
64
67
|
const segments = importTo.split("/");
|
|
65
68
|
|
|
66
69
|
const layer = segments[0];
|
|
70
|
+
const slice = segments[1];
|
|
71
|
+
|
|
67
72
|
if (!checkingLayers[layer]) {
|
|
68
73
|
return;
|
|
69
74
|
}
|
|
@@ -75,7 +80,13 @@ module.exports = {
|
|
|
75
80
|
if (isImportNotFromPublicApi && !isTestingPublicApi) {
|
|
76
81
|
context.report({
|
|
77
82
|
node,
|
|
78
|
-
messageId:
|
|
83
|
+
messageId: PUBLIC_ERROR,
|
|
84
|
+
fix: (fixer) => {
|
|
85
|
+
return fixer.replaceText(
|
|
86
|
+
node.source,
|
|
87
|
+
`'${alias}/${layer}/${slice}'`,
|
|
88
|
+
);
|
|
89
|
+
},
|
|
79
90
|
});
|
|
80
91
|
}
|
|
81
92
|
|
|
@@ -88,7 +99,7 @@ module.exports = {
|
|
|
88
99
|
if (!isCurrentFileTesting) {
|
|
89
100
|
context.report({
|
|
90
101
|
node,
|
|
91
|
-
messageId:
|
|
102
|
+
messageId: TESTING_PUBLIC_API,
|
|
92
103
|
});
|
|
93
104
|
}
|
|
94
105
|
}
|