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