@sj_lk/eslint-plugin-sj-plugin 0.1.4 → 0.1.6
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 +2 -0
- package/lib/rules/layer-imports.js +111 -0
- package/lib/rules/public-api-imports.js +5 -1
- package/package.json +2 -2
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,111 @@
|
|
|
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
|
+
return micromatch.isMatch(importPath, pattern)
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
if(isIgnored) {
|
|
102
|
+
return
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if(!layers[currrentFIleLayer]?.includes(importLayer)) {
|
|
106
|
+
context.report({node, messageId: 'layersImportError'})
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
};
|
|
@@ -13,7 +13,7 @@ module.exports = {
|
|
|
13
13
|
recommended: false,
|
|
14
14
|
url: null, // URL to the documentation page for this rule
|
|
15
15
|
},
|
|
16
|
-
fixable:
|
|
16
|
+
fixable: 'code', // Or `code` or `whitespace`
|
|
17
17
|
schema: [
|
|
18
18
|
{
|
|
19
19
|
type: 'object',
|
|
@@ -56,6 +56,7 @@ module.exports = {
|
|
|
56
56
|
const segments = importTo.split('/')
|
|
57
57
|
|
|
58
58
|
const layer = segments[0]
|
|
59
|
+
const slice = segments[1]
|
|
59
60
|
|
|
60
61
|
if(!allowedLayers[layer]) {
|
|
61
62
|
return;
|
|
@@ -68,6 +69,9 @@ module.exports = {
|
|
|
68
69
|
context.report({
|
|
69
70
|
node,
|
|
70
71
|
messageId: 'importProblem',
|
|
72
|
+
fix: (fixer) => {
|
|
73
|
+
return fixer.replaceText(node.source, `'${alias}/${layer}/${slice}'`)
|
|
74
|
+
}
|
|
71
75
|
})
|
|
72
76
|
}
|
|
73
77
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sj_lk/eslint-plugin-sj-plugin",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "add
|
|
3
|
+
"version": "0.1.6",
|
|
4
|
+
"description": "add feature sliced relative path checker, validator for layers imports and absolute imports from a public API",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"eslint",
|
|
7
7
|
"eslintplugin",
|