eslint-plugin-fsd-paths-check 0.0.3 → 0.0.4
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.
|
@@ -4,15 +4,17 @@ const path = require("path");
|
|
|
4
4
|
|
|
5
5
|
module.exports = {
|
|
6
6
|
meta: {
|
|
7
|
-
type:
|
|
7
|
+
type: `suggestion`, // `problem`, `suggestion`, or `layout`
|
|
8
8
|
docs: {
|
|
9
9
|
description: "FSD relative path check",
|
|
10
10
|
recommended: false,
|
|
11
11
|
url: null, // URL to the documentation page for this rule
|
|
12
12
|
},
|
|
13
|
-
fixable:
|
|
13
|
+
fixable: `code`,
|
|
14
14
|
schema: [], // Add a schema if the rule has options
|
|
15
|
-
messages: {
|
|
15
|
+
messages: {
|
|
16
|
+
useRelativePathWithinSlice: `Imports within the slice should be relative`
|
|
17
|
+
},
|
|
16
18
|
},
|
|
17
19
|
|
|
18
20
|
create(context) {
|
|
@@ -22,7 +24,17 @@ module.exports = {
|
|
|
22
24
|
const currentFile = context.getFilename();
|
|
23
25
|
|
|
24
26
|
if(shouldBeRelative(currentFile, importTo)) {
|
|
25
|
-
context.report(
|
|
27
|
+
context.report({
|
|
28
|
+
node,
|
|
29
|
+
messageId: `useRelativePathWithinSlice`,
|
|
30
|
+
fix(fixer) {
|
|
31
|
+
const projectFromPath = getProjectPath(currentFile);
|
|
32
|
+
const currentDir = path.dirname(projectFromPath);
|
|
33
|
+
const relativePath = path.relative(currentDir, node.source.value);
|
|
34
|
+
|
|
35
|
+
return fixer.replaceText(node.source, `"${relativePath}"`);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
26
38
|
}
|
|
27
39
|
}
|
|
28
40
|
};
|
|
@@ -41,6 +53,12 @@ function isPathRelative(path) {
|
|
|
41
53
|
return path === `.` || path.startsWith(`./`) || path.startsWith('../');
|
|
42
54
|
}
|
|
43
55
|
|
|
56
|
+
function getProjectPath(fullPath) {
|
|
57
|
+
const fromNormalized = path.toNamespacedPath(fullPath);
|
|
58
|
+
|
|
59
|
+
return fromNormalized.split(`src/`)[1];
|
|
60
|
+
}
|
|
61
|
+
|
|
44
62
|
function shouldBeRelative(from, to) {
|
|
45
63
|
if(isPathRelative(to)) {
|
|
46
64
|
return false;
|