eslint-plugin-path-checker-new 0.0.6 → 0.0.8

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