@sj_lk/eslint-plugin-sj-plugin 0.0.4 → 0.1.0

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.
@@ -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`
@@ -12,27 +14,65 @@ const rule = {
12
14
  fixable: null, // Or `code` or `whitespace`
13
15
  schema: [], // Add a schema if the rule has options
14
16
  messages: {
15
- someProblem: 'Линтер ругается!',
17
+ someProblem: 'В рамках одного слайса все пути должны быть относительными',
16
18
  }, // Add messageId and message
17
19
  },
18
20
 
19
21
  create(context) {
20
22
  return {
21
23
  ImportDeclaration(node) {
22
- // exemple app/entities/Article
23
24
  const importTo = node.source.value
24
-
25
- //example /Users/LK/Desktop/ulbiPrLinter/eslint-plugin-test1/lib/rules/path-checker.js
26
25
  const fromFilename = context.filename
27
26
 
28
- context.report({
29
- node: node,
30
- messageId: 'someProblem',
31
- data: `Из файла: ${fromFilename} -> Импорт в: ${importTo}`
32
- })
27
+ if(shouldBeRelative(fromFilename, importTo)) {
28
+ context.report({
29
+ node,
30
+ messageId: 'someProblem',
31
+ })
32
+ }
33
33
  }
34
34
  };
35
35
  },
36
36
  };
37
37
 
38
- export default rule;
38
+ module.exports = rule;
39
+
40
+ const layers = {
41
+ 'entities': 'entities',
42
+ 'features': 'features',
43
+ 'shared': 'shared',
44
+ 'pages': 'pages',
45
+ 'widgets': 'widgets'
46
+ }
47
+
48
+ function isPathRelative(path) {
49
+ return path === '.' || path.startsWith('./') || path.startsWith('../')
50
+ }
51
+
52
+ function shouldBeRelative(from, to) {
53
+
54
+ if (isPathRelative(to)) {
55
+ return false
56
+ }
57
+
58
+ const toArray = to.split('/');
59
+ const toLayer = toArray[0]
60
+ const toSlice = toArray[1]
61
+
62
+ if(!toLayer || !toSlice || !layers[toLayer]) {
63
+ return false
64
+ }
65
+
66
+ const normalizedPath = path.toNamespacedPath(from)
67
+ const projectFrom = normalizedPath.split('src')[1]
68
+ const fromArray = projectFrom.split('/')
69
+
70
+ const fromLayer = fromArray[1]
71
+ const fromSlice = fromArray[2]
72
+
73
+ if(!fromLayer || !fromSlice || !layers[fromLayer]) {
74
+ return false
75
+ }
76
+
77
+ return fromLayer === toLayer && fromSlice === toSlice;
78
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sj_lk/eslint-plugin-sj-plugin",
3
- "version": "0.0.4",
4
- "description": "fix index.js file",
3
+ "version": "0.1.0",
4
+ "description": "path-checker is working now",
5
5
  "keywords": [
6
6
  "eslint",
7
7
  "eslintplugin",