eslint-plugin-restrict-replace-import 1.1.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 +25 -6
- package/docs/rules/restrict-import.md +2 -2
- package/lib/rules/restrict-import.js +19 -29
- package/package.json +1 -1
- package/tests/lib/rules/restrict-import.js +0 -139
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/
|
|
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/
|
|
49
|
+
"restrict-replace-import/restrict-import": [
|
|
50
50
|
"error",
|
|
51
51
|
[
|
|
52
52
|
{
|
|
53
|
-
"
|
|
54
|
-
"
|
|
53
|
+
"target": "restricted-package1",
|
|
54
|
+
"replacement": "replacement-package1"
|
|
55
55
|
},
|
|
56
56
|
{
|
|
57
|
-
"
|
|
58
|
-
"
|
|
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,43 +23,33 @@ module.exports = {
|
|
|
23
23
|
"`{{ name }}` is restricted from being used. Replace it with `{{ replacement }}`.",
|
|
24
24
|
},
|
|
25
25
|
|
|
26
|
-
schema:
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
type: "string",
|
|
43
|
-
},
|
|
44
|
-
replacement: {
|
|
45
|
-
type: "string",
|
|
46
|
-
},
|
|
47
|
-
},
|
|
48
|
-
},
|
|
49
|
-
],
|
|
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
|
+
},
|
|
50
42
|
},
|
|
51
43
|
},
|
|
52
|
-
|
|
44
|
+
],
|
|
53
45
|
},
|
|
54
|
-
|
|
46
|
+
},
|
|
55
47
|
},
|
|
56
48
|
|
|
57
49
|
create(context) {
|
|
58
|
-
const option = context.options[0];
|
|
59
|
-
|
|
60
50
|
const restrictedPackages = new Map();
|
|
61
51
|
|
|
62
|
-
|
|
52
|
+
context.options.forEach((packageName) => {
|
|
63
53
|
if (typeof packageName === "string") {
|
|
64
54
|
restrictedPackages.set(
|
|
65
55
|
new RegExp(`^${packageName}$`),
|
package/package.json
CHANGED
|
@@ -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
|
-
});
|