# Plugins

A plugin adds mutators, presets and skip rules to Mootant. **Import it in the
configuration and give it a namespace: the namespace starts the id of everything
the plugin adds.**

## Add a plugin

Import the plugin in `mootant.config.ts` and put it in `plugins` under a
namespace in kebab case. Its mutators run only when a preset or `mutators` turns
them on:

```ts
import { defineConfig } from 'mootant'

import dates from './mootant/dates.ts'

export default defineConfig({
  mutators: {
    'dates/date-setter': 'on',
  },
  plugins: { dates },
})
```

Here the plugin is a file of the project, which
[Custom mutators](https://mootant.org/advanced/custom-mutators.md) writes. A plugin from a package is
imported by the name of the package, such as `mootant-plugin-dates`, a made-up
name in these examples.

A plugin can also bring presets, such as `dates/recommended`, which
[Write a plugin](#write-a-plugin) adds. Add one to `extends` after Mootant's
own:

```ts
export default defineConfig({
  extends: ['recommended', 'dates/recommended'],
  plugins: { dates },
})
```

A JSON configuration can't add plugins, since a plugin has to be imported.

## Ids

The key in `plugins` is the namespace. Each part of a plugin gets an id: the
namespace, a slash and its name in the plugin. Change the namespace to see the
ids:

| Part | Name in the plugin | Id under `dates` | Where you use the id |
| --- | --- | --- | --- |
| Mutator | `date-setter` | `dates/date-setter` | [`mutators`](https://mootant.org/guide/configuration.md#mutators), [`mootant-ignore-next-line`](https://mootant.org/mutators.md#ignore-mutants) |
| Preset | `recommended` | `dates/recommended` | [`extends`](https://mootant.org/guide/configuration.md#presets), [`--preset`](https://mootant.org/guide/cli.md#shared-options) |
| Skip rule | `not-a-date` | `dates/not-a-date` | [`--skipped`](https://mootant.org/mutators.md#skipped-changes) |

For example, this comment ignores the mutants of the plugin's mutator on the
next line:

```ts
// mootant-ignore-next-line dates/date-setter: the server sets the hours
date.setHours(hours)
```

A namespace must be in kebab case. Otherwise Mootant stops with an error:

```text
Error: Plugin namespace "Dates" is not kebab-case.
```

## Write a plugin

A plugin is an object with these parts, keyed by names in kebab case:

- `mutators`, required: the mutators of the plugin, each an object with `meta`
  and `create`, as [Custom mutators](https://mootant.org/advanced/custom-mutators.md) shows. A plugin
  of presets alone gives `{}`.
- `presets`, optional: presets with a `description` and `mutators`, and
  optionally `extends` and `settings`, like those of Mootant.
- `skipRules`, optional: the rules that the plugin's mutators name when they
  skip a change, each with a [category](https://mootant.org/mutators.md#skipped-changes) and
  `heuristic`, whether the rule rests on a guess.

This is the end of `mootant/dates.ts` from Custom mutators, with a preset added:

```ts
export default {
  presets: {
    recommended: {
      description: 'Turns on date-setter.',
      mutators: { 'dates/date-setter': 'on' },
    },
  },
  skipRules: {
    'not-a-date': { category: 'noise', heuristic: true },
  },
  mutators: {
    'date-setter': dateSetter,
  },
} satisfies Plugin
```

A preset names each mutator by its full id, here `dates/date-setter`, so it
works only under the namespace `dates`. Tell users which namespace to give the
plugin.

Mootant checks the shape of each plugin when it loads the configuration and
stops at the first problem:

```text
Error: Plugin "dates" is invalid: mutator "date-setter" has no meta.description.
```

### Publish a plugin

Node.js doesn't strip types from files in `node_modules`, so a package has to
ship JavaScript. If it ships a `.ts` file, loading the configuration fails:

```text
Error: Cannot load mootant.config.ts: Stripping types is currently unsupported for files under node_modules, for "file:///…/node_modules/mootant-plugin-dates/index.ts"
```

Build JavaScript and `.d.ts` files from the TypeScript sources, and export the
plugin as the default export of the package:

```json
{
  "name": "mootant-plugin-dates",
  "type": "module",
  "exports": {
    ".": {
      "types": "./dist/index.d.ts",
      "default": "./dist/index.js"
    }
  }
}
```

Take the types from `mootant` with `import type`: the built JavaScript then
doesn't load Mootant. Add `mootant` to `peerDependencies` with the versions you
tested the plugin with, since users install Mootant themselves.
