eslint-plugin-path-checker-relative 0.0.7 → 0.0.9
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/rules/layer-imports.js +116 -0
- package/lib/rules/path-checker.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview fix slice layer
|
|
3
|
+
* @author layer-imports
|
|
4
|
+
*/
|
|
5
|
+
"use strict";
|
|
6
|
+
const path = require("path");
|
|
7
|
+
const isPathRelative = require("../helpers");
|
|
8
|
+
const micromatch = require("micromatch");
|
|
9
|
+
//------------------------------------------------------------------------------
|
|
10
|
+
// Rule Definition
|
|
11
|
+
//------------------------------------------------------------------------------
|
|
12
|
+
|
|
13
|
+
/** @type {import('eslint').Rule.RuleModule} */
|
|
14
|
+
module.exports = {
|
|
15
|
+
meta: {
|
|
16
|
+
type: null, // `problem`, `suggestion`, or `layout`
|
|
17
|
+
docs: {
|
|
18
|
+
description: "fix slice layer",
|
|
19
|
+
recommended: false,
|
|
20
|
+
url: null, // URL to the documentation page for this rule
|
|
21
|
+
},
|
|
22
|
+
fixable: null, // Or `code` or `whitespace`
|
|
23
|
+
schema: [
|
|
24
|
+
{
|
|
25
|
+
type: "object",
|
|
26
|
+
properties: {
|
|
27
|
+
alias: {
|
|
28
|
+
type: "string",
|
|
29
|
+
},
|
|
30
|
+
testFilesPattern: {
|
|
31
|
+
type: "array",
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
],
|
|
36
|
+
messages: [],
|
|
37
|
+
},
|
|
38
|
+
|
|
39
|
+
create(context) {
|
|
40
|
+
const layers = {
|
|
41
|
+
app: ["pages", "widgets", "features", "shared", "entities"],
|
|
42
|
+
pages: ["widgets", "features", "shared", "entities"],
|
|
43
|
+
widgets: ["features", "shared", "entities"],
|
|
44
|
+
features: ["shared", "entities"],
|
|
45
|
+
entities: ["shared", "entities"],
|
|
46
|
+
shared: ["shared"],
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const availableLayers = {
|
|
50
|
+
app: "app",
|
|
51
|
+
entities: "entities",
|
|
52
|
+
features: "features",
|
|
53
|
+
pages: "pages",
|
|
54
|
+
shared: "shared",
|
|
55
|
+
widgets: "widgets",
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const { alias = "", ignoreImportPatterns = [] } = context.options[0] ?? {};
|
|
59
|
+
|
|
60
|
+
const getCurrentFileLayer = () => {
|
|
61
|
+
const currentFile = context.filename;
|
|
62
|
+
|
|
63
|
+
const normalizedPath = path.toNamespacedPath(currentFile);
|
|
64
|
+
const projectPath = normalizedPath?.split("src")[1];
|
|
65
|
+
|
|
66
|
+
const segment = projectPath?.split("/");
|
|
67
|
+
|
|
68
|
+
return segment?.[1];
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const getImportLayer = (value) => {
|
|
72
|
+
const importPath = alias ? value.replace(`${alias}/`, "") : value;
|
|
73
|
+
const segment = importPath?.split("/");
|
|
74
|
+
return segment?.[0];
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
ImportDeclaration(node) {
|
|
79
|
+
const importPath = node.source.value;
|
|
80
|
+
const currentFileLayer = getCurrentFileLayer();
|
|
81
|
+
const importLayer = getImportLayer(importPath);
|
|
82
|
+
|
|
83
|
+
if (isPathRelative(importPath)) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (
|
|
88
|
+
!availableLayers[importLayer] ||
|
|
89
|
+
!availableLayers[currentFileLayer]
|
|
90
|
+
) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const isIgnored = ignoreImportPatterns.some((pattern) =>
|
|
95
|
+
micromatch.isMatch(importPath, pattern)
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
if (isIgnored) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (!layers[currentFileLayer]?.includes(importLayer)) {
|
|
103
|
+
const allowedLayersMessage = `Слой "${currentFileLayer}" может импортировать в себя только нижележащие слои (${layers[
|
|
104
|
+
currentFileLayer
|
|
105
|
+
].join(", ")})`;
|
|
106
|
+
|
|
107
|
+
context.report({
|
|
108
|
+
node: node,
|
|
109
|
+
// eslint-disable-next-line eslint-plugin/prefer-message-ids
|
|
110
|
+
message: allowedLayersMessage,
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
},
|
|
116
|
+
};
|