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