@binbinji/eslint-plugin-import-alias
v1.2.4
Published
ESLint rule for restricting imports and dynamic imports to path aliases
Maintainers
Readme
eslint-plugin-import-alias
An ESLint rule for forcing import and dynamic import path aliases.
Install
npm install --save-dev @binbinji/eslint-plugin-import-alias
yarn add @binbinji/eslint-plugin-import-alias -D
pnpm add @binbinji/eslint-plugin-import-alias -DUsage
import { test } from '@src/test' // valid
import { test } from './test' // invalid
import { test } from '../test' // invalid
import('./test') // invalidConfigure
{
"rules": {
"@binbinji/import-alias/import-alias": [
"error",
{
"aliases": [
{ "alias": "@src", "matcher": "^src" }, // src/modules/app/test -> @src/modules/app/test
{ "alias": "@test", "matcher": "^test\/unit" }, // test/unit/modules/app -> @test/modules/app
{ "alias": "@testRoot", "matcher": "^(test)\/e2e" } // test/e2e/modules/app -> @testRoot/e2e/modules/app
]
}
]
}
}Aliases can be configured to fix the path and rewrite to an aliased path. Each alias has the alias text and a regex matcher that will match against the resolved path from the root directory of the eslint process (usually the project root). For example, if the resolved file path is in the 'src' folder (src/modules/app/test) then 'src' will be replaced with '@src'. Optionally, you can define a capture group to replace only the part within the capture group, but still match against the whole regex.
A 'rootDir' can be defined to resolve the file paths from. This defaults to process.cwd(). In a lot of cases, this is already the project root in most cases.
module.exports = {
plugins: ['@binbinji/import-alias'],
rules: {
'@binbinji/import-alias/import-alias': [
'error',
{
rootDir: __dirname,
aliases: [
{ alias: '@src', matcher: '^src' }, // src/modules/app/test -> @src/modules/app/test
],
},
],
},
}