npm package: @macklenc/eslint-plugin-absolute-imports
Description: Automatically replace relative imports with absolute imports
Author: Christopher Macklen
Latest version: 1.0.1

# @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](https://eslint.org/):

```sh
npm i eslint --save-dev
```

Next, install `@macklenc/eslint-plugin-absolute-imports`:

```sh
npm install @macklenc/eslint-plugin-absolute-imports --save-dev
```

## Usage

Add `absolute-imports` to the plugins section of your [ESLint configuration file](https://eslint.org/docs/latest/use/configure/configuration-files).

### For ESLint v9+ (flat config)

```js
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)

```js
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.ts
```

With the rule configured as:

```js
"absolute-imports/absolute-imports": ["error", { basePath: "./src" }]
```

The following code in `/src/pages/Page.ts`:

```js
import { Component } from "../components/Component";
```

Will be converted to:

```js
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](https://www.conventionalcommits.org/) for commit messages. This helps with automatic versioning and changelog generation.

To create a properly formatted commit, run:

```sh
npm run commit
# or
pnpm run commit
```

This will guide you through creating a conventional commit message.

### Versioning and Releases

This project uses [standard-version](https://github.com/conventional-changelog/standard-version) for versioning and changelog generation based on commit messages.

To create a new release:

```sh
# 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-run
```

The release process will:

1. Bump the version in package.json
2. Update CHANGELOG.md with all notable changes
3. Create a git tag for the new version
4. Create a commit with these changes

## License

MIT