ESLint Plugin: Import Path Restrictions
This ESLint plugin provides rules to enforce import path restrictions in your codebase. It helps maintain a clean and organized project structure by preventing imports from restricted directories.
Installation
npm install --save-dev @mwkt/eslint-plugin-imports
Usage
Add the plugin to your ESLint configuration:
{
"plugins": ["imports"],
"rules": {
"imports/restrict-import-paths": "error"
}
}
Rule: restrict-import-paths
This rule enforces restrictions on import paths in your codebase. It can be configured to:
- Apply only to specific source folders
- Restrict imports from certain directories
Options
The rule accepts an array of configuration objects, where each object can have the following properties:
sourceFolders (string[]): Array of folder paths where the rule should be applied. Default: ["/components/"]
restrictedFolders (string[]): Array of folder paths that should not be imported from. Default: ["/modules/"]
Example Configuration
{
"rules": {
"imports/restrict-import-paths": ["error", {
"sourceFolders": ["client/lib/components"],
"restrictedFolders": ["@lib/modules"]
}]
}
}
You can also provide multiple configuration objects to apply different restrictions to different source folders:
{
"rules": {
"imports/restrict-import-paths": ["error",
{
"sourceFolders": ["client/lib/components"],
"restrictedFolders": ["@lib/modules"]
},
{
"sourceFolders": ["server/api"],
"restrictedFolders": ["@lib/client"]
}
]
}
}
Examples
The rule will report errors for:
import { something } from '../modules/feature';
const something = require('../modules/feature');
import { something } from '../components/feature';
Features
- Supports both ES6 imports and CommonJS require statements
- Configurable source and restricted folders
- Only applies to specified source files
- Easy to integrate with existing ESLint configurations