eslint-plugin-laygrd-plugin 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.
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,13 +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/rule-name": 2
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
+ ]
38
53
  }
39
54
  }
40
55
  ```
41
-
42
- ## Supported Rules
43
-
44
- * Fill in provided rules here
45
-
46
-
@@ -1,4 +1,4 @@
1
- # absolute imports allowed only from public api file (index.ts) (`public-api-exports`)
1
+ # Absolute imports should be only from module`s Public API (index.ts). (`public-api-imports`)
2
2
 
3
3
  Please describe the origin of the rule here.
4
4
 
@@ -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.5",
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": {
@@ -0,0 +1,109 @@
1
+ /**
2
+ * @fileoverview absolute imports allowed only from public api file (index.ts)
3
+ * @author laygrd
4
+ */
5
+ "use strict";
6
+
7
+ //------------------------------------------------------------------------------
8
+ // Requirements
9
+ //------------------------------------------------------------------------------
10
+
11
+ const rule = require("../../../lib/rules/public-api-imports"),
12
+ RuleTester = require("eslint").RuleTester;
13
+
14
+
15
+ //------------------------------------------------------------------------------
16
+ // Tests
17
+ //------------------------------------------------------------------------------
18
+
19
+ const ruleTester = new RuleTester({
20
+ parserOptions: { ecmaVersion: 6, sourceType: 'module' }
21
+ });
22
+ ruleTester.run("public-api-exports", rule, {
23
+ valid: [
24
+ // index.ts
25
+ {
26
+ filename: 'C:\\Users\\Desktop\\Projects\\blog_app\\src\\entities\\Rating\\ui\\RatingCard\\RatingCard.tsx',
27
+ code: "import { Avatar } from '@/widgets/Avatar'",
28
+ errors: [],
29
+ options: [{ alias: '@' }]
30
+ },
31
+ {
32
+ filename: 'C:\\Users\\Desktop\\Projects\\blog_app\\src\\widgets\\ThemeSwitcher\\ui\\ThemeSwitcher\\ThemeSwitcher.tsx',
33
+ code: "import { Button } from '@/shared/Button/ui/Button'",
34
+ errors: [],
35
+ options: [{ alias: '@' }]
36
+ },
37
+ {
38
+ filename: 'C:\\Users\\Desktop\\Projects\\blog_app\\src\\widgets\\ThemeSwitcher\\ui\\ThemeSwitcher\\ThemeSwitcher.tsx',
39
+ code: "import { useCallback } from 'react'",
40
+ errors: [],
41
+ options: [{ alias: '@' }]
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
+ },
63
+ ],
64
+
65
+ invalid: [
66
+ // index.ts
67
+ {
68
+ filename: 'C:\\Users\\Desktop\\Projects\\blog_app\\src\\entities\\Rating\\ui\\RatingCard\\RatingCard.tsx',
69
+ code: "import { Avatar } from 'widgets/Avatar/ui/Avatar/Avatar'",
70
+ errors: [{ message: 'Absolute imports should be only from module`s Public API (index.ts, index.js). '}],
71
+ options: [{ alias: '@' }]
72
+ },
73
+ {
74
+ filename: 'C:\\Users\\Desktop\\Projects\\blog_app\\src\\widgets\\ThemeSwitcher\\ui\\ThemeSwitcher\\ThemeSwitcher.tsx',
75
+ code: "import { Theme } from '@/entities/Theme/lib/themes'",
76
+ errors: [{ message: 'Absolute imports should be only from module`s Public API (index.ts, index.js). '}],
77
+ options: [{ alias: '@' }]
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
+ },
108
+ ],
109
+ });
@@ -1,59 +0,0 @@
1
- /**
2
- * @fileoverview absolute imports allowed only from public api file (index.ts)
3
- * @author laygrd
4
- */
5
- "use strict";
6
-
7
- //------------------------------------------------------------------------------
8
- // Requirements
9
- //------------------------------------------------------------------------------
10
-
11
- const rule = require("../../../lib/rules/public-api-exports"),
12
- RuleTester = require("eslint").RuleTester;
13
-
14
-
15
- //------------------------------------------------------------------------------
16
- // Tests
17
- //------------------------------------------------------------------------------
18
-
19
- const ruleTester = new RuleTester({
20
- parserOptions: { ecmaVersion: 6, sourceType: 'module' }
21
- });
22
- ruleTester.run("public-api-exports", rule, {
23
- valid: [
24
- {
25
- filename: 'C:\\Users\\Desktop\\Projects\\blog_app\\src\\entities\\Rating\\ui\\RatingCard\\RatingCard',
26
- code: "import { Avatar } from '@/widgets/Avatar'",
27
- errors: [],
28
- options: [{ alias: '@' }]
29
- },
30
- {
31
- filename: 'C:\\Users\\Desktop\\Projects\\blog_app\\src\\widgets\\ThemeSwitcher\\ui\\ThemeSwitcher\\ThemeSwitcher',
32
- code: "import { Button } from '@/shared/Button/ui/Button'",
33
- errors: [],
34
- options: [{ alias: '@' }]
35
- },
36
- {
37
- filename: 'C:\\Users\\Desktop\\Projects\\blog_app\\src\\widgets\\ThemeSwitcher\\ui\\ThemeSwitcher\\ThemeSwitcher',
38
- code: "import { useCallback } from 'react'",
39
- errors: [],
40
- options: [{ alias: '@' }]
41
- },
42
-
43
- ],
44
-
45
- invalid: [
46
- {
47
- filename: 'C:\\Users\\Desktop\\Projects\\blog_app\\src\\entities\\Rating\\ui\\RatingCard\\RatingCard',
48
- code: "import { Avatar } from 'widgets/Avatar/ui/Avatar/Avatar'",
49
- errors: [{ message: 'Absolute imports should be only from module`s Public API (index.ts). '}],
50
- options: [{ alias: '@' }]
51
- },
52
- {
53
- filename: 'C:\\Users\\Desktop\\Projects\\blog_app\\src\\widgets\\ThemeSwitcher\\ui\\ThemeSwitcher\\ThemeSwitcher',
54
- code: "import { Theme } from '@/entities/Theme/lib/themes'",
55
- errors: [{ message: 'Absolute imports should be only from module`s Public API (index.ts). '}],
56
- options: [{ alias: '@' }]
57
- },
58
- ],
59
- });