Skip to content

Commit ade0b59

Browse files
committed
✨ add no-restricted-import
1 parent 578110e commit ade0b59

File tree

4 files changed

+466
-0
lines changed

4 files changed

+466
-0
lines changed

docs/rules/no-restricted-import.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# node/no-restricted-import
2+
> disallow specified modules when loaded by `require`
3+
4+
## 📖 Rule Details
5+
6+
This rule allows you to specify modules that you don’t want to use in your application.
7+
8+
### Options
9+
10+
The rule takes an array as options: the names of restricted modules.
11+
12+
```json
13+
{
14+
"no-restricted-import": ["error", [
15+
"foo-module",
16+
"bar-module"
17+
]]
18+
}
19+
```
20+
21+
You may also specify a custom message for each module you want to restrict as follows:
22+
23+
```json
24+
{
25+
"no-restricted-import": ["error", [
26+
{
27+
"name": "foo-module",
28+
"message": "Please use foo-module2 instead."
29+
},
30+
{
31+
"name": "bar-module",
32+
"message": "Please use bar-module2 instead."
33+
}
34+
]]
35+
}
36+
```
37+
38+
And you can use glob patterns in the `name` property.
39+
40+
```json
41+
{
42+
"no-restricted-import": ["error", [
43+
{
44+
"name": "lodash/*",
45+
"message": "Please use xyz-module instead."
46+
},
47+
{
48+
"name": ["foo-module/private/*", "bar-module/*", "!baz-module/good"],
49+
"message": "Please use xyz-module instead."
50+
}
51+
]]
52+
}
53+
```
54+
55+
And you can use absolute paths in the `name` property.
56+
57+
```js
58+
module.exports = {
59+
overrides: [
60+
{
61+
files: "client/**",
62+
rules: {
63+
"no-restricted-import": ["error", [
64+
{
65+
name: path.resolve(__dirname, "server/**"),
66+
message: "Don't use server code from client code."
67+
}
68+
]]
69+
}
70+
},
71+
{
72+
files: "server/**",
73+
rules: {
74+
"no-restricted-import": ["error", [
75+
{
76+
name: path.resolve(__dirname, "client/**"),
77+
message: "Don't use client code from server code."
78+
}
79+
]]
80+
}
81+
}
82+
]
83+
}
84+
```
85+
86+
### Examples
87+
88+
Examples of **incorrect** code for this rule with sample `"fs", "cluster", "lodash"` restricted modules:
89+
90+
```js
91+
/*eslint no-restricted-import: ["error", ["fs", "cluster", "lodash/*"]]*/
92+
93+
import fs from 'fs';
94+
import cluster from 'cluster';
95+
import pick from 'lodash/pick';
96+
```
97+
98+
Examples of **correct** code for this rule with sample `"fs", "cluster", "lodash"` restricted modules:
99+
100+
```js
101+
/*eslint no-restricted-import: ["error", ["fs", "cluster", "lodash/*"]]*/
102+
103+
import crypto from 'crypto';
104+
import _ from 'lodash';
105+
```
106+
107+
```js
108+
/*eslint no-restricted-import: ["error", ["fs", "cluster", { "name": ["lodash/*", "!lodash/pick"] }]]*/
109+
110+
import pick from 'lodash/pick';
111+
```
112+
113+
## 🔎 Implementation
114+
115+
- [Rule source](../../lib/rules/no-restricted-import.js)
116+
- [Test source](../../tests/lib/rules/no-restricted-import.js)

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ module.exports = {
2727
"no-path-concat": require("./rules/no-path-concat"),
2828
"no-process-env": require("./rules/no-process-env"),
2929
"no-process-exit": require("./rules/no-process-exit"),
30+
"no-restricted-import": require("./rules/no-restricted-import"),
3031
"no-restricted-require": require("./rules/no-restricted-require"),
3132
"no-sync": require("./rules/no-sync"),
3233
"no-unpublished-bin": require("./rules/no-unpublished-bin"),

lib/rules/no-restricted-import.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* @author Toru Nagashima
3+
* See LICENSE file in root directory for full license.
4+
*/
5+
"use strict"
6+
7+
const check = require("../util/check-restricted")
8+
const visit = require("../util/visit-import")
9+
10+
module.exports = {
11+
meta: {
12+
type: "suggestion",
13+
docs: {
14+
description: "disallow specified modules when loaded by `require`",
15+
category: "Stylistic Issues",
16+
recommended: false,
17+
url:
18+
"https://github.com/mysticatea/eslint-plugin-node/blob/v11.0.0/docs/rules/no-restricted-import.md",
19+
},
20+
fixable: null,
21+
schema: [
22+
{
23+
type: "array",
24+
items: {
25+
anyOf: [
26+
{ type: "string" },
27+
{
28+
type: "object",
29+
properties: {
30+
name: {
31+
anyOf: [
32+
{ type: "string" },
33+
{
34+
type: "array",
35+
items: { type: "string" },
36+
additionalItems: false,
37+
},
38+
],
39+
},
40+
message: { type: "string" },
41+
},
42+
additionalProperties: false,
43+
required: ["name"],
44+
},
45+
],
46+
},
47+
additionalItems: false,
48+
},
49+
],
50+
messages: {
51+
restricted:
52+
// eslint-disable-next-line @mysticatea/eslint-plugin/report-message-format
53+
"'{{name}}' module is restricted from being used.{{customMessage}}",
54+
},
55+
},
56+
57+
create(context) {
58+
const opts = { includeCore: true }
59+
return visit(context, opts, targets => check(context, targets))
60+
},
61+
}

0 commit comments

Comments
 (0)