eslint-plugin-fsd-paths-check 0.0.13 → 0.0.15

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,5 @@
1
+ const allFsdLayers = ['shared', 'entities', 'features', 'widgets', 'pages', 'app'];
2
+
3
+ module.exports = {
4
+ allFsdLayers
5
+ }
@@ -0,0 +1,12 @@
1
+ const {toNamespacedPath} = require("path");
2
+
3
+ function getNormalizedProjectPath(fullPath) {
4
+ const fromNamespaced = toNamespacedPath(fullPath);
5
+ const fromNormalized = fromNamespaced.split(`\\`).join(`/`);
6
+
7
+ return fromNormalized.split(`src/`)[1];
8
+ }
9
+
10
+ module.exports = {
11
+ getNormalizedProjectPath
12
+ };
@@ -1,5 +1,6 @@
1
1
  const { isPathRelative } = require("./isPathRelative");
2
2
  const path = require("path");
3
+ const { getNormalizedProjectPath } = require("./getNormalizedProjectPath");
3
4
 
4
5
  function isSameModuleImport(from, to, fsdLayers, noSlicesLayers) {
5
6
  if(isPathRelative(to)) {
@@ -15,8 +16,7 @@ function isSameModuleImport(from, to, fsdLayers, noSlicesLayers) {
15
16
  return false;
16
17
  }
17
18
 
18
- const fromNormalized = path.toNamespacedPath(from);
19
- const fromProject = fromNormalized.split(`src/`)[1];
19
+ const fromProject = getNormalizedProjectPath(from)
20
20
 
21
21
  if(!fromProject) {
22
22
  return false;
@@ -0,0 +1,23 @@
1
+ const { allFsdLayers } = require("../constants/constants");
2
+
3
+ const getFsdLayerFromPath = (filePath) => {
4
+ const normalizedPath = filePath.replace(/\\/g, '/');
5
+ const segments = normalizedPath.split('/');
6
+
7
+ for (const segment of segments) {
8
+ if (allFsdLayers.includes(segment)) {
9
+ return segment;
10
+ }
11
+ }
12
+
13
+ return null;
14
+ };
15
+
16
+ const hasFsdLayerInPath = (filePath) => {
17
+ return getFsdLayerFromPath(filePath) !== null;
18
+ };
19
+
20
+ module.exports = {
21
+ getFsdLayerFromPath,
22
+ hasFsdLayerInPath,
23
+ };
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
 
3
- const { isPathRelative } = require("../helpers/isPathRelative");
4
- const path = require("path");
3
+ const {isPathRelative} = require("../helpers/isPathRelative");
5
4
  const {isSameModuleImport} = require("../helpers/isSameModuleImport");
5
+ const {getFsdLayerFromPath} = require("../helpers/pathContainsFsdLayer");
6
+ const {allFsdLayers} = require("../constants/constants");
6
7
 
7
8
  const fsdLayers = {
8
9
  'entities': 'entities',
@@ -31,6 +32,7 @@ module.exports = {
31
32
  messages: {
32
33
  useOthersModulesPublicApi: `When importing from other modules, public api should be used`,
33
34
  doNotUseAbsolutePathInSameModuleImport: `When importing from the same module, absolute path should not be used`,
35
+ doNotUseRelativePathsBetweenLayers: `When importing from other modules, absolute path should be used`,
34
36
  },
35
37
  },
36
38
 
@@ -42,36 +44,62 @@ module.exports = {
42
44
  const pathTo = alias ? value.replace(`${alias}`, ``) : value;
43
45
  const currentFile = context.getFilename();
44
46
 
45
- if(isPathRelative(pathTo)) {
47
+ const pathToLayer = getFsdLayerFromPath(pathTo)
48
+ const segments = pathTo.split("/");
49
+
50
+ console.warn(value)
51
+ console.warn(pathTo)
52
+ console.warn(segments)
53
+
54
+
55
+ if(isPathRelative(pathTo) && !pathToLayer) {
46
56
  return;
57
+ } else if(isPathRelative(pathTo)) {
58
+ context.report({
59
+ node,
60
+ messageId: `doNotUseRelativePathsBetweenLayers`,
61
+ fix(fixer) {
62
+ const layerIndex = segments.findIndex(segment => allFsdLayers.includes(segment));
63
+ if (layerIndex === -1) return undefined;
64
+
65
+ const absolutePath = `${alias}${segments.slice(layerIndex).join(`/`)}`;
66
+
67
+ return fixer.replaceText(node.source, `'${absolutePath}'`);
68
+ }
69
+ })
47
70
  }
48
71
 
49
- const segments = pathTo.split("/");
50
72
  const layer = segments[0];
51
-
52
73
  if(!fsdLayers[layer]) {
53
74
  return;
54
75
  }
76
+ console.warn(layer)
55
77
 
56
78
  const isImportNotFromPublicApi = segments.length > 2;
57
79
  const isSameModule = isSameModuleImport(currentFile, pathTo, fsdLayers);
58
80
 
81
+ console.warn(isImportNotFromPublicApi);
82
+ console.warn(isSameModule);
83
+
59
84
  if (isImportNotFromPublicApi && !isSameModule) {
60
- context.report({
85
+ return context.report({
61
86
  node,
62
87
  messageId: `useOthersModulesPublicApi`,
63
88
  fix(fixer) {
64
89
  const index = value.indexOf(segments[1]);
90
+ console.warn(index)
65
91
  if (index === -1) return undefined;
66
92
 
67
93
  const publicPathImport = value.substring(0, index + segments[1].length);
68
94
 
95
+ console.warn(publicPathImport)
96
+
69
97
  return fixer.replaceText(node.source, `'${publicPathImport}'`);
70
98
  }
71
99
  })
72
100
  }
73
101
  else if (isSameModule) {
74
- context.report({
102
+ return context.report({
75
103
  node,
76
104
  messageId: `doNotUseAbsolutePathInSameModuleImport`
77
105
  })
@@ -2,6 +2,7 @@
2
2
 
3
3
  const path = require("path");
4
4
  const { isSameModuleImport } = require("../helpers/isSameModuleImport");
5
+ const { getNormalizedProjectPath } = require("../helpers/getNormalizedProjectPath");
5
6
 
6
7
  const fsdLayers = {
7
8
  'shared': 'shared',
@@ -62,7 +63,7 @@ const importExportDeclaration = (context) => (node) => {
62
63
  node,
63
64
  messageId: `useRelativePathWithinSlice`,
64
65
  fix(fixer) {
65
- const projectFromPath = getProjectPath(currentFile);
66
+ const projectFromPath = getNormalizedProjectPath(currentFile);
66
67
  const currentDir = path.dirname(projectFromPath);
67
68
  const relativePath = path.relative(currentDir, pathTo);
68
69
  const finalRelativePath = relativePath.startsWith(`.`) ? relativePath : `./${relativePath}`;
@@ -73,8 +74,4 @@ const importExportDeclaration = (context) => (node) => {
73
74
  }
74
75
  }
75
76
 
76
- function getProjectPath(fullPath) {
77
- const fromNormalized = path.toNamespacedPath(fullPath);
78
77
 
79
- return fromNormalized.split(`src/`)[1];
80
- }
@@ -3,6 +3,7 @@
3
3
  const path = require("path");
4
4
  const {isPathRelative} = require("../helpers/isPathRelative");
5
5
  const micromatch = require("micromatch");
6
+ const {getNormalizedProjectPath} = require("../helpers/getNormalizedProjectPath");
6
7
 
7
8
  const fsdLayers = {
8
9
  'shared': 'shared',
@@ -61,11 +62,10 @@ const importExportDeclaration = (context) => (node) => {
61
62
  const { alias = '', ignoreImportPatterns = [] } = context.options[0] ?? {};
62
63
 
63
64
  const getCurrentFileLayer = (currentFilePath) => {
64
- const normalizedPath = path.toNamespacedPath(currentFilePath);
65
- const projectPath = normalizedPath?.split('src')[1];
65
+ const projectPath = getNormalizedProjectPath(currentFilePath)
66
66
  const segments = projectPath?.split('/');
67
67
 
68
- return segments?.[1];
68
+ return segments?.[0];
69
69
  }
70
70
 
71
71
  const getImportLayer = (value) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-fsd-paths-check",
3
- "version": "0.0.13",
3
+ "version": "0.0.15",
4
4
  "description": "Plugin to check feature sliced design paths correctness",
5
5
  "keywords": [
6
6
  "eslint",