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

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.
@@ -12,7 +12,7 @@ module.exports = {
12
12
  recommended: false,
13
13
  url: null, // URL to the documentation page for this rule
14
14
  },
15
- fixable: null, // Or `code` or `whitespace`
15
+ fixable: "code", // Or `code` or `whitespace`
16
16
  schema: [
17
17
  {
18
18
  type: "object",
@@ -50,6 +50,21 @@ module.exports = {
50
50
  context.report({
51
51
  node,
52
52
  messageId: "relativePathRequired",
53
+ fix: (fixer) => {
54
+ const normalizedPath = getNormalizedCurrentFilePath(fromFilename)
55
+ .split("/")
56
+ .slice(0, -1)
57
+ .join("/");
58
+ let relativePath = path
59
+ .relative(normalizedPath, `/${importTo}`)
60
+ .split("\\")
61
+ .join("/");
62
+
63
+ if (relativePath.startsWith(".")) {
64
+ relativePath = "./" + relativePath;
65
+ }
66
+ return fixer.replaceText(node.source, relativePath);
67
+ },
53
68
  });
54
69
  }
55
70
  },
@@ -65,6 +80,12 @@ const layers = {
65
80
  widgets: "widgets",
66
81
  };
67
82
 
83
+ function getNormalizedCurrentFilePath(currentFilePath) {
84
+ const normalizedPath = path.toNamespacedPath(currentFilePath);
85
+ const projectFrom = normalizedPath.split("src")[1];
86
+ return projectFrom.split("\\").join("/");
87
+ }
88
+
68
89
  function shouldBeRelative(from, to) {
69
90
  if (isPathRelative(to)) {
70
91
  return false;
@@ -76,9 +97,8 @@ function shouldBeRelative(from, to) {
76
97
 
77
98
  if (!toLayer || !toSlice || !layers[toLayer]) return false;
78
99
 
79
- const normalizedPath = path.toNamespacedPath(from);
80
- const projectFrom = normalizedPath.split("src")[1];
81
- const fromArray = projectFrom.split("\\");
100
+ const projectFrom = getNormalizedCurrentFilePath(from);
101
+ const fromArray = projectFrom.split("/");
82
102
 
83
103
  const fromLayer = fromArray[1];
84
104
  const fromSlice = fromArray[2];
@@ -3,6 +3,9 @@
3
3
  const { isPathRelative } = require("../helpers");
4
4
  const micromatch = require("micromatch");
5
5
 
6
+ const PUBLIC_ERROR = "PUBLIC_ERROR";
7
+ const TESTING_PUBLIC_API = "TESTING_PUBLIC_API";
8
+
6
9
  /** @type {import('eslint').Rule.RuleModule} */
7
10
  module.exports = {
8
11
  meta: {
@@ -12,7 +15,7 @@ module.exports = {
12
15
  recommended: false,
13
16
  url: null, // URL to the documentation page for this rule
14
17
  },
15
- fixable: null, // Or `code` or `whitespace`
18
+ fixable: "code", // Or `code` or `whitespace`
16
19
  schema: [
17
20
  {
18
21
  type: "object",
@@ -34,9 +37,9 @@ module.exports = {
34
37
  },
35
38
  ],
36
39
  messages: {
37
- publicApiRequired:
40
+ PUBLIC_ERROR:
38
41
  "Абсолютный импорт разрешен только из Publick API(index.ts)",
39
- testingPublicApi:
42
+ TESTING_PUBLIC_API:
40
43
  "Тестовые данных необходимо импортировать из publicApi/testing.ts",
41
44
  },
42
45
  },
@@ -64,6 +67,8 @@ module.exports = {
64
67
  const segments = importTo.split("/");
65
68
 
66
69
  const layer = segments[0];
70
+ const slice = segments[1];
71
+
67
72
  if (!checkingLayers[layer]) {
68
73
  return;
69
74
  }
@@ -75,7 +80,13 @@ module.exports = {
75
80
  if (isImportNotFromPublicApi && !isTestingPublicApi) {
76
81
  context.report({
77
82
  node,
78
- messageId: "publicApiRequired",
83
+ messageId: PUBLIC_ERROR,
84
+ fix: (fixer) => {
85
+ return fixer.replaceText(
86
+ node.source,
87
+ `'${alias}/${layer}/${slice}'`,
88
+ );
89
+ },
79
90
  });
80
91
  }
81
92
 
@@ -88,7 +99,7 @@ module.exports = {
88
99
  if (!isCurrentFileTesting) {
89
100
  context.report({
90
101
  node,
91
- messageId: "testingPublicApi",
102
+ messageId: TESTING_PUBLIC_API,
92
103
  });
93
104
  }
94
105
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-import-path-correct",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "description": "plugin for paths",
5
5
  "keywords": [
6
6
  "eslint",