eslint-plugin-restrict-replace-import 1.2.0 → 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 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
  {
@@ -25,31 +25,47 @@ module.exports = {
25
25
 
26
26
  schema: {
27
27
  type: "array",
28
+ maxLength: 1,
29
+ minLength: 1,
28
30
  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",
31
+ type: "array",
32
+ items: {
33
+ oneOf: [
34
+ {
35
+ type: "string",
36
+ },
37
+ {
38
+ type: "object",
39
+ properties: {
40
+ target: {
41
+ type: "string",
42
+ },
43
+ replacement: {
44
+ oneOf: [
45
+ { type: "string" },
46
+ {
47
+ type: "object",
48
+ patternProperties: {
49
+ ".*": { type: "string" }
50
+ }
51
+ }
52
+ ]
53
+ },
41
54
  },
55
+ required: ["target"],
56
+ additionalProperties: false,
42
57
  },
43
- },
44
- ],
58
+ ],
59
+ },
45
60
  },
46
61
  },
47
62
  },
48
63
 
49
64
  create(context) {
50
65
  const restrictedPackages = new Map();
66
+ const restrictedPackagesOption = context.options[0];
51
67
 
52
- context.options.forEach((packageName) => {
68
+ restrictedPackagesOption.forEach((packageName) => {
53
69
  if (typeof packageName === "string") {
54
70
  restrictedPackages.set(
55
71
  new RegExp(`^${packageName}$`),
@@ -111,7 +127,7 @@ module.exports = {
111
127
 
112
128
  context.report({
113
129
  node,
114
- messageId: replacement
130
+ messageId: typeof replacement === "string"
115
131
  ? "ImportRestrictionWithReplacement"
116
132
  : "ImportRestriction",
117
133
  data: {
@@ -123,10 +139,26 @@ module.exports = {
123
139
  return;
124
140
  }
125
141
 
126
- return fixer.replaceText(
127
- node.source,
128
- `${quote}${replacement}${quote}`
129
- );
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;
130
162
  },
131
163
  });
132
164
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-restrict-replace-import",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "ESLint Plugin for Restricting and Replacing Import",
5
5
  "keywords": [
6
6
  "eslint",