eslint-plugin-fsd-paths-check 0.0.14 → 0.0.16
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,23 @@
|
|
|
1
|
+
const { allFsdLayers } = require("../constants/constants");
|
|
2
|
+
|
|
3
|
+
const getFsdLayerFromPath = (filePath) => {
|
|
4
|
+
const normalizedPath = filePath.replace(/\\/g, '/');
|
|
5
|
+
const segments = normalizedPath.split('/');
|
|
6
|
+
|
|
7
|
+
for (const segment of segments) {
|
|
8
|
+
if (allFsdLayers.includes(segment)) {
|
|
9
|
+
return segment;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return null;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const hasFsdLayerInPath = (filePath) => {
|
|
17
|
+
return getFsdLayerFromPath(filePath) !== null;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
module.exports = {
|
|
21
|
+
getFsdLayerFromPath,
|
|
22
|
+
hasFsdLayerInPath,
|
|
23
|
+
};
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
const {
|
|
3
|
+
const {isPathRelative} = require("../helpers/isPathRelative");
|
|
4
4
|
const {isSameModuleImport} = require("../helpers/isSameModuleImport");
|
|
5
|
+
const {getFsdLayerFromPath} = require("../helpers/pathContainsFsdLayer");
|
|
6
|
+
const {allFsdLayers} = require("../constants/constants");
|
|
5
7
|
|
|
6
8
|
const fsdLayers = {
|
|
7
9
|
'entities': 'entities',
|
|
@@ -30,6 +32,7 @@ module.exports = {
|
|
|
30
32
|
messages: {
|
|
31
33
|
useOthersModulesPublicApi: `When importing from other modules, public api should be used`,
|
|
32
34
|
doNotUseAbsolutePathInSameModuleImport: `When importing from the same module, absolute path should not be used`,
|
|
35
|
+
doNotUseRelativePathsBetweenLayers: `When importing from other modules, absolute path should be used`,
|
|
33
36
|
},
|
|
34
37
|
},
|
|
35
38
|
|
|
@@ -41,22 +44,35 @@ module.exports = {
|
|
|
41
44
|
const pathTo = alias ? value.replace(`${alias}`, ``) : value;
|
|
42
45
|
const currentFile = context.getFilename();
|
|
43
46
|
|
|
44
|
-
|
|
47
|
+
const pathToLayer = getFsdLayerFromPath(pathTo)
|
|
48
|
+
const segments = pathTo.split("/");
|
|
49
|
+
|
|
50
|
+
if(isPathRelative(pathTo) && !pathToLayer) {
|
|
45
51
|
return;
|
|
52
|
+
} else if(isPathRelative(pathTo)) {
|
|
53
|
+
context.report({
|
|
54
|
+
node,
|
|
55
|
+
messageId: `doNotUseRelativePathsBetweenLayers`,
|
|
56
|
+
fix(fixer) {
|
|
57
|
+
const layerIndex = segments.findIndex(segment => allFsdLayers.includes(segment));
|
|
58
|
+
if (layerIndex === -1) return undefined;
|
|
59
|
+
|
|
60
|
+
const absolutePath = `${alias}${segments.slice(layerIndex).join(`/`)}`;
|
|
61
|
+
|
|
62
|
+
return fixer.replaceText(node.source, `'${absolutePath}'`);
|
|
63
|
+
}
|
|
64
|
+
})
|
|
46
65
|
}
|
|
47
66
|
|
|
48
|
-
const segments = pathTo.split("/");
|
|
49
67
|
const layer = segments[0];
|
|
50
|
-
|
|
51
68
|
if(!fsdLayers[layer]) {
|
|
52
69
|
return;
|
|
53
70
|
}
|
|
54
|
-
|
|
55
71
|
const isImportNotFromPublicApi = segments.length > 2;
|
|
56
72
|
const isSameModule = isSameModuleImport(currentFile, pathTo, fsdLayers);
|
|
57
73
|
|
|
58
74
|
if (isImportNotFromPublicApi && !isSameModule) {
|
|
59
|
-
context.report({
|
|
75
|
+
return context.report({
|
|
60
76
|
node,
|
|
61
77
|
messageId: `useOthersModulesPublicApi`,
|
|
62
78
|
fix(fixer) {
|
|
@@ -70,7 +86,7 @@ module.exports = {
|
|
|
70
86
|
})
|
|
71
87
|
}
|
|
72
88
|
else if (isSameModule) {
|
|
73
|
-
context.report({
|
|
89
|
+
return context.report({
|
|
74
90
|
node,
|
|
75
91
|
messageId: `doNotUseAbsolutePathInSameModuleImport`
|
|
76
92
|
})
|