@sj_lk/eslint-plugin-sj-plugin 0.1.0 → 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: {
@@ -12,22 +14,34 @@ const rule = {
12
14
  url: null, // URL to the documentation page for this rule
13
15
  },
14
16
  fixable: null, // Or `code` or `whitespace`
15
- schema: [], // Add a schema if the rule has options
17
+ schema: [
18
+ {
19
+ type: 'object',
20
+ properties: {
21
+ alias: {
22
+ type: 'string',
23
+ description: 'alias for the rule',
24
+ }
25
+ }
26
+ }
27
+ ], defaultOptions: [{alias: ''}],
16
28
  messages: {
17
- someProblem: 'В рамках одного слайса все пути должны быть относительными',
18
- }, // Add messageId and message
29
+ pathProblem: 'В рамках одного слайса все пути должны быть относительными',
30
+ },
19
31
  },
20
32
 
21
33
  create(context) {
34
+ const alias = context.options[0]?.alias || ''
22
35
  return {
23
36
  ImportDeclaration(node) {
24
- const importTo = node.source.value
37
+ const value = node.source.value
38
+ const importTo = alias ? value.replace(`${alias}/`, '') : value
25
39
  const fromFilename = context.filename
26
40
 
27
41
  if(shouldBeRelative(fromFilename, importTo)) {
28
42
  context.report({
29
43
  node,
30
- messageId: 'someProblem',
44
+ messageId: 'pathProblem',
31
45
  })
32
46
  }
33
47
  }
@@ -45,10 +59,6 @@ const layers = {
45
59
  'widgets': 'widgets'
46
60
  }
47
61
 
48
- function isPathRelative(path) {
49
- return path === '.' || path.startsWith('./') || path.startsWith('../')
50
- }
51
-
52
62
  function shouldBeRelative(from, to) {
53
63
 
54
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.0",
4
- "description": "path-checker is working now",
3
+ "version": "0.1.2",
4
+ "description": "add public api imports validator",
5
5
  "keywords": [
6
6
  "eslint",
7
7
  "eslintplugin",