@sj_lk/eslint-plugin-sj-plugin 0.0.5 → 0.1.1
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/rules/path-checker.js +63 -14
- package/package.json +2 -2
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
/** @type {import('eslint').Rule.RuleModule} */
|
|
4
|
+
const path = require('path');
|
|
5
|
+
|
|
4
6
|
const rule = {
|
|
5
7
|
meta: {
|
|
6
8
|
type: "suggestion", // `problem`, `suggestion`, or `layout`
|
|
@@ -10,32 +12,79 @@ const rule = {
|
|
|
10
12
|
url: null, // URL to the documentation page for this rule
|
|
11
13
|
},
|
|
12
14
|
fixable: null, // Or `code` or `whitespace`
|
|
13
|
-
schema: [
|
|
15
|
+
schema: [
|
|
16
|
+
{
|
|
17
|
+
type: 'object',
|
|
18
|
+
properties: {
|
|
19
|
+
alias: {
|
|
20
|
+
type: 'string',
|
|
21
|
+
description: 'alias for the rule',
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
], defaultOptions: [{alias: ''}], // Add a schema if the rule has options
|
|
14
26
|
messages: {
|
|
15
|
-
someProblem: '
|
|
27
|
+
someProblem: 'В рамках одного слайса все пути должны быть относительными',
|
|
16
28
|
}, // Add messageId and message
|
|
17
29
|
},
|
|
18
30
|
|
|
19
31
|
create(context) {
|
|
32
|
+
const alias = context.options[0]?.alias || ''
|
|
20
33
|
return {
|
|
21
34
|
ImportDeclaration(node) {
|
|
22
|
-
|
|
23
|
-
const importTo =
|
|
24
|
-
|
|
25
|
-
//example /Users/LK/Desktop/ulbiPrLinter/eslint-plugin-test1/lib/rules/path-checker.js
|
|
35
|
+
const value = node.source.value
|
|
36
|
+
const importTo = alias ? value.replace(`${alias}/`, '') : value
|
|
26
37
|
const fromFilename = context.filename
|
|
27
38
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
})
|
|
39
|
+
if(shouldBeRelative(fromFilename, importTo)) {
|
|
40
|
+
context.report({
|
|
41
|
+
node,
|
|
42
|
+
messageId: 'someProblem',
|
|
43
|
+
})
|
|
44
|
+
}
|
|
36
45
|
}
|
|
37
46
|
};
|
|
38
47
|
},
|
|
39
48
|
};
|
|
40
49
|
|
|
41
50
|
module.exports = rule;
|
|
51
|
+
|
|
52
|
+
const layers = {
|
|
53
|
+
'entities': 'entities',
|
|
54
|
+
'features': 'features',
|
|
55
|
+
'shared': 'shared',
|
|
56
|
+
'pages': 'pages',
|
|
57
|
+
'widgets': 'widgets'
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function isPathRelative(path) {
|
|
61
|
+
return path === '.' || path.startsWith('./') || path.startsWith('../')
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function shouldBeRelative(from, to) {
|
|
65
|
+
|
|
66
|
+
if (isPathRelative(to)) {
|
|
67
|
+
return false
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const toArray = to.split('/');
|
|
71
|
+
const toLayer = toArray[0]
|
|
72
|
+
const toSlice = toArray[1]
|
|
73
|
+
|
|
74
|
+
if(!toLayer || !toSlice || !layers[toLayer]) {
|
|
75
|
+
return false
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const normalizedPath = path.toNamespacedPath(from)
|
|
79
|
+
const projectFrom = normalizedPath.split('src')[1]
|
|
80
|
+
const fromArray = projectFrom.split('/')
|
|
81
|
+
|
|
82
|
+
const fromLayer = fromArray[1]
|
|
83
|
+
const fromSlice = fromArray[2]
|
|
84
|
+
|
|
85
|
+
if(!fromLayer || !fromSlice || !layers[fromLayer]) {
|
|
86
|
+
return false
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return fromLayer === toLayer && fromSlice === toSlice;
|
|
90
|
+
}
|