Eslint plugin to ban specified imports in specified directories
This is useful to ban not using a barrelled import in multi-package repositories.
You'll first need to install ESLint:
$ npm i eslint --save-dev
Next, install eslint-plugin-restrict-imports
:
$ npm install eslint-plugin-restrict-imports --save-dev
Add restrict-imports
to the plugins section of your .eslintrc
configuration file. You can omit the eslint-plugin-
prefix:
{
"plugins": ["restrict-imports"]
}
Then configure the rules you want to use under the rules section.
{
"rules": {
"restrict-imports/restrict-imports": [
"error",
{
"../../clientA(.*)": {
"locations": ["(.*)/clientB(.*)"],
"message": "Do not import client A in client B"
},
"(.*)/common/(.*)": {
"locations": ["(.*)/client(.*)"],
"message": "Use barrelled import '@common'"
}
}
]
}
}
Rule configuration:
[
"error",
{
"../../clientA(.*)": { // regex for the import path to ban
"locations": ["(.*)/clientB(.*)"], // regex for the absoulte file names to enforce in
"message": "Do not import client A in client B" // optional - customised error message
}
}
]
Default Rule configuration:
[
"error",
{
"/(.*)": { // Ban absoulte imports
"locations": ["(.*)"] // in all files
}
}
]
- restrict-imports - Ban imports from a location in any directory.