<!-- title -->

# @kitschpatrol/eslint-config

<!-- /title -->

<!-- badges -->

[![NPM Package @kitschpatrol/eslint-config](https://img.shields.io/npm/v/@kitschpatrol/eslint-config.svg)](https://npmjs.com/package/@kitschpatrol/eslint-config)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/license/mit/)

<!-- /badges -->

<!-- description -->

**ESLint configuration for @kitschpatrol/shared-config.**

<!-- /description -->

## Overview

It's a shared [ESLint](https://eslint.org) config, plus a command-line tool `ksc-eslint` to perform ESLint-related project initialization, linting, and fixing.

<!-- recommendation -->

> [!IMPORTANT]
>
> **You can use this package on its own, but it's recommended to use [`@kitschpatrol/shared-config`](https://www.npmjs.com/package/@kitschpatrol/shared-config) instead for a single-dependency and single-package approach to linting and fixing your project.**
>
> This package is included as a dependency in [`@kitschpatrol/shared-config`](https://www.npmjs.com/package/@kitschpatrol/shared-config), which also automatically invokes the command line functionality in this package via its `ksc` command

<!-- /recommendation -->

## Setup

To use just this ESLint config in isolation:

1. Install the basic repository configuration files in your project root. This is required for correct PNPM behavior:

   ```sh
   pnpm --package=@kitschpatrol/repo-config dlx ksc-repo init
   ```

2. Install and initialize the required TypeScript `tsconfig.json` configuration in your project root:

   ```sh
   pnpm add -D @kitschpatrol/typescript-config
   pnpm --package=@kitschpatrol/typescript-config dlx ksc-typescript init
   ```

3. Add the package:

   ```sh
   pnpm add -D @kitschpatrol/eslint-config
   ```

4. Add the starter `eslint.config.ts` config files to your project root, and add any overrides you'd like:

   ```sh
   pnpm exec eslint-ksc init
   ```

## Usage

The ESLint binary should be picked up automatically by VS Code plugins.

You can call it directly, or use the script bundled with the config.

Integrate with your `package.json` scripts as you see fit, for example:

```json
{
  "scripts": {
    "lint": "eslint-config lint",
    "fix": "eslint-config fix"
  }
}
```

### CLI

<!-- cli-help -->

#### Command: `ksc-eslint`

Kitschpatrol's ESLint shared configuration tools.

This section lists top-level commands for `ksc-eslint`.

Usage:

```txt
ksc-eslint <command>
```

| Command        | Argument    | Description                                                                                                                                                               |
| -------------- | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `init`         |             | Initialize by copying starter config files to your project root.                                                                                                          |
| `lint`         | `[files..]` | Lint your project with ESLint. Matches files below the current working directory by default.                                                                              |
| `fix`          | `[files..]` | Fix your project with ESLint. Matches files below the current working directory by default.                                                                               |
| `print-config` | `[file]`    | Print the effective ESLint configuration. Package-scoped by default, file-scoped if a file argument is provided. Use `@eslint/config-inspector` for a more detailed view. |

| Option              | Description         | Type      |
| ------------------- | ------------------- | --------- |
| `--help`<br>`-h`    | Show help           | `boolean` |
| `--version`<br>`-v` | Show version number | `boolean` |

_See the sections below for more information on each subcommand._

#### Subcommand: `ksc-eslint init`

Initialize by copying starter config files to your project root.

Usage:

```txt
ksc-eslint init
```

| Option              | Description         | Type      |
| ------------------- | ------------------- | --------- |
| `--help`<br>`-h`    | Show help           | `boolean` |
| `--version`<br>`-v` | Show version number | `boolean` |

#### Subcommand: `ksc-eslint lint`

Lint your project with ESLint. Matches files below the current working directory by default.

Usage:

```txt
ksc-eslint lint [files..]
```

| Positional Argument | Description                    | Type    | Default |
| ------------------- | ------------------------------ | ------- | ------- |
| `files`             | Files or glob pattern to lint. | `array` | `"."`   |

| Option              | Description         | Type      |
| ------------------- | ------------------- | --------- |
| `--help`<br>`-h`    | Show help           | `boolean` |
| `--version`<br>`-v` | Show version number | `boolean` |

#### Subcommand: `ksc-eslint fix`

Fix your project with ESLint. Matches files below the current working directory by default.

Usage:

```txt
ksc-eslint fix [files..]
```

| Positional Argument | Description                   | Type    | Default |
| ------------------- | ----------------------------- | ------- | ------- |
| `files`             | Files or glob pattern to fix. | `array` | `"."`   |

| Option              | Description         | Type      |
| ------------------- | ------------------- | --------- |
| `--help`<br>`-h`    | Show help           | `boolean` |
| `--version`<br>`-v` | Show version number | `boolean` |

#### Subcommand: `ksc-eslint print-config`

Print the effective ESLint configuration. Package-scoped by default, file-scoped if a file argument is provided. Use `@eslint/config-inspector` for a more detailed view.

Usage:

```txt
ksc-eslint print-config [file]
```

| Positional Argument | Description                                      | Type     |
| ------------------- | ------------------------------------------------ | -------- |
| `file`              | File or glob pattern to print configuration for. | `string` |

| Option              | Description         | Type      |
| ------------------- | ------------------- | --------- |
| `--help`<br>`-h`    | Show help           | `boolean` |
| `--version`<br>`-v` | Show version number | `boolean` |

<!-- /cli-help -->

### API

The package also exports `fix`, `fixFile` functions for linting and fixing code programmatically, pre-configured with the shared ESLint configuration.

```typescript
import { clearCache, fix, fixFile } from '@kitschpatrol/eslint-config'

// Fix a string (defaults to TypeScript parser)
const fixed = await fix('let x = 1\nconsole.log(x)\n')

// Fix with a bare file extension for parser inference
const withExtension = await fix(code, 'tsx')

// Fix with ESLint options
const withOptions = await fix(code, { react: true })

// Both file type and options
const withBoth = await fix(code, 'tsx', { react: true })

// Fix a file in place with options
await fixFile('./src/index.ts', { type: 'lib' })

// Clear cached ESLint module and instances
clearCache()
```

Config is resolved using the shared `eslintConfig()` factory with `isInEditor: false`. Per-call options accept the same `OptionsConfig` shape as the factory.

The ESLint module and instances are cached internally for performance across multiple calls. Use `clearCache()` to force re-initialization.

## Notes

Regrettably the `eslint-config init --location package` option is not supported due to ESLint 9's deprecation of support for putting configuration in `package.json`. See ESLint discussion thread [18131](https://github.com/eslint/eslint/discussions/18131).

### Origins

This config is a heavily modified variation on Anthony Fu's [@antfu/eslint-config](https://github.com/antfu/eslint-config). This package is a somewhat leaner approach intended to work with other tools wrapped behind a monolithic CLI instead of handling everything on its own. It mainly leverages the factory / type generation implementation from the original repo, which itself builds on Kevin Deng's [@sxzz/eslint-config](https://github.com/sxzz/eslint-config). See the [modification notes](./modification-notes.md) for more details on what's changed from Anthony's approach.

### References

- [@antfu/eslint-config](https://github.com/antfu/eslint-config)
- [@sxzz/eslint-config](https://github.com/sxzz/eslint-config)
- [linting-setup-using-eslint](https://chris.lu/web_development/tutorials/next-js-static-mdx-blog/linting-setup-using-eslint)
- On [prefer-repository-shorthand](https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/issues/223)

<!-- license -->

## License

[MIT](license.txt) © [Eric Mika](https://ericmika.com)

<!-- /license -->
