Pular para o conteúdo

Plugins

Este conteúdo não está disponível em sua língua ainda.

When your project has a convention that no built-in rule covers, such as banning a function or an import, a plugin can enforce it. You can use a plugin from the community, or write your own in one of two formats:

  • GritQL plugins describe the code you want to find by showing what it looks like. They need little code, and Biome keeps their memory usage low. However, they run on every file, even when the pattern cannot match, and they cannot use information such as the semantic model or the path of the file.
  • JavaScript plugins are JavaScript or TypeScript modules. They can use the syntax tree, the semantic model, and the path of the file, but they need much more code than GritQL for the same rule. Biome cannot limit the memory they use, and a slow plugin makes Biome slower for everyone who uses it.

The language support table shows which languages each format supports.

Community plugins are npm packages that contain GritQL or JavaScript rules. To use one, install the package and enable its rules in plugins, as described in Publish packages.

Plugin name Description Useful links
biome-plugin-padding

Define rules for blank lines between adjacent JavaScript and TypeScript statements and let Biome add or remove them automatically. A Biome alternative to ESLint Stylistic’s @stylistic/padding-line-between-statements.

To add your plugin to this list, open a pull request that adds it to src/content/plugins.yaml.

GritQL plugins are written in GritQL, a pattern language for finding and rewriting code.

  1. Write the plugin. Create a file with the .grit extension anywhere in your project. This one reports every call to Object.assign():

    no-object-assign.grit
    `$fn($args)` where {
    $fn <: `Object.assign`,
    register_diagnostic(
    span = $fn,
    message = "Prefer object spread instead of `Object.assign()`"
    )
    }

    The pattern between backticks matches code that looks like it, and $fn and $args capture whatever is in their place.

  2. Enable it in your configuration. Add the path of the file to the top-level plugins list in your biome.json file:

    biome.json
    {
    "plugins": ["./no-object-assign.grit"]
    }
  3. Run the linter. Plugins run as part of biome lint and biome check, and their diagnostics look like any other:

    src/config.ts:12:17 plugin ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    ✖ Prefer object spread instead of `Object.assign()`
    11 │ export function createConfig(config: Partial<Config>) {
    > 12 │ return resolve(Object.assign({}, DEFAULTS, config));
    │ ^^^^^^^^^^^^^
    13 │ }

register_diagnostic() reports a diagnostic whenever the pattern matches. It accepts four arguments:

  • span (required): the code to highlight, usually a variable you captured in the pattern.
  • message (required): the message to show.
  • severity: one of "hint", "info", "warn", or "error". The default is "error".
  • fix_kind: whether the plugin’s fix is "safe" or "unsafe". See Suggest a fix.

Plugins can also use any of GritQL’s built-in functions.

A plugin can rewrite the code it matches with the => operator:

no-console-log.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)`
}

Plugin fixes work like the fixes of built-in rules:

  • Without --write, Biome shows the fix but does not apply it.
  • With --write, Biome applies fixes marked with fix_kind = "safe".
  • With --write --unsafe, Biome also applies unsafe fixes.

If you leave out fix_kind, the fix is unsafe.

By default, a GritQL plugin matches JavaScript and TypeScript code. To match CSS or JSON instead, declare the language at the top of the file:

no-explicit-colors.grit
language css;
`$selector { $props }` where {
$props <: contains `color: $color` as $rule,
not $selector <: r"\.color-.*",
register_diagnostic(
span = $rule,
message = "Don't set explicit colors. Use `.color-*` classes instead."
)
}

For more examples, such as banning forEach or hardcoded colors, see the GritQL plugin examples. You can also read the GritQL reference to understand how to write patterns, use variables and conditions, and match Biome’s syntax nodes directly when a code snippet is not precise enough.

JavaScript plugins are JavaScript or TypeScript modules that export lint rules. They work for JavaScript, TypeScript, and their variants, such as JSX.

  1. Write the plugin. Create a file with the .js or .ts extension anywhere in your project. If you write it in TypeScript, install the @biomejs/runtime package, which provides the types. This one reports every call to Object.assign():

    no-object-assign.ts
    import { ast, defineRule, registerDiagnostic } from "@biomejs/runtime/plugin";
    export const noObjectAssign = defineRule({
    query: ast("JS_CALL_EXPRESSION"),
    run(node) {
    const callee = node.callee;
    if (callee?.text === "Object.assign") {
    registerDiagnostic(callee, "error", "Prefer object spread instead of `Object.assign()`");
    }
    },
    });

    defineRule() creates a rule from a query, which selects the syntax nodes to inspect, and a run function, which Biome calls for each of them. To find the name of a syntax node, such as JS_CALL_EXPRESSION, run biome inspect file or open the syntax view of the playground.

  2. Enable it in your configuration. Add the path of the file to the top-level plugins list in your biome.json file:

    biome.json
    {
    "plugins": ["./no-object-assign.ts"]
    }
  3. Run the linter. Plugins run as part of biome lint and biome check, and their diagnostics look like any other:

    src/config.ts:12:18 plugin ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    ✖ Prefer object spread instead of `Object.assign()`
    11 │ export function createConfig(config: Partial<Config>) {
    > 12 │ return resolve(Object.assign({}, DEFAULTS, config));
    │ ^^^^^^^^^^^^^
    13 │ }

The JavaScript plugin API reference lists the queries, the rule context, the semantic model, and the helpers to check node kinds and search the syntax tree.

registerDiagnostic() reports a diagnostic on a node. It accepts four arguments:

  • The node to highlight, or a token or a trivia piece to highlight only that part.
  • The severity, such as "information", "warning", or "error".
  • The message to show.
  • An optional fix, described in the next section.

To suggest a fix, create a mutation with createMutation(), add the edits to it, and pass it to registerDiagnostic() along with a message and a kind of "safe" or "unsafe". This rule removes debugger statements:

no-debugger.ts
import { ast, createMutation, defineRule, registerDiagnostic } from "@biomejs/runtime/plugin";
export const noDebugger = defineRule({
query: ast("JS_DEBUGGER_STATEMENT"),
run(node) {
const mutation = createMutation(node);
mutation.removeNode(node);
registerDiagnostic(node, "warning", "Unexpected debugger statement.", {
mutation,
message: "Remove the debugger statement.",
kind: "safe",
});
},
});

The fix applies like the fixes of GritQL plugins and built-in rules: --write applies safe fixes, and --write --unsafe also applies unsafe ones. A mutation can also replace nodes and tokens. The types in the @biomejs/runtime package document every method.

By default, a plugin runs on every file that Biome lints. To limit it to some files, use an object with a path and an includes list of glob patterns:

biome.json
{
"plugins": [
{
"path": "./react-plugin.grit",
"includes": ["src/components/**"]
},
{
"path": "./ts-only-plugin.grit",
"includes": ["src/**/*.ts", "!src/**/*.test.ts"]
}
]
}

In this example, the first plugin only runs on files in src/components/, and the second runs on TypeScript files in src/, except tests.

When a configuration extends a shared configuration, Biome resolves the plugin paths of the shared configuration from the directory of the configuration that extends it. To load a plugin that ships with the shared configuration instead, set resolutionKind to "config":

biome.shared.jsonc
{
"plugins": [
{
"path": "./plugins/no-object-assign.grit",
"resolutionKind": "config"
}
]
}

Then extend the shared configuration:

biome.json
{
"extends": ["./biome.shared.jsonc"]
}

Biome now resolves the plugin path from the directory of biome.shared.jsonc. The includes globs are still resolved from the directory of biome.json. See plugins.<ITEM>.resolutionKind for details.

To share plugins and configurations as npm packages, see Publish packages.

To list the plugins that your configuration enables, and the files each one runs on, use biome inspect plugins. It also shows the rule name of each GritQL plugin, which you use to silence it:

npx biome inspect plugins

Pass --path to list only the plugins that apply to one file. To run only your plugins, for example while you write one, pass --only=plugin to lint or check:

npx biome lint --only=plugin

You can silence a plugin’s diagnostics with a lint/plugin suppression comment, like a built-in rule. To silence only one plugin, add its name: the file name without the .grit extension for a GritQL plugin, or the name of the exported rule for a JavaScript plugin:

src/polyfills.js
// biome-ignore lint/plugin/no-object-assign: spread syntax is not available in this environment
const options = Object.assign({}, defaults, overrides);

To silence every plugin, use lint/plugin alone. See Suppressions for the other kinds of suppression comments.