eslint-plugin-fsd-paths-check 0.0.9 → 0.0.10
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,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const path = require("path");
|
|
4
|
+
const {isPathRelative} = require("../helpers/isPathRelative");
|
|
5
|
+
const micromatch = require("micromatch");
|
|
6
|
+
|
|
7
|
+
const fsdLayers = {
|
|
8
|
+
'shared': 'shared',
|
|
9
|
+
'entities': 'entities',
|
|
10
|
+
'features': 'features',
|
|
11
|
+
'widgets': 'widgets',
|
|
12
|
+
'pages': 'pages',
|
|
13
|
+
'app': 'app'
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const allowedLayerImports = {
|
|
17
|
+
app: ['pages', 'widgets', 'features', 'entities', 'shared'],
|
|
18
|
+
pages: ['widgets', 'features', 'entities', 'shared'],
|
|
19
|
+
widgets: ['features', 'entities', 'shared'],
|
|
20
|
+
features: ['entities', 'shared'],
|
|
21
|
+
entities: ['entities', 'shared'],
|
|
22
|
+
shared: ['shared'],
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
module.exports = {
|
|
26
|
+
meta: {
|
|
27
|
+
type: `suggestion`,
|
|
28
|
+
docs: {
|
|
29
|
+
description: "Rule, which forbids imports from upper fsd slices",
|
|
30
|
+
recommended: false,
|
|
31
|
+
url: null,
|
|
32
|
+
},
|
|
33
|
+
fixable: null,
|
|
34
|
+
schema: [
|
|
35
|
+
{
|
|
36
|
+
type: `object`,
|
|
37
|
+
properties: {
|
|
38
|
+
alias: {
|
|
39
|
+
type: `string`,
|
|
40
|
+
},
|
|
41
|
+
ignoreImportPatterns: {
|
|
42
|
+
type: `array`,
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
],
|
|
47
|
+
messages: {
|
|
48
|
+
doNotImportFromUpperFSDLayers: `Imports from upper FSD layers are prohibited`
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
|
|
52
|
+
create(context) {
|
|
53
|
+
return {
|
|
54
|
+
ImportDeclaration: importExportDeclaration(context),
|
|
55
|
+
ExportNamedDeclaration: importExportDeclaration(context),
|
|
56
|
+
};
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const importExportDeclaration = (context) => (node) => {
|
|
61
|
+
const { alias = '', ignoreImportPatterns = [] } = context.options[0] ?? {};
|
|
62
|
+
|
|
63
|
+
const getCurrentFileLayer = (currentFilePath) => {
|
|
64
|
+
const normalizedPath = path.toNamespacedPath(currentFilePath);
|
|
65
|
+
const projectPath = normalizedPath?.split('src')[1];
|
|
66
|
+
const segments = projectPath.split('/');
|
|
67
|
+
|
|
68
|
+
return segments?.[1];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const getImportLayer = (value) => {
|
|
72
|
+
const importPath = alias ? value.replace(alias, '') : value;
|
|
73
|
+
const segments = importPath?.split('/');
|
|
74
|
+
|
|
75
|
+
return segments?.[0];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if(!node.source) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const currentFilePath = context.getFilename();
|
|
83
|
+
const importPath = node.source.value;
|
|
84
|
+
const currentFileLayer = getCurrentFileLayer(currentFilePath);
|
|
85
|
+
const importLayer = getImportLayer(importPath);
|
|
86
|
+
|
|
87
|
+
if(isPathRelative(importPath)) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if(!fsdLayers[importLayer] || !fsdLayers[currentFileLayer]) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const isIgnored = ignoreImportPatterns.some((pattern) => {
|
|
96
|
+
return micromatch.isMatch(importPath, pattern);
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
if(isIgnored) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if(!allowedLayerImports[currentFileLayer].includes(importLayer)) {
|
|
104
|
+
context.report({
|
|
105
|
+
node,
|
|
106
|
+
messageId: `doNotImportFromUpperFSDLayers`
|
|
107
|
+
})
|
|
108
|
+
}
|
|
109
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-fsd-paths-check",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.10",
|
|
4
4
|
"description": "Plugin to check feature sliced design paths correctness",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"eslint",
|
|
@@ -22,11 +22,12 @@
|
|
|
22
22
|
"update:eslint-docs": "eslint-doc-generator"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
+
"micromatch": "^4.0.8",
|
|
25
26
|
"requireindex": "^1.2.0"
|
|
26
27
|
},
|
|
27
28
|
"devDependencies": {
|
|
28
|
-
"eslint": "^8.26.0",
|
|
29
29
|
"@eslint/js": "^8.26.0",
|
|
30
|
+
"eslint": "^8.26.0",
|
|
30
31
|
"eslint-doc-generator": "^2.0.0",
|
|
31
32
|
"eslint-plugin-eslint-plugin": "^6.0.0",
|
|
32
33
|
"eslint-plugin-n": "^17.0.0",
|