eslint-plugin-restrict-replace-import 1.0.0 → 1.2.0

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.
package/README.md CHANGED
@@ -33,7 +33,7 @@ Then configure the rules you want to use under the rules section.
33
33
  ```json
34
34
  {
35
35
  "rules": {
36
- "restrict-import/rule-name": [
36
+ "restrict-replace-import/restrict-import": [
37
37
  "error",
38
38
  ["restricted-package1", "restricted-package2"]
39
39
  ]
@@ -46,16 +46,35 @@ You can also specify an alternative package to import instead:
46
46
  ```json
47
47
  {
48
48
  "rules": {
49
- "restrict-import/rule-name": [
49
+ "restrict-replace-import/restrict-import": [
50
50
  "error",
51
51
  [
52
52
  {
53
- "name": "restricted-package1",
54
- "alternative": "replacement-package1"
53
+ "target": "restricted-package1",
54
+ "replacement": "replacement-package1"
55
55
  },
56
56
  {
57
- "name": "restricted-package2",
58
- "alternative": "replacement-package2"
57
+ "target": "restricted-package2",
58
+ "replacement": "replacement-package2"
59
+ }
60
+ ]
61
+ ]
62
+ }
63
+ }
64
+ ```
65
+
66
+ You can use RegExp for package name:
67
+
68
+ ```json
69
+ {
70
+ "rules": {
71
+ "restrict-replace-import/restrict-import": [
72
+ "error",
73
+ [
74
+ "with(?:-regex)?-support",
75
+ {
76
+ "target": "restricted-.*",
77
+ "replacement": "replacement-package"
59
78
  }
60
79
  ]
61
80
  ]
@@ -10,6 +10,8 @@ With additional configuration, this rule can also suggest an alternative package
10
10
 
11
11
  If the alternative package is specified, auto-fixing will replace the import statement with the alternative package.
12
12
 
13
+ You can use RegExp to match multiple packages.
14
+
13
15
  ## Rule Details
14
16
 
15
17
  Example configuration:
@@ -21,25 +23,29 @@ Example configuration:
21
23
  "error",
22
24
  [
23
25
  {
24
- "name": "test-package",
25
- "alternative": "replacement-package"
26
+ "target": "test-package",
27
+ "replacement": "replacement-package"
26
28
  },
27
29
  "another-package"
30
+ "with(?:-regex)?-support"
28
31
  ]
29
32
  ]
30
33
  }
31
34
  }
32
35
  ```
33
36
 
34
- Examples of **incorrect** code for this rule:
37
+ Examples of **incorrect** code for this rule with options above:
35
38
 
36
39
  ```js
37
40
  import testPackage from "test-package";
38
41
 
39
42
  import anotherPackage from "another-package";
43
+
44
+ import withRegexSupport from "with-regex-support";
45
+ import withSupport from "with-support";
40
46
  ```
41
47
 
42
- Examples of **correct** code for this rule:
48
+ Examples of **correct** code for this rule with options above:
43
49
 
44
50
  ```js
45
51
  import testPackage from "replacement-package";
@@ -23,51 +23,43 @@ module.exports = {
23
23
  "`{{ name }}` is restricted from being used. Replace it with `{{ replacement }}`.",
24
24
  },
25
25
 
26
- schema: [
27
- {
28
- type: "object",
29
- properties: {
30
- restrictedPackages: {
31
- type: "array",
32
- items: {
33
- anyOf: [
34
- {
35
- type: "string",
36
- },
37
- {
38
- type: "object",
39
- properties: {
40
- target: {
41
- type: "string",
42
- required: true,
43
- },
44
- replacement: {
45
- type: "string",
46
- required: false,
47
- },
48
- },
49
- },
50
- ],
26
+ schema: {
27
+ type: "array",
28
+ items: {
29
+ anyOf: [
30
+ {
31
+ type: "string",
32
+ },
33
+ {
34
+ type: "object",
35
+ properties: {
36
+ target: {
37
+ type: "string",
38
+ },
39
+ replacement: {
40
+ type: "string",
41
+ },
51
42
  },
52
43
  },
53
- },
44
+ ],
54
45
  },
55
- ],
46
+ },
56
47
  },
57
48
 
58
49
  create(context) {
59
- const option = context.options[0];
60
-
61
50
  const restrictedPackages = new Map();
62
51
 
63
- option.restrictedPackages.forEach((packageName) => {
52
+ context.options.forEach((packageName) => {
64
53
  if (typeof packageName === "string") {
65
- restrictedPackages.set(packageName, null);
54
+ restrictedPackages.set(
55
+ new RegExp(`^${packageName}$`),
56
+ null
57
+ );
66
58
  return;
67
59
  }
68
60
 
69
61
  restrictedPackages.set(
70
- packageName.target,
62
+ new RegExp(`^${packageName.target}$`),
71
63
  packageName.replacement
72
64
  ? packageName.replacement
73
65
  : null
@@ -75,15 +67,22 @@ module.exports = {
75
67
  });
76
68
 
77
69
  const checkRestricted = (importSource) => {
78
- const isRestricted =
79
- restrictedPackages.has(importSource);
80
-
81
- return isRestricted;
70
+ const restrictedRegExp = Array.from(
71
+ restrictedPackages.keys()
72
+ ).find((packageName) => {
73
+ return packageName.test(importSource);
74
+ });
75
+
76
+ return {
77
+ isRestricted: !!restrictedRegExp,
78
+ restrictedRegExp,
79
+ };
82
80
  };
83
81
 
84
- const getReplacement = (importSource) => {
85
- const replacement =
86
- restrictedPackages.get(importSource);
82
+ const getReplacement = (restrictedRegExp) => {
83
+ const replacement = restrictedPackages.get(
84
+ restrictedRegExp
85
+ );
87
86
 
88
87
  return replacement;
89
88
  };
@@ -93,7 +92,8 @@ module.exports = {
93
92
  const importSource = node.source.value;
94
93
  const importSourceType = node.source.type;
95
94
 
96
- const isRestricted = checkRestricted(importSource);
95
+ const { isRestricted, restrictedRegExp } =
96
+ checkRestricted(importSource);
97
97
 
98
98
  if (
99
99
  !isRestricted ||
@@ -102,7 +102,9 @@ module.exports = {
102
102
  return;
103
103
  }
104
104
 
105
- const replacement = getReplacement(importSource);
105
+ const replacement = getReplacement(
106
+ restrictedRegExp
107
+ );
106
108
  const quote = node.source.raw.includes("'")
107
109
  ? "'"
108
110
  : '"';
@@ -113,7 +115,7 @@ module.exports = {
113
115
  ? "ImportRestrictionWithReplacement"
114
116
  : "ImportRestriction",
115
117
  data: {
116
- name: importSource,
118
+ name: restrictedRegExp.toString().slice(2, -2),
117
119
  replacement,
118
120
  },
119
121
  fix: (fixer) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-restrict-replace-import",
3
- "version": "1.0.0",
3
+ "version": "1.2.0",
4
4
  "description": "ESLint Plugin for Restricting and Replacing Import",
5
5
  "keywords": [
6
6
  "eslint",
@@ -1,79 +0,0 @@
1
- /**
2
- * @fileoverview Prevent the Import of a Specific Package
3
- * @author shiwoo.park
4
- */
5
- "use strict";
6
-
7
- //------------------------------------------------------------------------------
8
- // Requirements
9
- //------------------------------------------------------------------------------
10
-
11
- const rule = require("../../../lib/rules/restrict-import"),
12
- RuleTester = require("eslint").RuleTester;
13
-
14
- //------------------------------------------------------------------------------
15
- // Tests
16
- //------------------------------------------------------------------------------
17
-
18
- const ruleTester = new RuleTester({
19
- parserOptions: {
20
- ecmaVersion: 2015,
21
- sourceType: "module",
22
- },
23
- });
24
-
25
- const OPTIONS = [
26
- {
27
- restrictedPackages: [
28
- "lodash",
29
- {
30
- target: "react",
31
- replacement: "preact",
32
- },
33
- ],
34
- },
35
- ];
36
-
37
- ruleTester.run("restrict-import", rule, {
38
- valid: [
39
- {
40
- code: "import _ from 'underscore'",
41
- options: OPTIONS,
42
- },
43
- {
44
- code: "import _ from 'lodash-es'",
45
- options: OPTIONS,
46
- },
47
- {
48
- code: "import { useState } from 'preact'",
49
- options: OPTIONS,
50
- },
51
- ],
52
-
53
- invalid: [
54
- {
55
- code: "import _ from 'lodash'",
56
- errors: [
57
- {
58
- message:
59
- "`lodash` is restricted from being used.",
60
- type: "ImportDeclaration",
61
- },
62
- ],
63
- options: OPTIONS,
64
- output: null,
65
- },
66
- {
67
- code: "import { useState } from 'react'",
68
- errors: [
69
- {
70
- message:
71
- "`react` is restricted from being used. Replace it with `preact`.",
72
- type: "ImportDeclaration",
73
- },
74
- ],
75
- options: OPTIONS,
76
- output: "import { useState } from 'preact'",
77
- },
78
- ],
79
- });