eslint-plugin-laygrd-plugin 0.0.6 → 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.
package/README.md CHANGED
@@ -2,6 +2,11 @@
2
2
 
3
3
  some usefull fsd violation checkers
4
4
 
5
+ **Git**: https://github.com/Laygrd/eslint-plugin-laygrd-plugin
6
+
7
+ **NPM**: https://www.npmjs.com/package/eslint-plugin-laygrd-plugin
8
+
9
+
5
10
  ## Installation
6
11
 
7
12
  You'll first need to install [ESLint](https://eslint.org/):
@@ -34,14 +39,17 @@ Then configure the rules you want to use under the rules section.
34
39
  ```json
35
40
  {
36
41
  "rules": {
37
- "laygrd-plugin/fsd-relative-path": ['error, { alias: '@' }],
38
- "laygrd-plugin/public-api-imports": ['error, { alias: '@' }]
42
+ "laygrd-plugin/fsd-relative-path": ['error,
43
+ {
44
+ alias: '@',
45
+ }
46
+ ],
47
+ "laygrd-plugin/public-api-imports": ['error,
48
+ {
49
+ alias: '@',
50
+ testFilesPatterns: ['**/\*.test.*', '**/\*.stories.*', '**/StoreDecorator.tsx'],
51
+ }
52
+ ]
39
53
  }
40
54
  }
41
55
  ```
42
-
43
- ## Supported Rules
44
-
45
- * Fill in provided rules here
46
-
47
-
@@ -4,7 +4,7 @@
4
4
  */
5
5
  "use strict";
6
6
  const { isPathRelative } = require('../helpers');
7
-
7
+ const micromatch = require('micromatch');
8
8
 
9
9
  //------------------------------------------------------------------------------
10
10
  // Rule Definition
@@ -28,6 +28,9 @@ module.exports = {
28
28
  properties: {
29
29
  alias: {
30
30
  type: 'string'
31
+ },
32
+ testFilesPatterns: {
33
+ type: 'array'
31
34
  }
32
35
  }
33
36
  }
@@ -36,7 +39,7 @@ module.exports = {
36
39
  },
37
40
 
38
41
  create(context) {
39
- const alias = context.options[0]?.alias || '';
42
+ const { alias = '', testFilesPatterns = [] } = context.options[0] || {};
40
43
 
41
44
  return {
42
45
  ImportDeclaration(node) {
@@ -52,18 +55,34 @@ module.exports = {
52
55
 
53
56
  const segments = importSrc.split('/');
54
57
 
55
- // valid: "import {...} from 'entities/article'" -> ['entities', 'article']
56
- // invalid: "import {...} from 'entities/article/ui/articleCard/articleCard'" -> ['entities', 'article', 'ui', 'articleCard', 'articleCard']
58
+ // "import {...} from 'entities/article'" -> ['entities', 'article'] -> true
59
+ // "import {...} from 'entities/article/ui/articleCard/articleCard'" -> ['entities', 'article', 'ui', 'articleCard', 'articleCard'] -> false
57
60
  const isImportNotFromPublicApi = segments.length > 2;
58
61
 
62
+ // "import {...} from 'entities/article/testing'" -> ['entities', 'article', 'testing'] -> true
63
+ // "import {...} from 'entities/article/lib/mockData'" -> ['entities', 'article', 'lib', 'mockData'] -> fasle
64
+ const isImportFromTestingPublicApi = segments[2] === 'testing' && segments.length < 4;
65
+
59
66
  // check only declared layers from project root
60
67
  const layer = segments[0];
61
68
  if (!layersToCheck[layer]) {
62
69
  return;
63
70
  }
64
71
 
65
- if (isImportNotFromPublicApi) {
66
- context.report({node: node, message: 'Absolute imports should be only from module`s Public API (index.ts). '});
72
+ if (isImportNotFromPublicApi && !isImportFromTestingPublicApi) {
73
+ context.report({node: node, message: 'Absolute imports should be only from module`s Public API (index.ts, index.js). '});
74
+ }
75
+
76
+ if (isImportFromTestingPublicApi) {
77
+ const currentFilePath = context.getFilename();
78
+
79
+ const isCurrentFileTesting = testFilesPatterns.some(
80
+ pattern => micromatch.isMatch( currentFilePath, pattern )
81
+ )
82
+
83
+ if (!isCurrentFileTesting) {
84
+ context.report({node: node, message: 'Test data can only be imported into test files via the testing public API (testing.ts, testing.js). '})
85
+ }
67
86
  }
68
87
  }
69
88
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-laygrd-plugin",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "some usefull fsd rule-checkers",
5
5
  "keywords": [
6
6
  "eslint",
@@ -14,6 +14,7 @@
14
14
  "test": "mocha tests --recursive"
15
15
  },
16
16
  "dependencies": {
17
+ "micromatch": "^4.0.8",
17
18
  "requireindex": "^1.2.0"
18
19
  },
19
20
  "devDependencies": {
@@ -21,39 +21,89 @@ const ruleTester = new RuleTester({
21
21
  });
22
22
  ruleTester.run("public-api-exports", rule, {
23
23
  valid: [
24
+ // index.ts
24
25
  {
25
- filename: 'C:\\Users\\Desktop\\Projects\\blog_app\\src\\entities\\Rating\\ui\\RatingCard\\RatingCard',
26
+ filename: 'C:\\Users\\Desktop\\Projects\\blog_app\\src\\entities\\Rating\\ui\\RatingCard\\RatingCard.tsx',
26
27
  code: "import { Avatar } from '@/widgets/Avatar'",
27
28
  errors: [],
28
29
  options: [{ alias: '@' }]
29
30
  },
30
31
  {
31
- filename: 'C:\\Users\\Desktop\\Projects\\blog_app\\src\\widgets\\ThemeSwitcher\\ui\\ThemeSwitcher\\ThemeSwitcher',
32
+ filename: 'C:\\Users\\Desktop\\Projects\\blog_app\\src\\widgets\\ThemeSwitcher\\ui\\ThemeSwitcher\\ThemeSwitcher.tsx',
32
33
  code: "import { Button } from '@/shared/Button/ui/Button'",
33
34
  errors: [],
34
35
  options: [{ alias: '@' }]
35
36
  },
36
37
  {
37
- filename: 'C:\\Users\\Desktop\\Projects\\blog_app\\src\\widgets\\ThemeSwitcher\\ui\\ThemeSwitcher\\ThemeSwitcher',
38
+ filename: 'C:\\Users\\Desktop\\Projects\\blog_app\\src\\widgets\\ThemeSwitcher\\ui\\ThemeSwitcher\\ThemeSwitcher.tsx',
38
39
  code: "import { useCallback } from 'react'",
39
40
  errors: [],
40
41
  options: [{ alias: '@' }]
41
42
  },
42
-
43
+
44
+ // testing.ts
45
+ {
46
+ filename: 'C:\\Users\\Desktop\\Projects\\blog_app\\src\\entities\\article\\services\\getArticle.test.ts',
47
+ code: "import { mockData } from '@/entities/Article/testing'",
48
+ errors: [],
49
+ options: [{
50
+ alias: '@',
51
+ testFilesPatterns: ['**/*.test.*', '**/*.story.*', '**/StoreDecorator.tsx']
52
+ }]
53
+ },
54
+ {
55
+ filename: 'C:\\Users\\Desktop\\Projects\\blog_app\\src\\shared\\lib\\decorators\\StoreDecorator.tsx',
56
+ code: "import { userReducer } from '@/entities/User/testing'",
57
+ errors: [],
58
+ options: [{
59
+ alias: '@',
60
+ testFilesPatterns: ['**/*.test.*', '**/*.story.*', '**/StoreDecorator.tsx']
61
+ }]
62
+ },
43
63
  ],
44
64
 
45
65
  invalid: [
66
+ // index.ts
46
67
  {
47
- filename: 'C:\\Users\\Desktop\\Projects\\blog_app\\src\\entities\\Rating\\ui\\RatingCard\\RatingCard',
68
+ filename: 'C:\\Users\\Desktop\\Projects\\blog_app\\src\\entities\\Rating\\ui\\RatingCard\\RatingCard.tsx',
48
69
  code: "import { Avatar } from 'widgets/Avatar/ui/Avatar/Avatar'",
49
- errors: [{ message: 'Absolute imports should be only from module`s Public API (index.ts). '}],
70
+ errors: [{ message: 'Absolute imports should be only from module`s Public API (index.ts, index.js). '}],
50
71
  options: [{ alias: '@' }]
51
72
  },
52
73
  {
53
- filename: 'C:\\Users\\Desktop\\Projects\\blog_app\\src\\widgets\\ThemeSwitcher\\ui\\ThemeSwitcher\\ThemeSwitcher',
74
+ filename: 'C:\\Users\\Desktop\\Projects\\blog_app\\src\\widgets\\ThemeSwitcher\\ui\\ThemeSwitcher\\ThemeSwitcher.tsx',
54
75
  code: "import { Theme } from '@/entities/Theme/lib/themes'",
55
- errors: [{ message: 'Absolute imports should be only from module`s Public API (index.ts). '}],
76
+ errors: [{ message: 'Absolute imports should be only from module`s Public API (index.ts, index.js). '}],
56
77
  options: [{ alias: '@' }]
57
78
  },
79
+
80
+ // testing.ts
81
+ {
82
+ filename: 'C:\\Users\\Desktop\\Projects\\blog_app\\src\\entities\\Article\\ui\\ArticleCard\\ArticleCard.tsx',
83
+ code: "import { mockData } from '@/entities/Article/testing'",
84
+ errors: [{ message: 'Test data can only be imported into test files via the testing public API (testing.ts, testing.js). '}],
85
+ options: [{
86
+ alias: '@',
87
+ testFilesPatterns: ['**/*.test.*', '**/*.story.*', '**/StoreDecorator.tsx']
88
+ }]
89
+ },
90
+ {
91
+ filename: 'C:\\Users\\Desktop\\Projects\\blog_app\\src\\entities\\Article\\services\\getArticle.test.ts',
92
+ code: "import { mockData } from '@/entities/Article/lib/consts'",
93
+ errors: [{ message: 'Absolute imports should be only from module`s Public API (index.ts, index.js). '}],
94
+ options: [{
95
+ alias: '@',
96
+ testFilesPatterns: ['**/*.test.*', '**/*.story.*', '**/StoreDecorator.tsx']
97
+ }]
98
+ },
99
+ {
100
+ filename: 'C:\\Users\\Desktop\\Projects\\blog_app\\src\\shared\\lib\\decorators\\StoreDecorator.tsx',
101
+ code: "import { userReducer } from '@/entities/User/testing/reducer.ts'",
102
+ errors: [{ message: 'Absolute imports should be only from module`s Public API (index.ts, index.js). '}],
103
+ options: [{
104
+ alias: '@',
105
+ testFilesPatterns: ['**/*.test.*', '**/*.story.*', '**/StoreDecorator.tsx']
106
+ }]
107
+ },
58
108
  ],
59
109
  });