eslint-plugin-restrict-replace-import 1.5.0 → 2.0.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 +35 -4
- package/dist/index.d.mts +10 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +354 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +372 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +36 -22
- package/docs/rules/restrict-import.md +0 -166
- package/eslint.config.mjs +0 -43
- package/lib/index.js +0 -18
- package/lib/rules/restrict-import.js +0 -548
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
# Prevent the Import of a Specific Package (`restrict-replace-import/restrict-import`)
|
|
2
|
-
|
|
3
|
-
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
|
|
4
|
-
|
|
5
|
-
<!-- end auto-generated rule header -->
|
|
6
|
-
|
|
7
|
-
This rule aims to prevent the import of a specific package and optionally replace it with an alternative package.
|
|
8
|
-
|
|
9
|
-
## Features
|
|
10
|
-
|
|
11
|
-
- Restrict imports from specific packages
|
|
12
|
-
- Replace restricted imports with alternative packages
|
|
13
|
-
- Support for RegExp patterns in package names
|
|
14
|
-
- Restrict specific named imports while allowing others
|
|
15
|
-
- Partial string replacements using RegExp
|
|
16
|
-
- Automatic merging with existing imports from replacement modules
|
|
17
|
-
|
|
18
|
-
## Options
|
|
19
|
-
|
|
20
|
-
<!-- begin auto-generated rule options list -->
|
|
21
|
-
|
|
22
|
-
| Name | Description | Type | Required |
|
|
23
|
-
| :------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------- | :------- |
|
|
24
|
-
| `namedImports` | The named imports to be restricted. If not provided, all named imports will be restricted. | String[] | |
|
|
25
|
-
| `replacement` | The replacement for the import. If a string is provided, it will be used as the replacement for all imports. If an object is provided, the keys will be used as the pattern and the values will be used as the replacement. | | |
|
|
26
|
-
| `target` | The target of the import to be restricted | String | Yes |
|
|
27
|
-
|
|
28
|
-
<!-- end auto-generated rule options list -->
|
|
29
|
-
|
|
30
|
-
## Usage Examples
|
|
31
|
-
|
|
32
|
-
### Basic Usage - Restricting Packages
|
|
33
|
-
|
|
34
|
-
```json
|
|
35
|
-
{
|
|
36
|
-
"rules": {
|
|
37
|
-
"restrict-replace-import/restrict-import": ["error", ["lodash"]]
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
This will prevent imports from `lodash`:
|
|
43
|
-
```js
|
|
44
|
-
// ❌ Error: `lodash` is restricted from being used
|
|
45
|
-
import _ from 'lodash'
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
### Replacing with Alternative Package
|
|
49
|
-
|
|
50
|
-
```json
|
|
51
|
-
{
|
|
52
|
-
"rules": {
|
|
53
|
-
"restrict-replace-import/restrict-import": [
|
|
54
|
-
"error",
|
|
55
|
-
[{
|
|
56
|
-
"target": "react",
|
|
57
|
-
"replacement": "preact"
|
|
58
|
-
}]
|
|
59
|
-
]
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
```
|
|
63
|
-
|
|
64
|
-
```js
|
|
65
|
-
// Before
|
|
66
|
-
import { useState } from 'react'
|
|
67
|
-
|
|
68
|
-
// After
|
|
69
|
-
import { useState } from 'preact'
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
### Using RegExp for Package Names
|
|
73
|
-
|
|
74
|
-
```json
|
|
75
|
-
{
|
|
76
|
-
"rules": {
|
|
77
|
-
"restrict-replace-import/restrict-import": [
|
|
78
|
-
"error",
|
|
79
|
-
[{
|
|
80
|
-
"target": "with(?:-regex)?-support",
|
|
81
|
-
"replacement": "with-support-replacement"
|
|
82
|
-
}]
|
|
83
|
-
]
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
This will match and replace both:
|
|
89
|
-
```js
|
|
90
|
-
import { something } from 'with-regex-support' // will be replaced
|
|
91
|
-
import { something } from 'with-support' // will be replaced
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
### Partial String Replacements
|
|
95
|
-
|
|
96
|
-
```json
|
|
97
|
-
{
|
|
98
|
-
"rules": {
|
|
99
|
-
"restrict-replace-import/restrict-import": [
|
|
100
|
-
"error",
|
|
101
|
-
[{
|
|
102
|
-
"target": "with-partial-replacements",
|
|
103
|
-
"replacement": {
|
|
104
|
-
"par(regExp)?tial-": "successfully-",
|
|
105
|
-
"repla(regExp)?cements": "replaced",
|
|
106
|
-
"with-": ""
|
|
107
|
-
}
|
|
108
|
-
}]
|
|
109
|
-
]
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
```
|
|
113
|
-
|
|
114
|
-
```js
|
|
115
|
-
// Before
|
|
116
|
-
import { useState } from 'with-partial-replacements'
|
|
117
|
-
|
|
118
|
-
// After
|
|
119
|
-
import { useState } from 'successfully-replaced'
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
### Restricting Specific Named Imports
|
|
123
|
-
|
|
124
|
-
```json
|
|
125
|
-
{
|
|
126
|
-
"rules": {
|
|
127
|
-
"restrict-replace-import/restrict-import": [
|
|
128
|
-
"error",
|
|
129
|
-
[{
|
|
130
|
-
"target": "restricted-module",
|
|
131
|
-
"namedImports": ["restrictedImport", "alsoRestricted"],
|
|
132
|
-
"replacement": "replacement-module" // Object is not supported yet
|
|
133
|
-
}]
|
|
134
|
-
]
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
This configuration handles various scenarios:
|
|
140
|
-
|
|
141
|
-
```js
|
|
142
|
-
// ✅ Allowed - import not in restricted list
|
|
143
|
-
import { allowedImport } from 'restricted-module'
|
|
144
|
-
|
|
145
|
-
// ❌ Will be replaced
|
|
146
|
-
import { restrictedImport } from 'restricted-module'
|
|
147
|
-
// ↓ Becomes
|
|
148
|
-
import { restrictedImport } from 'replacement-module'
|
|
149
|
-
|
|
150
|
-
// Mixed imports are split
|
|
151
|
-
import { restrictedImport, allowed } from 'restricted-module'
|
|
152
|
-
// ↓ Becomes
|
|
153
|
-
import { restrictedImport } from 'replacement-module'
|
|
154
|
-
import { allowed } from 'restricted-module'
|
|
155
|
-
|
|
156
|
-
// Handles aliases
|
|
157
|
-
import { restrictedImport as aliasName } from 'restricted-module'
|
|
158
|
-
// ↓ Becomes
|
|
159
|
-
import { restrictedImport as aliasName } from 'replacement-module'
|
|
160
|
-
|
|
161
|
-
// Merges with existing imports
|
|
162
|
-
import { existingImport } from 'replacement-module';
|
|
163
|
-
import { restrictedImport } from 'restricted-module';
|
|
164
|
-
// ↓ Becomes
|
|
165
|
-
import { existingImport, restrictedImport } from 'replacement-module';
|
|
166
|
-
```
|
package/eslint.config.mjs
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
import eslint from '@eslint/js'
|
|
4
|
-
import eslintPluginEslintPlugin from 'eslint-plugin-eslint-plugin'
|
|
5
|
-
import eslintPluginNode from 'eslint-plugin-node'
|
|
6
|
-
import { fixupPluginRules } from '@eslint/compat'
|
|
7
|
-
import globals from 'globals'
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* @type {import('eslint').Linter.Config[]}
|
|
11
|
-
*/
|
|
12
|
-
export default [
|
|
13
|
-
eslint.configs.recommended,
|
|
14
|
-
{
|
|
15
|
-
languageOptions: {
|
|
16
|
-
ecmaVersion: 2023,
|
|
17
|
-
sourceType: 'commonjs',
|
|
18
|
-
globals: {
|
|
19
|
-
...globals.node,
|
|
20
|
-
},
|
|
21
|
-
},
|
|
22
|
-
files: ['**/*.js'],
|
|
23
|
-
rules: {
|
|
24
|
-
...eslintPluginEslintPlugin.configs.recommended.rules,
|
|
25
|
-
...eslintPluginNode.configs.recommended.rules,
|
|
26
|
-
},
|
|
27
|
-
plugins: {
|
|
28
|
-
'eslint-plugin': eslintPluginEslintPlugin,
|
|
29
|
-
node: fixupPluginRules(eslintPluginNode),
|
|
30
|
-
},
|
|
31
|
-
linterOptions: {
|
|
32
|
-
reportUnusedDisableDirectives: true,
|
|
33
|
-
},
|
|
34
|
-
},
|
|
35
|
-
{
|
|
36
|
-
files: ['tests/**/*.js'],
|
|
37
|
-
languageOptions: {
|
|
38
|
-
globals: {
|
|
39
|
-
mocha: 'readonly',
|
|
40
|
-
},
|
|
41
|
-
},
|
|
42
|
-
},
|
|
43
|
-
]
|
package/lib/index.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview ESLint Plugin for Restricting Import
|
|
3
|
-
* @author shiwoo.park
|
|
4
|
-
*/
|
|
5
|
-
'use strict'
|
|
6
|
-
|
|
7
|
-
//------------------------------------------------------------------------------
|
|
8
|
-
// Requirements
|
|
9
|
-
//------------------------------------------------------------------------------
|
|
10
|
-
|
|
11
|
-
const requireIndex = require('requireindex')
|
|
12
|
-
|
|
13
|
-
//------------------------------------------------------------------------------
|
|
14
|
-
// Plugin Definition
|
|
15
|
-
//------------------------------------------------------------------------------
|
|
16
|
-
|
|
17
|
-
// import all rules in lib/rules
|
|
18
|
-
module.exports.rules = requireIndex(__dirname + '/rules')
|