eslint-plugin-zozu-plugin 0.0.0 → 0.0.2

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,28 +1,86 @@
1
1
  "use strict";
2
+
3
+ const path = require('path');
2
4
  module.exports = {
3
5
  meta: {
4
- type: null, // `problem`, `suggestion`, or `layout`
6
+ type: null,
5
7
  docs: {
6
8
  description: "feature sliced relative path checker",
7
9
  recommended: false,
8
- url: null, // URL to the documentation page for this rule
10
+ url: null,
9
11
  },
10
- fixable: null, // Or `code` or `whitespace`
11
- schema: [], // Add a schema if the rule has options
12
- messages: {}, // Add messageId and message
12
+ fixable: null,
13
+ schema: [
14
+ {
15
+ type: 'object',
16
+ properties: {
17
+ alias: {
18
+ type: 'string',
19
+ }
20
+ }
21
+ }
22
+ ],
23
+ messages: {},
13
24
  },
14
25
 
15
26
  create(context) {
27
+ const alias = context.options[0]?.alias || '';
28
+ console.log('ALIAS', alias);
16
29
  return {
17
30
  ImportDeclaration(node) {
18
31
  // example app/entities/Article
19
- const importTo = node.source.value;
32
+ const value = node.source.value;
33
+ const importTo = alias ? value.replace(`${alias}/`, '') : value;
20
34
 
21
35
  // example D:/Alexandr/projects/pet-projects/src/index.tsx
22
36
  const fromFilename = context.getFilename();
23
37
 
24
- context.report(node, 'ЛИНТЕР РУГАЕТСЯ!!!');
38
+ if (shouldBeRelative(fromFilename, importTo)) {
39
+ context.report({node: node, message: 'В рамках одного слайса пути должны быть относительные'});
40
+ }
25
41
  }
26
42
  };
27
43
  },
28
44
  };
45
+
46
+ function isPathRelative(path) {
47
+ return path === '.' || path.startsWith('./') || path.startsWith('../')
48
+ }
49
+
50
+ const layers = {
51
+ 'entities': 'entities',
52
+ 'features': 'features',
53
+ 'shared': 'shared',
54
+ 'pages': 'pages',
55
+ 'widgets': 'widgets',
56
+ }
57
+
58
+ function shouldBeRelative(from, to) {
59
+ if (isPathRelative(to)) {
60
+ return false;
61
+ }
62
+
63
+ const toArray = to.split('/');
64
+ const toLayer = toArray[0];
65
+ const toSlice = toArray[1];
66
+
67
+ if(!toLayer || !toSlice || !layers[toLayer]) {
68
+ return false;
69
+ }
70
+
71
+ const normalizedPath = path.toNamespacedPath(from);
72
+ const projectFrom = normalizedPath.split('src')[1];
73
+ const fromArray = projectFrom.split('\\');
74
+
75
+ const fromLayer = fromArray[1];
76
+ const fromSlice = fromArray[2];
77
+
78
+ if(!fromLayer || !fromSlice || !layers[fromLayer]) {
79
+ return false;
80
+ }
81
+
82
+ return fromSlice === toSlice && toLayer === fromLayer;
83
+ }
84
+
85
+ console.log(path.toNamespacedPath('C:/Users/tim'));
86
+ console.log(path.toNamespacedPath('C:\\Users\\tim'));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-zozu-plugin",
3
- "version": "0.0.0",
3
+ "version": "0.0.2",
4
4
  "description": "plugin for my pet project",
5
5
  "keywords": [
6
6
  "eslint",