eslint-plugin-big-react-app-plugin 0.2.2 → 0.2.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,94 @@
1
+ "use strict";
2
+ const micromatch = require("micromatch");
3
+ const {isPathRelative} = require("../helpers")
4
+ const path = require("path");
5
+
6
+ const PUBLIC_ERROR = 'PUBLIC_ERROR'
7
+ const TEST_PUBLIC_ERROR = 'TEST_PUBLIC_ERROR'
8
+ module.exports = {
9
+ meta: {
10
+ type: null,
11
+ docs: {
12
+ description: "description",
13
+ category: "Fill me in",
14
+ recommended: false,
15
+ url: null,
16
+ },
17
+ fixable: code,
18
+ messages:{
19
+ [PUBLIC_ERROR]:'Абсолютный импорт разрешен только из Public API (index.ts)',
20
+ [TEST_PUBLIC_ERROR]:'Тестовые данные необходимо импортировать из publicApi/testing.ts'
21
+ },
22
+ schema: [
23
+ {
24
+ type: 'object',
25
+ properties: {
26
+ alias: {
27
+ type: 'string'
28
+ },
29
+ testFilesPatterns: {
30
+ type: 'array'
31
+ }
32
+ }
33
+ }
34
+ ],
35
+ },
36
+
37
+
38
+ create(context) {
39
+ const checkingLayers = {
40
+ "entities":"entities",
41
+ "features":"features",
42
+ "pages":"pages",
43
+ "widgets":"widgets",
44
+ }
45
+ const {alias='', testFilesPatterns=[]} = context.options[0] ?? {};
46
+ return {
47
+ ImportDeclaration(node) {
48
+ // example app/entities/Article
49
+ const value = node.source.value;
50
+ const importTo = alias ? value.replace(`${alias}/`, "") : value;
51
+
52
+ //если путь относительный то выходим
53
+ if(isPathRelative(importTo)){
54
+ return;
55
+ }
56
+ //[enteties, article, ... далее еще что-то может быть]
57
+ const segments = importTo.split('/')
58
+ const layer = segments[0]//получаем слой
59
+ const slice = segments[1]//получаем slice Article
60
+ // проверяем что есть слой из массива
61
+
62
+ //[enteties, article, testing]
63
+ const isTestingPublicApi = segments[2] === 'testing' && segments.length < 4;
64
+
65
+ if(!checkingLayers[layer]){
66
+ return;
67
+ }
68
+ const isImportNotFromPublicApi = segments.length > 2;
69
+
70
+ if(isImportNotFromPublicApi && !isTestingPublicApi ) {
71
+ context.report({
72
+ node,
73
+ messageId:PUBLIC_ERROR,
74
+ fix:(fixer)=>{
75
+ return fixer.replaceText(node.source, `'${alias}/${layer}/${slice}'`)
76
+ }
77
+ });
78
+
79
+ }
80
+
81
+ if(isTestingPublicApi){
82
+ const currentFilePath = context.getFilename();
83
+ const normalizedPath = currentFilePath.split(path.sep).join('/');
84
+ const isCurrentFileTesting = testFilesPatterns.some(pattern=>micromatch.isMatch(normalizedPath, pattern))
85
+ if(!isCurrentFileTesting){
86
+ context.report({
87
+ node,
88
+ messageId:TEST_PUBLIC_ERROR});
89
+ }
90
+ }
91
+ }
92
+ };
93
+ },
94
+ };
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ const micromatch = require("micromatch");
3
+ const {isPathRelative} = require("../helpers")
4
+ const path = require("path");
5
+
6
+ const PUBLIC_ERROR = 'PUBLIC_ERROR'
7
+ const TEST_PUBLIC_ERROR = 'TEST_PUBLIC_ERROR'
8
+ module.exports = {
9
+ meta: {
10
+ type: null,
11
+ docs: {
12
+ description: "description",
13
+ category: "Fill me in",
14
+ recommended: false,
15
+ url: null,
16
+ },
17
+ fixable: code,
18
+ messages:{
19
+ [PUBLIC_ERROR]:'Абсолютный импорт разрешен только из Public API (index.ts)',
20
+ [TEST_PUBLIC_ERROR]:'Тестовые данные необходимо импортировать из publicApi/testing.ts'
21
+ },
22
+ schema: [
23
+ {
24
+ type: 'object',
25
+ properties: {
26
+ alias: {
27
+ type: 'string'
28
+ },
29
+ testFilesPatterns: {
30
+ type: 'array'
31
+ }
32
+ }
33
+ }
34
+ ],
35
+ },
36
+
37
+
38
+ create(context) {
39
+ const checkingLayers = {
40
+ "entities":"entities",
41
+ "features":"features",
42
+ "pages":"pages",
43
+ "widgets":"widgets",
44
+ }
45
+ const {alias='', testFilesPatterns=[]} = context.options[0] ?? {};
46
+ return {
47
+ ImportDeclaration(node) {
48
+ // example app/entities/Article
49
+ const value = node.source.value;
50
+ const importTo = alias ? value.replace(`${alias}/`, "") : value;
51
+
52
+ //если путь относительный то выходим
53
+ if(isPathRelative(importTo)){
54
+ return;
55
+ }
56
+ //[enteties, article, ... далее еще что-то может быть]
57
+ const segments = importTo.split('/')
58
+ const layer = segments[0]//получаем слой
59
+ const slice = segments[1]//получаем slice Article
60
+ // проверяем что есть слой из массива
61
+
62
+ //[enteties, article, testing]
63
+ const isTestingPublicApi = segments[2] === 'testing' && segments.length < 4;
64
+
65
+ if(!checkingLayers[layer]){
66
+ return;
67
+ }
68
+ const isImportNotFromPublicApi = segments.length > 2;
69
+
70
+ if(isImportNotFromPublicApi && !isTestingPublicApi ) {
71
+ context.report({
72
+ node,
73
+ messageId:PUBLIC_ERROR,
74
+ fix:(fixer)=>{
75
+ return fixer.replaceText(node.source, `'${alias}/${layer}/${slice}'`)
76
+ }
77
+ });
78
+
79
+ }
80
+
81
+ if(isTestingPublicApi){
82
+ const currentFilePath = context.getFilename();
83
+ const normalizedPath = currentFilePath.split(path.sep).join('/');
84
+ const isCurrentFileTesting = testFilesPatterns.some(pattern=>micromatch.isMatch(normalizedPath, pattern))
85
+ if(!isCurrentFileTesting){
86
+ context.report({
87
+ node,
88
+ messageId:TEST_PUBLIC_ERROR
89
+ });
90
+ }
91
+ }
92
+ }
93
+ };
94
+ },
95
+ };
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "eslint-plugin-big-react-app-plugin",
3
+ "version": "0.2.3",
4
+ "description": "plugin for prod proj",
5
+ "keywords": [
6
+ "eslint",
7
+ "eslintplugin",
8
+ "eslint-plugin"
9
+ ],
10
+ "author": "Vasilii",
11
+ "main": "lib/index.js",
12
+ "scripts": {
13
+ "lint": "eslint .",
14
+ "test": "mocha tests --recursive"
15
+ },
16
+ "dependencies": {
17
+ "micromatch": "^4.0.5",
18
+ "requireindex": "^1.2.0"
19
+ },
20
+ "devDependencies": {
21
+ "eslint": "^8.0.1",
22
+ "eslint-plugin-eslint-plugin": "^4.0.1",
23
+ "eslint-plugin-node": "^11.1.0",
24
+ "mocha": "^9.1.3"
25
+ },
26
+ "engines": {
27
+ "node": "12.x || 14.x || >= 16"
28
+ },
29
+ "peerDependencies": {
30
+ "eslint": ">=6"
31
+ },
32
+ "license": "ISC"
33
+ }
@@ -83,7 +83,10 @@ module.exports = {
83
83
  const normalizedPath = currentFilePath.split(path.sep).join('/');
84
84
  const isCurrentFileTesting = testFilesPatterns.some(pattern=>micromatch.isMatch(normalizedPath, pattern))
85
85
  if(!isCurrentFileTesting){
86
- context.report({node, messsageId:TEST_PUBLIC_ERROR});
86
+ context.report({
87
+ node,
88
+ messageId:TEST_PUBLIC_ERROR
89
+ });
87
90
  }
88
91
  }
89
92
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-big-react-app-plugin",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "plugin for prod proj",
5
5
  "keywords": [
6
6
  "eslint",