@macklenc/eslint-plugin-absolute-imports
v1.0.1
Published
Automatically replace relative imports with absolute imports
Maintainers
Readme
@macklenc/eslint-plugin-absolute-imports
An ESLint plugin that automatically replaces relative imports with absolute imports based on your project's structure.
Installation
You'll first need to install ESLint:
npm i eslint --save-devNext, install @macklenc/eslint-plugin-absolute-imports:
npm install @macklenc/eslint-plugin-absolute-imports --save-devUsage
Add absolute-imports to the plugins section of your ESLint configuration file.
For ESLint v9+ (flat config)
import absoluteImports from "@macklenc/eslint-plugin-absolute-imports";
export default [
{
plugins: {
"absolute-imports": absoluteImports,
},
rules: {
"absolute-imports/absolute-imports": [
"error",
{
// Optional: specify the base path for your project
// basePath: "./src",
// Optional: specify the path to your tsconfig.json for path mapping
// tsconfigPath: "./tsconfig.json"
},
],
},
},
];For ESLint v8 (legacy config)
module.exports = {
plugins: ["absolute-imports"],
rules: {
"absolute-imports/absolute-imports": [
"error",
{
// Optional: specify the base path for your project
// basePath: "./src",
// Optional: specify the path to your tsconfig.json for path mapping
// tsconfigPath: "./tsconfig.json"
},
],
},
};Rules
absolute-imports
This rule identifies relative imports in your code and suggests replacing them with absolute imports.
Options
The rule accepts an options object with the following properties:
basePath(optional): The base path of your project. Defaults to the current working directory.tsconfigPath(optional): Path to your tsconfig.json file for path mapping. Defaults to<projectRoot>/tsconfig.json.
Examples
Given a project structure:
/project
/src
/components
Component.ts
/pages
Page.tsWith the rule configured as:
"absolute-imports/absolute-imports": ["error", { basePath: "./src" }]The following code in /src/pages/Page.ts:
import { Component } from "../components/Component";Will be converted to:
import { Component } from "/components/Component";When the rule is triggered
The rule will be triggered when:
- You use relative imports (starting with
./or../) - The import can be converted to an absolute path based on your project structure
The rule will not be triggered for:
- Already absolute imports
- Imports using aliases (e.g.,
@components/Component) - External package imports (e.g.,
react,lodash)
Development
Commit Guidelines
This project follows Conventional Commits for commit messages. This helps with automatic versioning and changelog generation.
To create a properly formatted commit, run:
npm run commit
# or
pnpm run commitThis will guide you through creating a conventional commit message.
Versioning and Releases
This project uses standard-version for versioning and changelog generation based on commit messages.
To create a new release:
# Automatically determine version bump based on commits
npm run release
# or
pnpm run release
# Specific version bumps
npm run release:patch # 0.0.1 -> 0.0.2
npm run release:minor # 0.0.1 -> 0.1.0
npm run release:major # 0.0.1 -> 1.0.0
# Preview what the release would do
npm run release:dry-runThe release process will:
- Bump the version in package.json
- Update CHANGELOG.md with all notable changes
- Create a git tag for the new version
- Create a commit with these changes
License
MIT
