リンター

ソースコードを静的解析し、バグの可能性やスタイル違反を検出するツール

開発ツール品質

リンターとは

リンター (Linter) は、ソースコードを実行せずに静的解析し、バグの可能性、スタイル違反、セキュリティ上の問題を検出するツールである。名前は 1978 年に Stephen C. Johnson が作った C 言語の lint ツールに由来する。

リンターとフォーマッターの違い

ツール 目的
リンター バグ検出、品質チェック ESLint, Biome
フォーマッター コードスタイルの統一 Prettier, Biome
// リンターが検出する問題
const x = 10;        // ❌ 未使用変数 (no-unused-vars)
if (x = 10) {}       // ❌ 代入と比較の混同 (no-cond-assign)
eval('code');         // ❌ eval の使用 (no-eval)
console.log('debug'); // ❌ console の使用 (no-console)

// フォーマッターが修正する問題
const y=1+2;          // → const y = 1 + 2;
if(true){return}      // → if (true) { return; }

言語ごとのリンター

言語 リンター 特徴
TypeScript/JS ESLint プラグインエコシステムが豊富
TypeScript/JS Biome Rust 製、ESLint + Prettier の統合
Python Ruff Rust 製、高速
Rust Clippy 公式リンター
Go golangci-lint 複数リンターの統合
CSS Stylelint CSS/SCSS のリンター

ESLint の設定

// eslint.config.js (Flat Config)
import tseslint from 'typescript-eslint';

export default tseslint.config(
  ...tseslint.configs.recommended,
  {
    rules: {
      '@typescript-eslint/no-unused-vars': 'error',
      '@typescript-eslint/no-explicit-any': 'error',
      'no-console': 'warn',
    },
  },
);

Biome (ESLint + Prettier の代替)

// biome.json
{
  "linter": { "enabled": true },
  "formatter": { "enabled": true, "indentStyle": "space" }
}

Biome は Rust 製で ESLint + Prettier の 10〜100 倍高速。リントとフォーマットを 1 つのツールで行える。

CI での実行

# GitHub Actions
- run: npx eslint . --max-warnings 0
- run: npx prettier --check .

--max-warnings 0 で警告も CI を失敗させる。

Git フックとの連携

# .husky/pre-commit
npx lint-staged
{ "lint-staged": { "*.{ts,tsx}": ["eslint --fix", "prettier --write"] } }

コミット時に変更ファイルだけリント + フォーマットする。

詳しくは関連書籍を参照。

関連用語