eslint-plugin-restrict-replace-import 1.0.0 → 1.1.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.
|
@@ -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:
|
|
@@ -25,21 +27,25 @@ Example configuration:
|
|
|
25
27
|
"alternative": "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";
|
|
@@ -26,6 +26,7 @@ module.exports = {
|
|
|
26
26
|
schema: [
|
|
27
27
|
{
|
|
28
28
|
type: "object",
|
|
29
|
+
additionalProperties: false,
|
|
29
30
|
properties: {
|
|
30
31
|
restrictedPackages: {
|
|
31
32
|
type: "array",
|
|
@@ -39,11 +40,9 @@ module.exports = {
|
|
|
39
40
|
properties: {
|
|
40
41
|
target: {
|
|
41
42
|
type: "string",
|
|
42
|
-
required: true,
|
|
43
43
|
},
|
|
44
44
|
replacement: {
|
|
45
45
|
type: "string",
|
|
46
|
-
required: false,
|
|
47
46
|
},
|
|
48
47
|
},
|
|
49
48
|
},
|
|
@@ -62,12 +61,15 @@ module.exports = {
|
|
|
62
61
|
|
|
63
62
|
option.restrictedPackages.forEach((packageName) => {
|
|
64
63
|
if (typeof packageName === "string") {
|
|
65
|
-
restrictedPackages.set(
|
|
64
|
+
restrictedPackages.set(
|
|
65
|
+
new RegExp(`^${packageName}$`),
|
|
66
|
+
null
|
|
67
|
+
);
|
|
66
68
|
return;
|
|
67
69
|
}
|
|
68
70
|
|
|
69
71
|
restrictedPackages.set(
|
|
70
|
-
packageName.target,
|
|
72
|
+
new RegExp(`^${packageName.target}$`),
|
|
71
73
|
packageName.replacement
|
|
72
74
|
? packageName.replacement
|
|
73
75
|
: null
|
|
@@ -75,15 +77,22 @@ module.exports = {
|
|
|
75
77
|
});
|
|
76
78
|
|
|
77
79
|
const checkRestricted = (importSource) => {
|
|
78
|
-
const
|
|
79
|
-
restrictedPackages.
|
|
80
|
-
|
|
81
|
-
|
|
80
|
+
const restrictedRegExp = Array.from(
|
|
81
|
+
restrictedPackages.keys()
|
|
82
|
+
).find((packageName) => {
|
|
83
|
+
return packageName.test(importSource);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
isRestricted: !!restrictedRegExp,
|
|
88
|
+
restrictedRegExp,
|
|
89
|
+
};
|
|
82
90
|
};
|
|
83
91
|
|
|
84
|
-
const getReplacement = (
|
|
85
|
-
const replacement =
|
|
86
|
-
|
|
92
|
+
const getReplacement = (restrictedRegExp) => {
|
|
93
|
+
const replacement = restrictedPackages.get(
|
|
94
|
+
restrictedRegExp
|
|
95
|
+
);
|
|
87
96
|
|
|
88
97
|
return replacement;
|
|
89
98
|
};
|
|
@@ -93,7 +102,8 @@ module.exports = {
|
|
|
93
102
|
const importSource = node.source.value;
|
|
94
103
|
const importSourceType = node.source.type;
|
|
95
104
|
|
|
96
|
-
const isRestricted =
|
|
105
|
+
const { isRestricted, restrictedRegExp } =
|
|
106
|
+
checkRestricted(importSource);
|
|
97
107
|
|
|
98
108
|
if (
|
|
99
109
|
!isRestricted ||
|
|
@@ -102,7 +112,9 @@ module.exports = {
|
|
|
102
112
|
return;
|
|
103
113
|
}
|
|
104
114
|
|
|
105
|
-
const replacement = getReplacement(
|
|
115
|
+
const replacement = getReplacement(
|
|
116
|
+
restrictedRegExp
|
|
117
|
+
);
|
|
106
118
|
const quote = node.source.raw.includes("'")
|
|
107
119
|
? "'"
|
|
108
120
|
: '"';
|
|
@@ -113,7 +125,7 @@ module.exports = {
|
|
|
113
125
|
? "ImportRestrictionWithReplacement"
|
|
114
126
|
: "ImportRestriction",
|
|
115
127
|
data: {
|
|
116
|
-
name:
|
|
128
|
+
name: restrictedRegExp.toString().slice(2, -2),
|
|
117
129
|
replacement,
|
|
118
130
|
},
|
|
119
131
|
fix: (fixer) => {
|
package/package.json
CHANGED
|
@@ -30,6 +30,15 @@ const OPTIONS = [
|
|
|
30
30
|
target: "react",
|
|
31
31
|
replacement: "preact",
|
|
32
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
|
+
},
|
|
33
42
|
],
|
|
34
43
|
},
|
|
35
44
|
];
|
|
@@ -75,5 +84,56 @@ ruleTester.run("restrict-import", rule, {
|
|
|
75
84
|
options: OPTIONS,
|
|
76
85
|
output: "import { useState } from 'preact'",
|
|
77
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
|
+
},
|
|
78
138
|
],
|
|
79
139
|
});
|