eslint-plugin-import-path-correct 0.0.1 → 0.0.3

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