Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions src/content/docs/linter/plugins.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ title: Linter Plugins
description: An overview of how to use Biome's Linter Plugins
---

Biome Linter supports [GritQL](/reference/gritql/) plugins. Currently, these
plugins allow you to match specific code patterns and register customized
diagnostic messages for them.
Biome Linter supports [GritQL](/reference/gritql/) plugins. Plugins can match
specific code patterns, report customized diagnostics, and suggest fixable
rewrites.

Here is an example of a plugin that reports on all usages of `Object.assign()`:

Expand Down Expand Up @@ -46,6 +46,27 @@ $ biome lint
14 │ }
```

Plugins can also suggest code rewrites using the `=>` operator:

```grit
`console.log($msg)` as $call where {
register_diagnostic(
span = $call,
message = "Use console.info instead of console.log.",
severity = "warn",
fix_kind = "safe"
),
$call => `console.info($msg)`
}
```

Rewrite behavior:

- Without `--write`, rewrites are shown as suggestions but not applied.
- With `--write`, Biome applies plugin rewrites marked with `fix_kind = "safe"`.
- With `--write --unsafe`, Biome also applies unsafe plugin rewrites.
- If `fix_kind` is omitted, the rewrite is treated as unsafe by default.

## Target Languages

A GritQL snippet always attempts to match against a given _target language_.
Expand All @@ -69,7 +90,7 @@ language css;
}
```

We currently do not support other target languages than JavaScript and CSS.
Biome currently supports JavaScript, CSS, and JSON target languages.

## Plugin API

Expand All @@ -81,10 +102,13 @@ Biome currently supports one extra function:

Registers a diagnostic to be reported whenever the pattern matches.

Supports three arguments:
Supports four arguments:

- `span` (required): The syntax node to attach the diagnostic to. This is
typically a variable that you matched within a code snippet.
- `message` (required): The message to show with the diagnostic.
- `severity`: The severity of the diagnostic. Allowed values are: `hint`,
`info`, `warn`, and `error`. By default, `error` is used.
- `fix_kind`: Safety of an associated rewrite fix (`safe` or `unsafe`). This is
only relevant when the pattern also uses the rewrite operator (`=>`). By
default, fixes are treated as unsafe.
4 changes: 4 additions & 0 deletions src/content/docs/reference/gritql.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ Biome uses GritQL for two purposes:
- The [`biome search`](/reference/cli/#biome-search) command, which we hope to
extend to our IDE extensions as well.

When GritQL is used in analyzer plugins, patterns can also include rewrites
(`=>`) to provide fixable diagnostics. See [Linter Plugins](/linter/plugins)
for details about safe vs unsafe fixes and `fix_kind`.

## Patterns

GritQL queries work through _patterns_. The most common pattern you will see is
Expand Down