eslint-plugin-fsd-paths-check 0.0.8 → 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.
- package/lib/helpers/isPathRelative.js +7 -0
- package/lib/helpers/isSameModuleImport.js +37 -0
- package/lib/rules/enforce-public-api-imports.js +82 -0
- package/lib/rules/enforce-relative-path-within-slice.js +12 -48
- package/lib/rules/forbid-imports-from-upper-slices.js +109 -0
- package/package.json +3 -2
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
const { isPathRelative } = require("./isPathRelative");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
|
|
4
|
+
function isSameModuleImport(from, to, fsdLayers) {
|
|
5
|
+
if(isPathRelative(to)) {
|
|
6
|
+
return false;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const toArray = to.split('/');
|
|
10
|
+
const toLayer = toArray[0];
|
|
11
|
+
const toSlice = toArray[1];
|
|
12
|
+
|
|
13
|
+
if(!toLayer || !toSlice || !fsdLayers[toLayer]) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const fromNormalized = path.toNamespacedPath(from);
|
|
18
|
+
const fromProject = fromNormalized.split(`src/`)[1];
|
|
19
|
+
|
|
20
|
+
if(!fromProject) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const fromArray = fromProject.split('/');
|
|
25
|
+
const fromLayer = fromArray[0];
|
|
26
|
+
const fromSlice = fromArray[1];
|
|
27
|
+
|
|
28
|
+
if(!fromLayer || !fromSlice || !fsdLayers[fromLayer]) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return fromSlice === toSlice && fromLayer === toLayer;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
module.exports = {
|
|
36
|
+
isSameModuleImport
|
|
37
|
+
};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { isPathRelative } = require("../helpers/isPathRelative");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
const {isSameModuleImport} = require("../helpers/isSameModuleImport");
|
|
6
|
+
|
|
7
|
+
const fsdLayers = {
|
|
8
|
+
'entities': 'entities',
|
|
9
|
+
'features': 'features',
|
|
10
|
+
'widgets': 'widgets',
|
|
11
|
+
'pages': 'pages',
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
module.exports = {
|
|
15
|
+
meta: {
|
|
16
|
+
type: `suggestion`,
|
|
17
|
+
docs: {
|
|
18
|
+
description: "Rule to enforce other modules public api usage",
|
|
19
|
+
recommended: false,
|
|
20
|
+
url: null,
|
|
21
|
+
},
|
|
22
|
+
fixable: `code`,
|
|
23
|
+
schema: [{
|
|
24
|
+
type: `object`,
|
|
25
|
+
properties: {
|
|
26
|
+
alias: {
|
|
27
|
+
type: `string`,
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}],
|
|
31
|
+
messages: {
|
|
32
|
+
useOthersModulesPublicApi: `When importing from other modules, public api should be used`,
|
|
33
|
+
doNotUseAbsolutePathInSameModuleImport: `When importing from the same module, absolute path should not be used`,
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
|
|
37
|
+
create(context) {
|
|
38
|
+
return {
|
|
39
|
+
ImportDeclaration(node) {
|
|
40
|
+
const alias = context.options[0]?.alias ?? ``;
|
|
41
|
+
const value = node.source.value;
|
|
42
|
+
const pathTo = alias ? value.replace(`${alias}`, ``) : value;
|
|
43
|
+
const currentFile = context.getFilename();
|
|
44
|
+
|
|
45
|
+
if(isPathRelative(pathTo)) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const segments = pathTo.split("/");
|
|
50
|
+
const layer = segments[0];
|
|
51
|
+
|
|
52
|
+
if(!fsdLayers[layer]) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const isImportNotFromPublicApi = segments.length > 2;
|
|
57
|
+
const isSameModule = isSameModuleImport(currentFile, pathTo, fsdLayers);
|
|
58
|
+
|
|
59
|
+
if (isImportNotFromPublicApi && !isSameModule) {
|
|
60
|
+
context.report({
|
|
61
|
+
node,
|
|
62
|
+
messageId: `useOthersModulesPublicApi`,
|
|
63
|
+
fix(fixer) {
|
|
64
|
+
const index = value.indexOf(segments[1]);
|
|
65
|
+
if (index === -1) return undefined;
|
|
66
|
+
|
|
67
|
+
const publicPathImport = value.substring(0, index + segments[1].length);
|
|
68
|
+
|
|
69
|
+
return fixer.replaceText(node.source, `'${publicPathImport}'`);
|
|
70
|
+
}
|
|
71
|
+
})
|
|
72
|
+
}
|
|
73
|
+
else if (isSameModule) {
|
|
74
|
+
context.report({
|
|
75
|
+
node,
|
|
76
|
+
messageId: `doNotUseAbsolutePathInSameModuleImport`
|
|
77
|
+
})
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
},
|
|
82
|
+
};
|
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
const path = require("path");
|
|
4
|
+
const { isSameModuleImport } = require("../helpers/isSameModuleImport");
|
|
5
|
+
|
|
6
|
+
const fsdLayers = {
|
|
7
|
+
'shared': 'shared',
|
|
8
|
+
'entities': 'entities',
|
|
9
|
+
'features': 'features',
|
|
10
|
+
'widgets': 'widgets',
|
|
11
|
+
'pages': 'pages',
|
|
12
|
+
}
|
|
4
13
|
|
|
5
14
|
module.exports = {
|
|
6
15
|
meta: {
|
|
@@ -11,16 +20,14 @@ module.exports = {
|
|
|
11
20
|
url: null, // URL to the documentation page for this rule
|
|
12
21
|
},
|
|
13
22
|
fixable: `code`,
|
|
14
|
-
schema: [
|
|
15
|
-
{
|
|
23
|
+
schema: [{
|
|
16
24
|
type: `object`,
|
|
17
25
|
properties: {
|
|
18
26
|
alias: {
|
|
19
27
|
type: `string`,
|
|
20
28
|
}
|
|
21
29
|
}
|
|
22
|
-
}
|
|
23
|
-
],
|
|
30
|
+
}],
|
|
24
31
|
messages: {
|
|
25
32
|
useRelativePathWithinSlice: `Imports within the slice should be relative`
|
|
26
33
|
},
|
|
@@ -44,7 +51,7 @@ const importExportDeclaration = (context) => (node) => {
|
|
|
44
51
|
const pathTo = alias ? value.replace(`${alias}`, ``) : value;
|
|
45
52
|
const currentFile = context.getFilename();
|
|
46
53
|
|
|
47
|
-
if(
|
|
54
|
+
if(isSameModuleImport(currentFile, pathTo, fsdLayers)) {
|
|
48
55
|
context.report({
|
|
49
56
|
node,
|
|
50
57
|
messageId: `useRelativePathWithinSlice`,
|
|
@@ -60,51 +67,8 @@ const importExportDeclaration = (context) => (node) => {
|
|
|
60
67
|
}
|
|
61
68
|
}
|
|
62
69
|
|
|
63
|
-
const fsdLayers = {
|
|
64
|
-
'shared': 'shared',
|
|
65
|
-
'entities': 'entities',
|
|
66
|
-
'features': 'features',
|
|
67
|
-
'widgets': 'widgets',
|
|
68
|
-
'pages': 'pages',
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function isPathRelative(path) {
|
|
72
|
-
return path === `.` || path.startsWith(`./`) || path.startsWith('../');
|
|
73
|
-
}
|
|
74
|
-
|
|
75
70
|
function getProjectPath(fullPath) {
|
|
76
71
|
const fromNormalized = path.toNamespacedPath(fullPath);
|
|
77
72
|
|
|
78
73
|
return fromNormalized.split(`src/`)[1];
|
|
79
74
|
}
|
|
80
|
-
|
|
81
|
-
function shouldBeRelative(from, to) {
|
|
82
|
-
if(isPathRelative(to)) {
|
|
83
|
-
return false;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
const toArray = to.split('/');
|
|
87
|
-
const toLayer = toArray[0];
|
|
88
|
-
const toSlice = toArray[1];
|
|
89
|
-
|
|
90
|
-
if(!toLayer || !toSlice || !fsdLayers[toLayer]) {
|
|
91
|
-
return false;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
const fromNormalized = path.toNamespacedPath(from);
|
|
95
|
-
const fromProject = fromNormalized.split(`src/`)[1];
|
|
96
|
-
|
|
97
|
-
if(!fromProject) {
|
|
98
|
-
return false;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const fromArray = fromProject.split('/');
|
|
102
|
-
const fromLayer = fromArray[0];
|
|
103
|
-
const fromSlice = fromArray[1];
|
|
104
|
-
|
|
105
|
-
if(!fromLayer || !fromSlice || !fsdLayers[fromLayer]) {
|
|
106
|
-
return false;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
return fromSlice === toSlice && fromLayer === toLayer;
|
|
110
|
-
}
|
|
@@ -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",
|