eslint-plugin-import-path-correct 0.0.4 → 0.0.5

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,113 @@
1
+ ("use strict");
2
+ const micromatch = require("micromatch");
3
+ const path = require("path");
4
+ const { isPathRelative } = require("../helpers");
5
+ //------------------------------------------------------------------------------
6
+ // Rule Definition
7
+ //------------------------------------------------------------------------------
8
+
9
+ /** @type {import('eslint').Rule.RuleModule} */
10
+ module.exports = {
11
+ meta: {
12
+ type: "layout", // `problem`, `suggestion`, or `layout`
13
+ docs: {
14
+ description: "layter imports",
15
+ recommended: false,
16
+ url: null, // URL to the documentation page for this rule
17
+ },
18
+ fixable: null, // Or `code` or `whitespace`
19
+ schema: [
20
+ {
21
+ type: "object",
22
+ properties: {
23
+ alias: { type: "string", description: "some" },
24
+ ignoreImportPatterns: {
25
+ type: "array",
26
+ description: "description",
27
+ },
28
+ },
29
+ additionalProperties: false,
30
+ },
31
+ ],
32
+ defaultOptions: [
33
+ {
34
+ alias: "",
35
+ ignoreImportPatterns: [],
36
+ },
37
+ ],
38
+ messages: {
39
+ needCurrentImport:
40
+ "Слой может импортировать в себя только нижележащие слои (shared, entities, features, widgets, pages, app)",
41
+ },
42
+ },
43
+
44
+ create(context) {
45
+ const layers = {
46
+ app: ["pages", "widgets", "features", "shared", "entites"],
47
+ pages: ["widgets", "features", "shared", "entites"],
48
+ widgets: ["features", "shared", "entites"],
49
+ features: ["shared", "entites"],
50
+ entites: ["shared", "entites"],
51
+ shared: ["shared"],
52
+ };
53
+
54
+ const availableLayers = {
55
+ app: "app",
56
+ entites: "entites",
57
+ features: "features",
58
+ shared: "shared",
59
+ pages: "pages",
60
+ widgets: "widgets",
61
+ };
62
+
63
+ const { alias = "", ignoreImportPatterns = [] } = context.options[0] ?? {};
64
+
65
+ const getCurrentFileLayer = () => {
66
+ const currentFilePath = context.getFilename();
67
+
68
+ const normalizedPath = path.toNamespacedPath(currentFilePath);
69
+ const projectPath = normalizedPath?.split("src")[1];
70
+ const segments = projectPath?.split("\\");
71
+
72
+ return segments?.[1];
73
+ };
74
+
75
+ const getImportLayer = (value) => {
76
+ const importPath = alias ? value.replace(`${alias}/`, "") : value;
77
+ const segments = importPath?.split("/");
78
+
79
+ return segments?.[0];
80
+ };
81
+
82
+ return {
83
+ ImportDeclaration(node) {
84
+ const importPath = node.source.value;
85
+ const currentFileLayer = getCurrentFileLayer();
86
+ const importLayer = getImportLayer(importPath);
87
+
88
+ if (isPathRelative(importPath)) {
89
+ return;
90
+ }
91
+
92
+ if (
93
+ !availableLayers[importLayer] ||
94
+ !availableLayers[currentFileLayer]
95
+ ) {
96
+ return;
97
+ }
98
+
99
+ const isIgnored = ignoreImportPatterns.some((pattern) => {
100
+ return micromatch.isMatch(importPath, pattern);
101
+ });
102
+
103
+ if (isIgnored) {
104
+ return;
105
+ }
106
+
107
+ if (!layers[currentFileLayer]?.includes(importLayer)) {
108
+ context.report({ node, messageId: "needCurrentImport" });
109
+ }
110
+ },
111
+ };
112
+ },
113
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-import-path-correct",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "plugin for paths",
5
5
  "keywords": [
6
6
  "eslint",