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