フォーマッター/リンターしたい(biome

$ biome format --write .
$ biome lint .

biomeはJS(やTS)用のフォーマッター&リンターです。 ゼロコンフィグで利用できます。 Rust製で高速に動作します。

インストールしたい

// プロジェクトにインストール
$ npm i -D --save-exact @biomejs/biome

-D--save-dev)オプションでdevDependenciesにインストールします。 --save-exactオプションでバージョンを固定できます。

スクリプト設定したい(package.json

{
    "name": "...",
    "scripts": {
        "format:check": "biome format .",
        "format:write": "biome format --write .",
        "lint:check": "biome lint .",
        "lint:fix": "biome lint --fix .",
        "...": "..."
    }
}
$ npm run format:write
$ npm run lint:check

check系(変更しない)とwrite/fix系(変更する)を分けておくと、 CIではcheck系を、ローカルではwrite/fix系を使う、という使い分けがしやすくなります。

フォーマッターしたい(biome format

$ biome format .
$ biome format --write .

biome formatでフォーマットが必要な箇所を検出できます。 --writeオプションで、ファイルを変更します。

リンターしたい(biome lint

$ biome lint
$ biome lint --fix

biome lintでリンターできます。 --fixオプションで、自動修正できる箇所を修正します。

ヒント

biome checkを使うと、フォーマット・リント・import整理(後述のassist)を まとめて1コマンドで実行できます。 CIでチェックだけしたい場合はbiome ciが便利です。

設定したい(biome.json

$ biome init
// -> biome.json

biome initコマンドで設定ファイル(biome.json)を生成できます。 ゼロコンフィグ(=設定ファイルなし)で使い始められますが、 きちんとしたプロジェクトの場合は、設定を追加しておくとよいです。

{
  "$schema": "https://biomejs.dev/schemas/2.5.8/schema.json",
  "vcs": {
    "enabled": true,
    "clientKind": "git",
    "useIgnoreFile": true
  },
  "files": {
    "includes": [
      "**/*.js",
      "**/*.ts",
      "!**/node_modules/",
      "!**/dist/",
      "!**/docs/",
      "!**/coverage/"
    ],
    "ignoreUnknown": true
  },
  "formatter": {
    "enabled": true,
    "indentStyle": "space",
    "indentWidth": 2
  },
  "assist": {
    "actions": {
      "source": {
        "organizeImports": "on"
      }
    }
  },
  "linter": {
    "enabled": true,
    "rules": {
      "recommended": true
    }
  },
  "javascript": {
    "formatter": {
      "quoteStyle": "double"
    }
  }
}

files.includesに、対象とするファイルのパターンと、 除外したいパターン(!で始まる否定パターン)をまとめて指定します。 Biome 2系では、files.includefiles.ignoreのような分離した書き方は廃止され、 このように1つの配列にまとめる形式に統一されました。

assist.actions.source.organizeImportsで、import文の並び替えを有効化できます。 (Biome 1系にあったorganizeImports.enabledは2系で廃止され、assist配下に統合されています。)

注意

Biomeは1系から2系への移行でいくつかの設定項目が変わっています。 古いバージョン向けの設定例をそのまま使うと、Found an unknown keyのようなエラーになることがあります。 biome migrateコマンドで、古い設定ファイルを現在のバージョン向けに自動変換できます。

リファレンス