Aller au contenu

Assist

Ce contenu n’est pas encore disponible dans votre langue.

Assist actions tidy up your code, for example by sorting imports or the keys of an object. Unlike lint rules, they do not look for bugs or bad practices: every action comes with a fix, and the fix is safe to apply. Biome has 13 assist actions.

biome check reports code that an enabled action would change, and fails if it finds any. Add --write to apply the actions:

npx biome check --write

To apply only assist actions, without formatting or linting, disable the other tools for that run:

npx biome check --write --formatter-enabled=false --linter-enabled=false

To apply assist actions when you ask for them, without failing when they have not been applied, pass --enforce-assist=false:

npx biome check --enforce-assist=false

See the CLI reference for every option of the check command.

The most common assist action is organizeImports. It is enabled by default, and it:

  • Sorts imports and exports by how “far” their source is, from URLs and packages to relative paths.
  • Merges imports from the same module.
  • Sorts the names inside each import.
example.ts
// Before
import { b, a } from "./utils";
import fs from "node:fs";
import { c } from "./utils";
import React from "react";
// After
import fs from "node:fs";
import React from "react";
import { a, b, c } from "./utils";

Biome never moves an import past other code. Imports separated by another statement, or by a comment followed by a blank line, are sorted separately, but a blank line alone does not split them. To customize the order, for example to put your own packages in a separate group, see the options of organizeImports.

Assist actions are configured under assist.actions in your biome.json file, grouped the same way they appear in the list of assist actions. All current actions belong to the source group.

Some actions, like organizeImports, are enabled by default. Set an action to "on" to enable it:

biome.json
{
"assist": {
"actions": {
"source": {
"useSortedKeys": "on"
}
}
}
}

Set an action to "off" to disable it:

biome.json
{
"assist": {
"actions": {
"source": {
"organizeImports": "off"
}
}
}
}

Some actions accept options. To set them, replace "on" with an object that has a level and an options field:

biome.json
{
"assist": {
"actions": {
"source": {
"useSortedKeys": {
"level": "on",
"options": {
"sortOrder": "lexicographic"
}
}
}
}
}
}

The documentation page of each action lists the options it supports.

To configure actions differently for some files, use overrides. For example, a file that loads polyfills might need its imports to stay in their original order:

biome.json
{
"overrides": [
{
"includes": ["src/polyfills.ts"],
"assist": {
"actions": {
"source": {
"organizeImports": "off"
}
}
}
}
]
}

To skip files that you do not want changed, such as generated code, exclude them with assist.includes:

biome.json
{
"assist": {
"includes": ["**", "!**/generated/**"]
}
}

The formatter and the linter still process these files. To exclude a file from every tool, use files.includes instead.

If another tool already sorts your imports, such as an ESLint or Prettier plugin, and you want to keep it for now, disable assist actions with assist.enabled:

biome.json
{
"assist": {
"enabled": false
}
}

Biome then stops applying assist actions, both in the CLI and in your editor, while the formatter and the linter keep working.

If an action should not apply to some code, such as an object whose keys are in a meaningful order, add a suppression comment above it:

example.js
// biome-ignore assist/source/useSortedKeys: keys follow the visual order
const layout = { top: 0, right: 0, bottom: 0, left: 0 };

The Suppressions guide covers every way to write these comments.

Assist actions replace the rules that sort code in other tools, such as ESLint’s sort-imports and sort-keys, or the import ordering rules of eslint-plugin-import and eslint-plugin-perfectionist. To find the action that replaces a rule you use, check the action sources.

When you switch, turn off the rule in your previous tool, so that the two tools do not reorder the same code in different ways.