0.0.3 • Published 4 years ago

eslint-plugin-restrict-imports v0.0.3

Weekly downloads
1,587
License
ISC
Repository
-
Last release
4 years ago

eslint-plugin-restrict-imports

CircleCI

Eslint plugin to ban specified imports in specified directories

This is useful to ban not using a barrelled import in multi-package repositories.

Installation

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

Usage

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
        }
    }
]

Supported Rules

  • restrict-imports - Ban imports from a location in any directory.