@sj_lk/eslint-plugin-sj-plugin 0.1.1 → 0.1.2

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,7 @@
1
+ function isPathRelative(path) {
2
+ return path === '.' || path.startsWith('./') || path.startsWith('../')
3
+ }
4
+
5
+ module.exports = {
6
+ isPathRelative
7
+ }
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
2
 
3
3
  /** @type {import('eslint').Rule.RuleModule} */
4
+
4
5
  const path = require('path');
6
+ const { isPathRelative } = require('../helpers');
5
7
 
6
8
  const rule = {
7
9
  meta: {
@@ -22,10 +24,10 @@ const rule = {
22
24
  }
23
25
  }
24
26
  }
25
- ], defaultOptions: [{alias: ''}], // Add a schema if the rule has options
27
+ ], defaultOptions: [{alias: ''}],
26
28
  messages: {
27
- someProblem: 'В рамках одного слайса все пути должны быть относительными',
28
- }, // Add messageId and message
29
+ pathProblem: 'В рамках одного слайса все пути должны быть относительными',
30
+ },
29
31
  },
30
32
 
31
33
  create(context) {
@@ -39,7 +41,7 @@ const rule = {
39
41
  if(shouldBeRelative(fromFilename, importTo)) {
40
42
  context.report({
41
43
  node,
42
- messageId: 'someProblem',
44
+ messageId: 'pathProblem',
43
45
  })
44
46
  }
45
47
  }
@@ -57,10 +59,6 @@ const layers = {
57
59
  'widgets': 'widgets'
58
60
  }
59
61
 
60
- function isPathRelative(path) {
61
- return path === '.' || path.startsWith('./') || path.startsWith('../')
62
- }
63
-
64
62
  function shouldBeRelative(from, to) {
65
63
 
66
64
  if (isPathRelative(to)) {
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+
3
+ /** @type {import('eslint').Rule.RuleModule} */
4
+
5
+ const { isPathRelative } = require('../helpers')
6
+
7
+ module.exports = {
8
+ meta: {
9
+ type: 'suggestion', // `problem`, `suggestion`, or `layout`
10
+ docs: {
11
+ description: "validator for absolute imports from a public API",
12
+ recommended: false,
13
+ url: null, // URL to the documentation page for this rule
14
+ },
15
+ fixable: null, // Or `code` or `whitespace`
16
+ schema: [
17
+ {
18
+ type: 'object',
19
+ properties: {
20
+ alias: {
21
+ type: 'string',
22
+ description: 'alias for the rule',
23
+ }
24
+ },
25
+ }
26
+ ], defaultOptions: [{alias: ''}],
27
+ messages: {
28
+ importProblem: 'Абсолютный импорт разрешен только из Public API (index.ts)'
29
+ },
30
+ },
31
+
32
+ create(context) {
33
+ const alias = context.options[0]?.alias || ''
34
+
35
+ const allowedLayers = {
36
+ 'entities': 'entities',
37
+ 'features': 'features',
38
+ 'pages': 'pages',
39
+ 'widgets': 'widgets'
40
+ }
41
+
42
+ return {
43
+ ImportDeclaration(node) {
44
+ const value = node.source.value
45
+ const importTo = alias ? value.replace(`${alias}/`, '') : value
46
+
47
+ if(isPathRelative(importTo)) {
48
+ return;
49
+ }
50
+ const segments = importTo.split('/')
51
+ const layer = segments[0]
52
+
53
+ if(!allowedLayers[layer]) {
54
+ return;
55
+ }
56
+
57
+ const isImportNotFromPublicApi = segments.length > 2
58
+
59
+ if(isImportNotFromPublicApi) {
60
+ context.report({
61
+ node,
62
+ messageId: 'importProblem',
63
+ })
64
+ }
65
+ }
66
+ };
67
+ },
68
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sj_lk/eslint-plugin-sj-plugin",
3
- "version": "0.1.1",
4
- "description": "add working alias for the rule",
3
+ "version": "0.1.2",
4
+ "description": "add public api imports validator",
5
5
  "keywords": [
6
6
  "eslint",
7
7
  "eslintplugin",