eslint-plugin-restrict-replace-import 1.1.0 → 1.2.1

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
  ]
@@ -23,8 +23,8 @@ Example configuration:
23
23
  "error",
24
24
  [
25
25
  {
26
- "name": "test-package",
27
- "alternative": "replacement-package"
26
+ "target": "test-package",
27
+ "replacement": "replacement-package"
28
28
  },
29
29
  "another-package"
30
30
  "with(?:-regex)?-support"
@@ -23,43 +23,41 @@ 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
- additionalProperties: false,
30
- properties: {
31
- restrictedPackages: {
32
- type: "array",
33
- items: {
34
- anyOf: [
35
- {
26
+ schema: {
27
+ type: "array",
28
+ maxLength: 1,
29
+ minLength: 1,
30
+ items: {
31
+ type: "array",
32
+ items: {
33
+ oneOf: [
34
+ {
35
+ type: "string",
36
+ },
37
+ {
38
+ type: "object",
39
+ properties: {
40
+ target: {
36
41
  type: "string",
37
42
  },
38
- {
39
- type: "object",
40
- properties: {
41
- target: {
42
- type: "string",
43
- },
44
- replacement: {
45
- type: "string",
46
- },
47
- },
43
+ replacement: {
44
+ type: "string",
48
45
  },
49
- ],
46
+ },
47
+ required: ["target"],
48
+ additionalProperties: false,
50
49
  },
51
- },
50
+ ],
52
51
  },
53
52
  },
54
- ],
53
+ },
55
54
  },
56
55
 
57
56
  create(context) {
58
- const option = context.options[0];
59
-
60
57
  const restrictedPackages = new Map();
58
+ const restrictedPackagesOption = context.options[0];
61
59
 
62
- option.restrictedPackages.forEach((packageName) => {
60
+ restrictedPackagesOption.forEach((packageName) => {
63
61
  if (typeof packageName === "string") {
64
62
  restrictedPackages.set(
65
63
  new RegExp(`^${packageName}$`),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-restrict-replace-import",
3
- "version": "1.1.0",
3
+ "version": "1.2.1",
4
4
  "description": "ESLint Plugin for Restricting and Replacing Import",
5
5
  "keywords": [
6
6
  "eslint",
@@ -1,139 +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
- target: "any-other-with(?:regex)",
35
- replacement: "any-other-replacement",
36
- },
37
- "other-with(?:regex)",
38
- {
39
- target: "with(?:-regex)?-support",
40
- replacement: "with-support-replacement",
41
- },
42
- ],
43
- },
44
- ];
45
-
46
- ruleTester.run("restrict-import", rule, {
47
- valid: [
48
- {
49
- code: "import _ from 'underscore'",
50
- options: OPTIONS,
51
- },
52
- {
53
- code: "import _ from 'lodash-es'",
54
- options: OPTIONS,
55
- },
56
- {
57
- code: "import { useState } from 'preact'",
58
- options: OPTIONS,
59
- },
60
- ],
61
-
62
- invalid: [
63
- {
64
- code: "import _ from 'lodash'",
65
- errors: [
66
- {
67
- message:
68
- "`lodash` is restricted from being used.",
69
- type: "ImportDeclaration",
70
- },
71
- ],
72
- options: OPTIONS,
73
- output: null,
74
- },
75
- {
76
- code: "import { useState } from 'react'",
77
- errors: [
78
- {
79
- message:
80
- "`react` is restricted from being used. Replace it with `preact`.",
81
- type: "ImportDeclaration",
82
- },
83
- ],
84
- options: OPTIONS,
85
- output: "import { useState } from 'preact'",
86
- },
87
- {
88
- code: "import { useState } from 'any-other-withregex'",
89
- errors: [
90
- {
91
- message:
92
- "`any-other-with(?:regex)` is restricted from being used. Replace it with `any-other-replacement`.",
93
- type: "ImportDeclaration",
94
- },
95
- ],
96
- options: OPTIONS,
97
- output:
98
- "import { useState } from 'any-other-replacement'",
99
- },
100
- {
101
- code: "import { useState } from 'other-withregex'",
102
- errors: [
103
- {
104
- message:
105
- "`other-with(?:regex)` is restricted from being used.",
106
- type: "ImportDeclaration",
107
- },
108
- ],
109
- options: OPTIONS,
110
- output: null,
111
- },
112
- {
113
- code: "import { useState } from 'with-regex-support'",
114
- errors: [
115
- {
116
- message:
117
- "`with(?:-regex)?-support` is restricted from being used. Replace it with `with-support-replacement`.",
118
- type: "ImportDeclaration",
119
- },
120
- ],
121
- options: OPTIONS,
122
- output:
123
- "import { useState } from 'with-support-replacement'",
124
- },
125
- {
126
- code: "import { useState } from 'with-support'",
127
- errors: [
128
- {
129
- message:
130
- "`with(?:-regex)?-support` is restricted from being used. Replace it with `with-support-replacement`.",
131
- type: "ImportDeclaration",
132
- },
133
- ],
134
- options: OPTIONS,
135
- output:
136
- "import { useState } from 'with-support-replacement'",
137
- },
138
- ],
139
- });