eslint-plugin-fsd-paths-check 0.0.12 → 0.0.14

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,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,21 +1,22 @@
1
1
  const { isPathRelative } = require("./isPathRelative");
2
2
  const path = require("path");
3
+ const { getNormalizedProjectPath } = require("./getNormalizedProjectPath");
3
4
 
4
- function isSameModuleImport(from, to, fsdLayers) {
5
+ function isSameModuleImport(from, to, fsdLayers, noSlicesLayers) {
5
6
  if(isPathRelative(to)) {
6
7
  return false;
7
8
  }
8
9
 
10
+ const noSliceLayersInternal = noSlicesLayers ?? {};
9
11
  const toArray = to.split('/');
10
12
  const toLayer = toArray[0];
11
13
  const toSlice = toArray[1];
12
14
 
13
- if(!toLayer || !toSlice || !fsdLayers[toLayer]) {
15
+ if(!toLayer || !fsdLayers[toLayer] || (!noSliceLayersInternal[toLayer] && !toSlice)) {
14
16
  return false;
15
17
  }
16
18
 
17
- const fromNormalized = path.toNamespacedPath(from);
18
- const fromProject = fromNormalized.split(`src/`)[1];
19
+ const fromProject = getNormalizedProjectPath(from)
19
20
 
20
21
  if(!fromProject) {
21
22
  return false;
@@ -25,11 +26,11 @@ function isSameModuleImport(from, to, fsdLayers) {
25
26
  const fromLayer = fromArray[0];
26
27
  const fromSlice = fromArray[1];
27
28
 
28
- if(!fromLayer || !fromSlice || !fsdLayers[fromLayer]) {
29
+ if(!fromLayer || !fsdLayers[fromLayer] || (!noSliceLayersInternal[fromLayer] && !fromSlice)) {
29
30
  return false;
30
31
  }
31
32
 
32
- return fromSlice === toSlice && fromLayer === toLayer;
33
+ return fromLayer === toLayer && (noSliceLayersInternal[fromLayer] || fromSlice === toSlice);
33
34
  }
34
35
 
35
36
  module.exports = {
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
 
3
3
  const { isPathRelative } = require("../helpers/isPathRelative");
4
- const path = require("path");
5
4
  const {isSameModuleImport} = require("../helpers/isSameModuleImport");
6
5
 
7
6
  const fsdLayers = {
@@ -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',
@@ -9,6 +10,12 @@ const fsdLayers = {
9
10
  'features': 'features',
10
11
  'widgets': 'widgets',
11
12
  'pages': 'pages',
13
+ 'app': 'app',
14
+ }
15
+
16
+ const noSlicesLayers = {
17
+ 'shared': 'shared',
18
+ 'app': 'app',
12
19
  }
13
20
 
14
21
  module.exports = {
@@ -51,12 +58,12 @@ const importExportDeclaration = (context) => (node) => {
51
58
  const pathTo = alias ? value.replace(`${alias}`, ``) : value;
52
59
  const currentFile = context.getFilename();
53
60
 
54
- if(isSameModuleImport(currentFile, pathTo, fsdLayers)) {
61
+ if(isSameModuleImport(currentFile, pathTo, fsdLayers, noSlicesLayers)) {
55
62
  context.report({
56
63
  node,
57
64
  messageId: `useRelativePathWithinSlice`,
58
65
  fix(fixer) {
59
- const projectFromPath = getProjectPath(currentFile);
66
+ const projectFromPath = getNormalizedProjectPath(currentFile);
60
67
  const currentDir = path.dirname(projectFromPath);
61
68
  const relativePath = path.relative(currentDir, pathTo);
62
69
  const finalRelativePath = relativePath.startsWith(`.`) ? relativePath : `./${relativePath}`;
@@ -67,8 +74,4 @@ const importExportDeclaration = (context) => (node) => {
67
74
  }
68
75
  }
69
76
 
70
- function getProjectPath(fullPath) {
71
- const fromNormalized = path.toNamespacedPath(fullPath);
72
77
 
73
- return fromNormalized.split(`src/`)[1];
74
- }
@@ -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',
@@ -14,7 +15,7 @@ const fsdLayers = {
14
15
  }
15
16
 
16
17
  const allowedLayerImports = {
17
- app: ['app', 'pages', 'widgets', 'features', 'entities', 'shared'],
18
+ app: ['pages', 'widgets', 'features', 'entities', 'shared'],
18
19
  pages: ['widgets', 'features', 'entities', 'shared'],
19
20
  widgets: ['features', 'entities', 'shared'],
20
21
  features: ['entities', '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.12",
3
+ "version": "0.0.14",
4
4
  "description": "Plugin to check feature sliced design paths correctness",
5
5
  "keywords": [
6
6
  "eslint",