eslint-plugin-laygrd-plugin 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.
package/.eslintrc.js CHANGED
@@ -7,6 +7,9 @@ module.exports = {
7
7
  "plugin:eslint-plugin/recommended",
8
8
  "plugin:node/recommended",
9
9
  ],
10
+ parserOptions: {
11
+ ecmaVersion: '2020'
12
+ },
10
13
  env: {
11
14
  node: true,
12
15
  },
@@ -4,6 +4,7 @@
4
4
  */
5
5
  "use strict";
6
6
 
7
+ const path = require('path');
7
8
  //------------------------------------------------------------------------------
8
9
  // Rule Definition
9
10
  //------------------------------------------------------------------------------
@@ -13,29 +14,82 @@
13
14
  */
14
15
  module.exports = {
15
16
  meta: {
17
+ // eslint-disable-next-line eslint-plugin/require-meta-type
16
18
  type: null, // `problem`, `suggestion`, or `layout`
17
19
  docs: {
18
20
  description: "check relative paths inside module ",
19
- category: "Fill me in",
21
+ category: "fsd",
20
22
  recommended: false,
21
23
  url: null, // URL to the documentation page for this rule
22
24
  },
23
25
  fixable: null, // Or `code` or `whitespace`
24
- schema: [], // Add a schema if the rule has options
26
+ schema: [
27
+ {
28
+ type: 'object',
29
+ properties: {
30
+ alias: {
31
+ type: 'string'
32
+ }
33
+ }
34
+ }
35
+ ], // Add a schema if the rule has options
25
36
  },
26
37
 
27
38
  create(context) {
39
+ const alias = context.options[0]?.alias || '';
40
+ console.log('GIVEN ALIAS:', alias);
28
41
 
29
42
  return {
30
43
  ImportDeclaration(node) {
44
+ const value = node.source.value;
45
+
31
46
  // import from 'shared/Button/Button' <--- this path
32
- const importSrc = node.source.value;
47
+ const importSrc = alias ? value.replace(`${alias}/`, '') : value;
33
48
 
34
49
  // location of file where current import statement is
35
50
  const currentFileName = context.getFilename();
36
51
 
37
- context.report(node, `Линтер ругается!! \nimportSrc: ${importSrc}\ncurrentFilename: ${currentFileName}`)
52
+ if (shouldPathBeRelative(currentFileName, importSrc)) {
53
+ context.report({node: node, message: 'Imports within the same module must be relative, according to FSD. '});
54
+ }
38
55
  }
39
56
  };
40
57
  },
41
58
  };
59
+
60
+ const layers = {
61
+ 'pages':'pages',
62
+ 'widgets':'widgets',
63
+ 'features':'features',
64
+ 'entities':'entities',
65
+ 'shared':'shared',
66
+ };
67
+
68
+ const isPathRelative = (p) => {
69
+ return p === '.' || p.startsWith('./') || p.startsWith('../');
70
+ };
71
+
72
+ const shouldPathBeRelative = (curFile, impSrc) => {
73
+ if (isPathRelative(impSrc)) {
74
+ return false;
75
+ }
76
+
77
+ const inpSrcArray = impSrc.split('/'); // entities/Profile/ui/ProfileCard -> [entities, Profile, ui, ProfileCard]
78
+ const inpSrcLayer = inpSrcArray[0]; // entities
79
+ const inpSrcSlice = inpSrcArray[1]; // Profile
80
+
81
+ if ( !inpSrcLayer || !inpSrcSlice || !layers[inpSrcLayer] ) {
82
+ return false;
83
+ }
84
+
85
+ const normalizedPath = curFile.split('src')[1]; // 'C:\Users\rosti\Desktop\projects\blog-app\src\entities\Article\ui\ArticleList' -> \entities\Article\ui\ArticleList
86
+ const projectFileArray = normalizedPath.split(path.sep); // \entities\Article\ui\ArticleList -> [ '', 'entities', 'Article', 'ui', 'ArticleList' ]
87
+ const curFileLayer = projectFileArray[1]; // entities
88
+ const curFileSlice = projectFileArray[2]; // Article
89
+
90
+ if ( !curFileLayer || !curFileSlice || !layers[curFileLayer] ) {
91
+ return false;
92
+ }
93
+
94
+ return inpSrcLayer === curFileLayer && inpSrcSlice === curFileSlice;
95
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-laygrd-plugin",
3
- "version": "0.0.0",
3
+ "version": "0.0.2",
4
4
  "description": "some usefull fsd violation checkers",
5
5
  "keywords": [
6
6
  "eslint",
@@ -16,16 +16,35 @@ const rule = require("../../../lib/rules/fsd-relative-path"),
16
16
  // Tests
17
17
  //------------------------------------------------------------------------------
18
18
 
19
- const ruleTester = new RuleTester();
19
+ const ruleTester = new RuleTester({
20
+ parserOptions: { ecmaVersion: 6, sourceType: 'module' }
21
+ });
20
22
  ruleTester.run("fsd-relative-path", rule, {
21
23
  valid: [
22
- // give me some code that won't trigger a warning
24
+ {
25
+ filename: 'C:\\Users\\Desctop\\Projects\\blog_app\\src\\entities\\Rating',
26
+ code: "import { RatingCard, RatingCardTheme } from './ui/RatingCard/RatingCard'",
27
+ errors: [],
28
+ },
29
+ {
30
+ filename: 'C:\\Users\\Desctop\\Projects\\blog_app\\src\\features\\ArticleRating\\ui\\ArticleRating\\ArticleRating',
31
+ code: "import { useArticleRating } from '../../api/articleRatingApi'",
32
+ errors: [],
33
+ },
23
34
  ],
24
35
 
25
36
  invalid: [
26
37
  {
27
- code: "",
28
- errors: [{ message: "Fill me in.", type: "Me too" }],
38
+ filename: 'C:\\Users\\Desctop\\Projects\\blog_app\\src\\entities\\Rating',
39
+ code: "import { RatingCard, RatingCardTheme } from '@/entities/Rating/ui/RatingCard/RatingCard'",
40
+ errors: [{ message: "Imports within the same module must be relative, according to FSD. " }],
41
+ options: [{ alias: '@' }]
42
+ },
43
+ {
44
+ filename: 'C:\\Users\\Desctop\\Projects\\blog_app\\src\\features\\ArticleRating\\ui\\ArticleRating\\ArticleRating',
45
+ code: "import { useArticleRating } from '@/features/ArticleRating/api/articleRatingApi'",
46
+ errors: [{ message: "Imports within the same module must be relative, according to FSD. " }],
47
+ options: [{ alias: '@' }]
29
48
  },
30
49
  ],
31
50
  });