-
-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
578110e
commit ade0b59
Showing
4 changed files
with
466 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
# node/no-restricted-import | ||
> disallow specified modules when loaded by `require` | ||
## 📖 Rule Details | ||
|
||
This rule allows you to specify modules that you don’t want to use in your application. | ||
|
||
### Options | ||
|
||
The rule takes an array as options: the names of restricted modules. | ||
|
||
```json | ||
{ | ||
"no-restricted-import": ["error", [ | ||
"foo-module", | ||
"bar-module" | ||
]] | ||
} | ||
``` | ||
|
||
You may also specify a custom message for each module you want to restrict as follows: | ||
|
||
```json | ||
{ | ||
"no-restricted-import": ["error", [ | ||
{ | ||
"name": "foo-module", | ||
"message": "Please use foo-module2 instead." | ||
}, | ||
{ | ||
"name": "bar-module", | ||
"message": "Please use bar-module2 instead." | ||
} | ||
]] | ||
} | ||
``` | ||
|
||
And you can use glob patterns in the `name` property. | ||
|
||
```json | ||
{ | ||
"no-restricted-import": ["error", [ | ||
{ | ||
"name": "lodash/*", | ||
"message": "Please use xyz-module instead." | ||
}, | ||
{ | ||
"name": ["foo-module/private/*", "bar-module/*", "!baz-module/good"], | ||
"message": "Please use xyz-module instead." | ||
} | ||
]] | ||
} | ||
``` | ||
|
||
And you can use absolute paths in the `name` property. | ||
|
||
```js | ||
module.exports = { | ||
overrides: [ | ||
{ | ||
files: "client/**", | ||
rules: { | ||
"no-restricted-import": ["error", [ | ||
{ | ||
name: path.resolve(__dirname, "server/**"), | ||
message: "Don't use server code from client code." | ||
} | ||
]] | ||
} | ||
}, | ||
{ | ||
files: "server/**", | ||
rules: { | ||
"no-restricted-import": ["error", [ | ||
{ | ||
name: path.resolve(__dirname, "client/**"), | ||
message: "Don't use client code from server code." | ||
} | ||
]] | ||
} | ||
} | ||
] | ||
} | ||
``` | ||
|
||
### Examples | ||
|
||
Examples of **incorrect** code for this rule with sample `"fs", "cluster", "lodash"` restricted modules: | ||
|
||
```js | ||
/*eslint no-restricted-import: ["error", ["fs", "cluster", "lodash/*"]]*/ | ||
|
||
import fs from 'fs'; | ||
import cluster from 'cluster'; | ||
import pick from 'lodash/pick'; | ||
``` | ||
|
||
Examples of **correct** code for this rule with sample `"fs", "cluster", "lodash"` restricted modules: | ||
|
||
```js | ||
/*eslint no-restricted-import: ["error", ["fs", "cluster", "lodash/*"]]*/ | ||
|
||
import crypto from 'crypto'; | ||
import _ from 'lodash'; | ||
``` | ||
|
||
```js | ||
/*eslint no-restricted-import: ["error", ["fs", "cluster", { "name": ["lodash/*", "!lodash/pick"] }]]*/ | ||
|
||
import pick from 'lodash/pick'; | ||
``` | ||
|
||
## 🔎 Implementation | ||
|
||
- [Rule source](../../lib/rules/no-restricted-import.js) | ||
- [Test source](../../tests/lib/rules/no-restricted-import.js) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* @author Toru Nagashima | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
"use strict" | ||
|
||
const check = require("../util/check-restricted") | ||
const visit = require("../util/visit-import") | ||
|
||
module.exports = { | ||
meta: { | ||
type: "suggestion", | ||
docs: { | ||
description: "disallow specified modules when loaded by `require`", | ||
category: "Stylistic Issues", | ||
recommended: false, | ||
url: | ||
"https://github.com/mysticatea/eslint-plugin-node/blob/v11.0.0/docs/rules/no-restricted-import.md", | ||
}, | ||
fixable: null, | ||
schema: [ | ||
{ | ||
type: "array", | ||
items: { | ||
anyOf: [ | ||
{ type: "string" }, | ||
{ | ||
type: "object", | ||
properties: { | ||
name: { | ||
anyOf: [ | ||
{ type: "string" }, | ||
{ | ||
type: "array", | ||
items: { type: "string" }, | ||
additionalItems: false, | ||
}, | ||
], | ||
}, | ||
message: { type: "string" }, | ||
}, | ||
additionalProperties: false, | ||
required: ["name"], | ||
}, | ||
], | ||
}, | ||
additionalItems: false, | ||
}, | ||
], | ||
messages: { | ||
restricted: | ||
// eslint-disable-next-line @mysticatea/eslint-plugin/report-message-format | ||
"'{{name}}' module is restricted from being used.{{customMessage}}", | ||
}, | ||
}, | ||
|
||
create(context) { | ||
const opts = { includeCore: true } | ||
return visit(context, opts, targets => check(context, targets)) | ||
}, | ||
} |
Oops, something went wrong.