eslint-plugin-restrict-replace-import 1.2.1 → 1.3.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 +36 -2
- package/docs/rules/restrict-import.md +2 -2
- package/lib/rules/restrict-import.js +30 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -20,11 +20,11 @@ npm install eslint-plugin-restrict-replace-import --save-dev
|
|
|
20
20
|
|
|
21
21
|
## Usage
|
|
22
22
|
|
|
23
|
-
Add `restrict-import` to the plugins section of your `.eslintrc` configuration file. You can omit the `eslint-plugin-` prefix:
|
|
23
|
+
Add `restrict-replace-import` to the plugins section of your `.eslintrc` configuration file. You can omit the `eslint-plugin-` prefix:
|
|
24
24
|
|
|
25
25
|
```json
|
|
26
26
|
{
|
|
27
|
-
"plugins": ["restrict-import"]
|
|
27
|
+
"plugins": ["restrict-replace-import"]
|
|
28
28
|
}
|
|
29
29
|
```
|
|
30
30
|
|
|
@@ -82,6 +82,40 @@ You can use RegExp for package name:
|
|
|
82
82
|
}
|
|
83
83
|
```
|
|
84
84
|
|
|
85
|
+
Is it possible as well to perform multiple partial replacements by setting and Object in the `replacement` property:
|
|
86
|
+
|
|
87
|
+
```json
|
|
88
|
+
{
|
|
89
|
+
"rules": {
|
|
90
|
+
"restrict-replace-import/restrict-import": [
|
|
91
|
+
"error",
|
|
92
|
+
[
|
|
93
|
+
{
|
|
94
|
+
"target": "with-partial-.*",
|
|
95
|
+
"replacement": {
|
|
96
|
+
"par(regExp)?tial-": "successfully-",
|
|
97
|
+
"repla(regExp)?cements": "replaced",
|
|
98
|
+
"with-": "",
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
]
|
|
102
|
+
]
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
Given that rule configuration it will perform the following replacement:
|
|
107
|
+
|
|
108
|
+
Input:
|
|
109
|
+
```js
|
|
110
|
+
import { useState } from 'with-partial-replacements'
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Output:
|
|
114
|
+
```js
|
|
115
|
+
import { useState } from 'successfully-replaced'
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
|
|
85
119
|
## Rules
|
|
86
120
|
|
|
87
121
|
<!-- begin auto-generated rules list -->
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Prevent the Import of a Specific Package (`restrict-import/restrict-import`)
|
|
1
|
+
# Prevent the Import of a Specific Package (`restrict-replace-import/restrict-import`)
|
|
2
2
|
|
|
3
3
|
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
|
|
4
4
|
|
|
@@ -19,7 +19,7 @@ Example configuration:
|
|
|
19
19
|
```json
|
|
20
20
|
{
|
|
21
21
|
"rules": {
|
|
22
|
-
"restrict-import/restrict-import": [
|
|
22
|
+
"restrict-replace-import/restrict-import": [
|
|
23
23
|
"error",
|
|
24
24
|
[
|
|
25
25
|
{
|
|
@@ -41,7 +41,15 @@ module.exports = {
|
|
|
41
41
|
type: "string",
|
|
42
42
|
},
|
|
43
43
|
replacement: {
|
|
44
|
-
|
|
44
|
+
oneOf: [
|
|
45
|
+
{ type: "string" },
|
|
46
|
+
{
|
|
47
|
+
type: "object",
|
|
48
|
+
patternProperties: {
|
|
49
|
+
".*": { type: "string" }
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
]
|
|
45
53
|
},
|
|
46
54
|
},
|
|
47
55
|
required: ["target"],
|
|
@@ -119,7 +127,7 @@ module.exports = {
|
|
|
119
127
|
|
|
120
128
|
context.report({
|
|
121
129
|
node,
|
|
122
|
-
messageId: replacement
|
|
130
|
+
messageId: typeof replacement === "string"
|
|
123
131
|
? "ImportRestrictionWithReplacement"
|
|
124
132
|
: "ImportRestriction",
|
|
125
133
|
data: {
|
|
@@ -131,10 +139,26 @@ module.exports = {
|
|
|
131
139
|
return;
|
|
132
140
|
}
|
|
133
141
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
142
|
+
if (typeof replacement === "string") {
|
|
143
|
+
return fixer.replaceText(
|
|
144
|
+
node.source,
|
|
145
|
+
`${quote}${replacement}${quote}`
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (typeof replacement === "object") {
|
|
150
|
+
let partiallyReplaced = node.source.value;
|
|
151
|
+
for (const [key, value] of Object.entries(replacement)) {
|
|
152
|
+
const regex = new RegExp(key, 'g');
|
|
153
|
+
partiallyReplaced = partiallyReplaced.replace(regex, value);
|
|
154
|
+
}
|
|
155
|
+
return fixer.replaceText(
|
|
156
|
+
node.source,
|
|
157
|
+
`${quote}${partiallyReplaced}${quote}`
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return null;
|
|
138
162
|
},
|
|
139
163
|
});
|
|
140
164
|
},
|