@sj_lk/eslint-plugin-sj-plugin 0.1.3 → 0.1.5

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/lib/index.js CHANGED
@@ -11,12 +11,14 @@
11
11
 
12
12
  const pathChecker = require("./rules/path-checker")
13
13
  const publicApiImports = require("./rules/public-api-imports")
14
+ const layerImports = require("./rules/layer-imports")
14
15
  //------------------------------------------------------------------------------
15
16
  // Plugin Definition
16
17
  const plugin = {
17
18
  rules: {
18
19
  "path-checker": pathChecker,
19
20
  "public-api-imports": publicApiImports,
21
+ "layer-imports": layerImports
20
22
  },
21
23
  };
22
24
  //------------------------------------------------------------------------------
@@ -0,0 +1,112 @@
1
+ /**
2
+ * @fileoverview description
3
+ * @author sj_lk
4
+ */
5
+ "use strict";
6
+
7
+ //------------------------------------------------------------------------------
8
+ // Rule Definition
9
+ //------------------------------------------------------------------------------
10
+
11
+ /** @type {import('eslint').Rule.RuleModule} */
12
+ const path = require('path');
13
+ const micromatch = require('micromatch')
14
+ const { isPathRelative } = require('../helpers');
15
+
16
+ module.exports = {
17
+ meta: {
18
+ type: 'suggestion', // `problem`, `suggestion`, or `layout`
19
+ docs: {
20
+ description: "valdator for layers imports",
21
+ recommended: false,
22
+ url: null, // URL to the documentation page for this rule
23
+ },
24
+ fixable: null, // Or `code` or `whitespace`
25
+ schema: [
26
+ {
27
+ type: 'object',
28
+ properties: {
29
+ alias: {
30
+ type: 'string',
31
+ description: 'alias for the rule'
32
+ },
33
+ ignoreImportPatterns: {
34
+ type: 'array',
35
+ description: 'patterns for ignore'
36
+ }
37
+ }
38
+ }
39
+ ], defaultOptions: [{alias: ''}], // Add a schema if the rule has options
40
+ messages: {
41
+ layersImportError: 'Слой может импортировать в себя только нижележащие слои (shared, entities, features, widgets, pages, app'
42
+ }, // Add messageId and message
43
+ },
44
+
45
+ create(context) {
46
+ const layers = {
47
+ 'app': ['pages', 'widgets', 'features', 'shared', 'entities'],
48
+ 'pages': ['widgets', 'features', 'shared', 'entities'],
49
+ 'widgets': ['features', 'shared', 'entities'],
50
+ 'features': ['shared', 'entities'],
51
+ 'entities': ['shared', 'entities'],
52
+ 'shared': ['shared'],
53
+ }
54
+
55
+ const availableLayers = {
56
+ 'app': 'app',
57
+ 'entities': 'entities',
58
+ 'features': 'features',
59
+ 'shared': 'shared',
60
+ 'pages': 'pages',
61
+ 'widgets': 'widgets'
62
+ }
63
+
64
+ const { alias = '', ignoreImportPatterns = []} = context.options[0] ?? {}
65
+
66
+ const getCurrrentFileLayer = () => {
67
+ const currentFilePath = context.filename
68
+
69
+ const normalizedPath = path.toNamespacedPath(currentFilePath)
70
+ const projectPath = normalizedPath?.split('src')[1]
71
+ const segments = projectPath?.split('/')
72
+
73
+ return segments?.[1]
74
+ }
75
+
76
+ const getImportLayer = (value) => {
77
+ const importPath = alias ? value.replace(`${alias}/`, '') : value
78
+ const segments = importPath?.split('/')
79
+
80
+ return segments?.[0]
81
+ }
82
+
83
+ return {
84
+ ImportDeclaration(node) {
85
+ const importPath = node.source.value
86
+ const currrentFIleLayer = getCurrrentFileLayer()
87
+ const importLayer = getImportLayer(importPath)
88
+
89
+ if(isPathRelative(importPath)) {
90
+ return
91
+ }
92
+
93
+ if(!availableLayers[importLayer] || !availableLayers[currrentFIleLayer]) {
94
+ return
95
+ }
96
+
97
+ const isIgnored = ignoreImportPatterns.some(pattern => {
98
+ console.log(importPath, pattern)
99
+ return micromatch.isMatch(importPath, pattern)
100
+ })
101
+
102
+ if(isIgnored) {
103
+ return
104
+ }
105
+
106
+ if(!layers[currrentFIleLayer]?.includes(importLayer)) {
107
+ context.report({node, messageId: 'layersImportError'})
108
+ }
109
+ }
110
+ }
111
+ },
112
+ };
@@ -3,6 +3,7 @@
3
3
  /** @type {import('eslint').Rule.RuleModule} */
4
4
 
5
5
  const { isPathRelative } = require('../helpers')
6
+ const micromatch = require('micromatch')
6
7
 
7
8
  module.exports = {
8
9
  meta: {
@@ -20,17 +21,22 @@ module.exports = {
20
21
  alias: {
21
22
  type: 'string',
22
23
  description: 'alias for the rule',
24
+ },
25
+ testFilesPatterns: {
26
+ type: 'array',
27
+ description: 'patterns to match test files',
23
28
  }
24
29
  },
25
30
  }
26
31
  ], defaultOptions: [{alias: ''}],
27
32
  messages: {
28
- importProblem: 'Абсолютный импорт разрешен только из Public API (index.ts)'
33
+ importProblem: 'Абсолютный импорт разрешен только из Public API (index.ts)',
34
+ testImportProblem: 'Тестовые данные необходимо импортировать из publicAPI (testing.ts)'
29
35
  },
30
36
  },
31
37
 
32
38
  create(context) {
33
- const alias = context.options[0]?.alias || ''
39
+ const { alias = '', testFilesPatterns = []} = context.options[0] ?? {}
34
40
 
35
41
  const allowedLayers = {
36
42
  'entities': 'entities',
@@ -48,6 +54,7 @@ module.exports = {
48
54
  return;
49
55
  }
50
56
  const segments = importTo.split('/')
57
+
51
58
  const layer = segments[0]
52
59
 
53
60
  if(!allowedLayers[layer]) {
@@ -55,14 +62,24 @@ module.exports = {
55
62
  }
56
63
 
57
64
  const isImportNotFromPublicApi = segments.length > 2
65
+ const isTestingPublicApi = segments[2] === 'testing' && segments.length < 4
58
66
 
59
- if(isImportNotFromPublicApi) {
67
+ if(isImportNotFromPublicApi && !isTestingPublicApi) {
60
68
  context.report({
61
69
  node,
62
70
  messageId: 'importProblem',
63
71
  })
64
72
  }
73
+
74
+ if(isTestingPublicApi) {
75
+ const currentFilePath = context.filename
76
+ const isCurrentFileTesting = testFilesPatterns.some(pattern => micromatch.isMatch(currentFilePath, pattern))
77
+
78
+ if(!isCurrentFileTesting) {
79
+ context.report({node, messageId: 'testImportProblem'})
80
+ }
81
+ }
65
82
  }
66
- };
83
+ }
67
84
  },
68
85
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sj_lk/eslint-plugin-sj-plugin",
3
- "version": "0.1.3",
4
- "description": "fixes",
3
+ "version": "0.1.5",
4
+ "description": "add validation for layers imports",
5
5
  "keywords": [
6
6
  "eslint",
7
7
  "eslintplugin",
@@ -21,11 +21,12 @@
21
21
  "update:eslint-docs": "eslint-doc-generator"
22
22
  },
23
23
  "dependencies": {
24
+ "micromatch": "^4.0.8",
24
25
  "requireindex": "^1.2.0"
25
26
  },
26
27
  "devDependencies": {
27
- "eslint": "^10.0.0",
28
28
  "@eslint/js": "^10.0.1",
29
+ "eslint": "^10.0.0",
29
30
  "eslint-doc-generator": "^2.0.0",
30
31
  "eslint-plugin-eslint-plugin": "^7.3.2",
31
32
  "eslint-plugin-n": "^17.0.0",