eslint-plugin-laygrd-plugin 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.
@@ -0,0 +1,35 @@
1
+ # absolute imports allowed only from public api file (index.ts) (`public-api-exports`)
2
+
3
+ Please describe the origin of the rule here.
4
+
5
+ ## Rule Details
6
+
7
+ This rule aims to...
8
+
9
+ Examples of **incorrect** code for this rule:
10
+
11
+ ```js
12
+
13
+ // fill me in
14
+
15
+ ```
16
+
17
+ Examples of **correct** code for this rule:
18
+
19
+ ```js
20
+
21
+ // fill me in
22
+
23
+ ```
24
+
25
+ ### Options
26
+
27
+ If there are any options, describe them here. Otherwise, delete this section.
28
+
29
+ ## When Not To Use It
30
+
31
+ Give a short description of when it would be appropriate to turn off this rule.
32
+
33
+ ## Further Reading
34
+
35
+ If there are other links that describe the issue this rule addresses, please include them here in a bulleted list.
@@ -0,0 +1,7 @@
1
+ const isPathRelative = (p) => {
2
+ return p === '.' || p.startsWith('./') || p.startsWith('../');
3
+ };
4
+
5
+ module.exports = {
6
+ isPathRelative,
7
+ };
@@ -5,6 +5,8 @@
5
5
  "use strict";
6
6
 
7
7
  const path = require('path');
8
+ const { isPathRelative } = require('../helpers');
9
+
8
10
  //------------------------------------------------------------------------------
9
11
  // Rule Definition
10
12
  //------------------------------------------------------------------------------
@@ -15,14 +17,14 @@ const path = require('path');
15
17
  module.exports = {
16
18
  meta: {
17
19
  // eslint-disable-next-line eslint-plugin/require-meta-type
18
- type: null, // `problem`, `suggestion`, or `layout`
20
+ type: null,
19
21
  docs: {
20
22
  description: "check relative paths inside module ",
21
23
  category: "fsd",
22
24
  recommended: false,
23
- url: null, // URL to the documentation page for this rule
25
+ url: null,
24
26
  },
25
- fixable: null, // Or `code` or `whitespace`
27
+ fixable: null,
26
28
  schema: [
27
29
  {
28
30
  type: 'object',
@@ -32,7 +34,7 @@ module.exports = {
32
34
  }
33
35
  }
34
36
  }
35
- ], // Add a schema if the rule has options
37
+ ],
36
38
  },
37
39
 
38
40
  create(context) {
@@ -64,10 +66,6 @@ const layers = {
64
66
  'shared':'shared',
65
67
  };
66
68
 
67
- const isPathRelative = (p) => {
68
- return p === '.' || p.startsWith('./') || p.startsWith('../');
69
- };
70
-
71
69
  const shouldPathBeRelative = (curFile, impSrc) => {
72
70
  if (isPathRelative(impSrc)) {
73
71
  return false;
@@ -0,0 +1,78 @@
1
+ /**
2
+ * @fileoverview absolute imports allowed only from public api file (index.ts)
3
+ * @author laygrd
4
+ */
5
+ "use strict";
6
+ const { isPathRelative } = require('../helpers');
7
+
8
+
9
+ //------------------------------------------------------------------------------
10
+ // Rule Definition
11
+ //------------------------------------------------------------------------------
12
+
13
+ /** @type {import('eslint').Rule.RuleModule} */
14
+ module.exports = {
15
+ meta: {
16
+ // eslint-disable-next-line eslint-plugin/require-meta-type
17
+ type: null, // `problem`, `suggestion`, or `layout`
18
+ docs: {
19
+ category: "fsd",
20
+ description: "absolute imports allowed only from public api file (index.ts)",
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
+ }
32
+ }
33
+ }
34
+ ], // Add a schema if the rule has options
35
+ messages: {}, // Add messageId and message
36
+ },
37
+
38
+ create(context) {
39
+ const alias = context.options[0]?.alias || '';
40
+
41
+ return {
42
+ ImportDeclaration(node) {
43
+ const value = node.source.value;
44
+
45
+ // import from '@/features/Button/Button' --> import from 'features/Button/Button'
46
+ const importSrc = alias ? value.replace(`${alias}/`, '') : value;
47
+
48
+ // skipping relative paths like '../ui/Button/Button'
49
+ if (isPathRelative(importSrc)) {
50
+ return null;
51
+ }
52
+
53
+ const segments = importSrc.split('/');
54
+
55
+ // valid: "import {...} from 'entities/article'" -> ['entities', 'article']
56
+ // invalid: "import {...} from 'entities/article/ui/articleCard/articleCard'" -> ['entities', 'article', 'ui', 'articleCard', 'articleCard']
57
+ const isImportNotFromPublicApi = segments.length > 2;
58
+
59
+ // check only declared layers from project root
60
+ const layer = segments[0];
61
+ if (!layersToCheck[layer]) {
62
+ return null;
63
+ }
64
+
65
+ if (isImportNotFromPublicApi) {
66
+ context.report({node: node, message: 'Absolute imports should be only from module`s Public API (index.ts). '});
67
+ }
68
+ }
69
+ };
70
+ },
71
+ };
72
+
73
+ const layersToCheck = {
74
+ 'pages':'pages',
75
+ 'widgets':'widgets',
76
+ 'features':'features',
77
+ 'entities':'entities',
78
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "eslint-plugin-laygrd-plugin",
3
- "version": "0.0.3",
4
- "description": "some usefull fsd violation checkers",
3
+ "version": "0.0.4",
4
+ "description": "some usefull fsd rule-checkers",
5
5
  "keywords": [
6
6
  "eslint",
7
7
  "eslintplugin",
@@ -22,12 +22,12 @@ const ruleTester = new RuleTester({
22
22
  ruleTester.run("fsd-relative-path", rule, {
23
23
  valid: [
24
24
  {
25
- filename: 'C:\\Users\\Desctop\\Projects\\blog_app\\src\\entities\\Rating',
25
+ filename: 'C:\\Users\\Desktop\\Projects\\blog_app\\src\\entities\\Rating',
26
26
  code: "import { RatingCard, RatingCardTheme } from './ui/RatingCard/RatingCard'",
27
27
  errors: [],
28
28
  },
29
29
  {
30
- filename: 'C:\\Users\\Desctop\\Projects\\blog_app\\src\\features\\ArticleRating\\ui\\ArticleRating\\ArticleRating',
30
+ filename: 'C:\\Users\\Desktop\\Projects\\blog_app\\src\\features\\ArticleRating\\ui\\ArticleRating\\ArticleRating',
31
31
  code: "import { useArticleRating } from '../../api/articleRatingApi'",
32
32
  errors: [],
33
33
  },
@@ -35,13 +35,13 @@ ruleTester.run("fsd-relative-path", rule, {
35
35
 
36
36
  invalid: [
37
37
  {
38
- filename: 'C:\\Users\\Desctop\\Projects\\blog_app\\src\\entities\\Rating',
38
+ filename: 'C:\\Users\\Desktop\\Projects\\blog_app\\src\\entities\\Rating',
39
39
  code: "import { RatingCard, RatingCardTheme } from '@/entities/Rating/ui/RatingCard/RatingCard'",
40
40
  errors: [{ message: "Imports within the same module must be relative, according to FSD. " }],
41
41
  options: [{ alias: '@' }]
42
42
  },
43
43
  {
44
- filename: 'C:\\Users\\Desctop\\Projects\\blog_app\\src\\features\\ArticleRating\\ui\\ArticleRating\\ArticleRating',
44
+ filename: 'C:\\Users\\Desktop\\Projects\\blog_app\\src\\features\\ArticleRating\\ui\\ArticleRating\\ArticleRating',
45
45
  code: "import { useArticleRating } from '@/features/ArticleRating/api/articleRatingApi'",
46
46
  errors: [{ message: "Imports within the same module must be relative, according to FSD. " }],
47
47
  options: [{ alias: '@' }]
@@ -0,0 +1,53 @@
1
+ /**
2
+ * @fileoverview absolute imports allowed only from public api file (index.ts)
3
+ * @author laygrd
4
+ */
5
+ "use strict";
6
+
7
+ //------------------------------------------------------------------------------
8
+ // Requirements
9
+ //------------------------------------------------------------------------------
10
+
11
+ const rule = require("../../../lib/rules/public-api-exports"),
12
+ RuleTester = require("eslint").RuleTester;
13
+
14
+
15
+ //------------------------------------------------------------------------------
16
+ // Tests
17
+ //------------------------------------------------------------------------------
18
+
19
+ const ruleTester = new RuleTester({
20
+ parserOptions: { ecmaVersion: 6, sourceType: 'module' }
21
+ });
22
+ ruleTester.run("public-api-exports", rule, {
23
+ valid: [
24
+ {
25
+ filename: 'C:\\Users\\Desktop\\Projects\\blog_app\\src\\entities\\Rating\\ui\\RatingCard\\RatingCard',
26
+ code: "import { Avatar } from '@/widgets/Avatar'",
27
+ errors: [],
28
+ options: [{ alias: '@' }]
29
+ },
30
+ {
31
+ filename: 'C:\\Users\\Desktop\\Projects\\blog_app\\src\\widgets\\ThemeSwitcher\\ui\\ThemeSwitcher\\ThemeSwitcher',
32
+ code: "import { Button } from '@/shared/Button/ui/Button'",
33
+ errors: [],
34
+ options: [{ alias: '@' }]
35
+ },
36
+
37
+ ],
38
+
39
+ invalid: [
40
+ {
41
+ filename: 'C:\\Users\\Desktop\\Projects\\blog_app\\src\\entities\\Rating\\ui\\RatingCard\\RatingCard',
42
+ code: "import { Avatar } from 'widgets/Avatar/ui/Avatar/Avatar'",
43
+ errors: [{ message: 'Absolute imports should be only from module`s Public API (index.ts). '}],
44
+ options: [{ alias: '@' }]
45
+ },
46
+ {
47
+ filename: 'C:\\Users\\Desktop\\Projects\\blog_app\\src\\widgets\\ThemeSwitcher\\ui\\ThemeSwitcher\\ThemeSwitcher',
48
+ code: "import { Theme } from '@/entities/Theme/lib/themes'",
49
+ errors: [{ message: 'Absolute imports should be only from module`s Public API (index.ts). '}],
50
+ options: [{ alias: '@' }]
51
+ },
52
+ ],
53
+ });