ESLint 10
このスキルは、https://eslint.org/docs/latest/ に文書化されたESLint 10.14.0をカバーします。習慣との違い(フラット設定の落とし穴、意外なデフォルト、削除されたAPI、ドキュメントが推奨するパターン)をまとめています。各参照ファイルの記述は、出典ページが末尾に記載されています。
ナビゲーション方法
- 以下のクイックルールを読んでください。最もよくある間違いをカバーしています。
- タスクに該当する参照ファイルを、末尾の表から開いてください。
- コードが
.eslintrc、env、context.getScope()、FlatESLintを使用している場合、最初にreferences/migration.mdを読んでください。
クイックスタート
npm init @eslint/config@latest
// eslint.config.js
import js from "@eslint/js";
import globals from "globals";
import { defineConfig, globalIgnores } from "eslint/config";
export default defineConfig([
globalIgnores(["dist/"]),
{
files: ["**/*.js", "**/*.mjs", "**/*.cjs"],
plugins: { js },
extends: ["js/recommended"],
languageOptions: { globals: { ...globals.browser } },
rules: { "no-unused-vars": "warn" },
},
]);
https://eslint.org/docs/latest/use/getting-started 、 https://eslint.org/docs/latest/use/configure/configuration-files
- Node.js
^20.19.0、^22.13.0、または>=24。TypeScriptの型を使用するにはTypeScript 5.3+が必要です。 https://eslint.org/docs/latest/use/getting-started eslintと@eslint/jsをローカルのdevDependenciesとしてインストールします。グローバルインストールは推奨されず、プラグインは引き続きローカルにインストールする必要があります。globalsは別パッケージです。 https://eslint.org/docs/latest/use/getting-started 、 https://eslint.org/docs/latest/use/configure/migration-guideeslint.config.tsを使用するには、Node上でdevDependencyとしてjiti>= 2.2.0 が必要です。 https://eslint.org/docs/latest/use/configure/configuration-files
クイックルール
設定ファイル
- ESLintは、設定を拡張するかルールを有効にするまで、実質的に何もリントしません。 https://eslint.org/docs/latest/use/getting-started
- 設定の検索は、リント対象の各ファイルのディレクトリから始まり、上位に向かって行われます。
--configは1つのファイルに強制します。 https://eslint.org/docs/latest/use/configure/configuration-files 、 https://eslint.org/docs/latest/use/migrate-to-10.0.0 filesを持たないオブジェクトは、別のオブジェクトがマッチするすべてのファイルに適用されます。.ts、.md、.jsonをリントする場合、JSルールのスコープをfilesで指定し、常にfilesをextendsの隣に配置してください。 https://eslint.org/docs/latest/use/configure/configuration-files- オブジェクト内で単独の
ignores(グローバル ignores)はディレクトリにマッチできます。他のキーと一緒にある場合はファイルのみにマッチします:"dir/**"("dir/"ではありません)。globalIgnores()を推奨します。 https://eslint.org/docs/latest/use/configure/configuration-files - ドットファイルは、無視されない限りリントされます。デフォルトで無視されるのは
**/node_modules/と.git/のみです。 https://eslint.org/docs/latest/use/configure/ignore 、 https://eslint.org/docs/latest/use/configure/configuration-files eslint/configからincludeIgnoreFile(path, { gitignoreResolution: true })を使用して.gitignoreをインポートします。 https://eslint.org/docs/latest/use/configure/ignore- 環境はありません:
languageOptions.globalsでglobalsパッケージを使用してグローバル変数を宣言します。 https://eslint.org/docs/latest/use/configure/language-options - プラグイン、パーサー、プロセッサーはインポートされたオブジェクトです。プロセッサーは自動的に適用されることはありません。 https://eslint.org/docs/latest/use/configure/migration-guide
npx eslint --inspect-configまたは--print-config file.jsで、ファイルにマッチする設定を確認できます。 https://eslint.org/docs/latest/use/configure/debug
ルールとコメント
- 重要度のみのオーバーライドは、設定内および
/* eslint */コメント内の以前のオプションを維持します。 https://eslint.org/docs/latest/use/configure/rules 、 https://eslint.org/docs/latest/use/migrate-to-9.0.0 - インラインコメントではプラグインをロードできません。単独の
/* eslint-enable */はすべてのルールを再び有効にします。 https://eslint.org/docs/latest/use/configure/rules - 理由は
--の後に記述します:// eslint-disable-next-line no-console -- 理由。 https://eslint.org/docs/latest/use/configure/rules /* eslint-env */はESLint 10ではエラーになります。/* global name */または設定のglobalsを使用してください。 https://eslint.org/docs/latest/use/migrate-to-10.0.0no-unused-varsはデフォルトで未使用のcatch (e)を報告します:catch {と記述してください。 https://eslint.org/docs/latest/rules/no-unused-varsno-return-awaitは有効にしないでください。フォーマッターやESLint Stylisticをコアのレイアウトルールの代わりに使用してください。 https://eslint.org/docs/latest/rules/no-return-await 、 https://eslint.org/docs/latest/use/core-concepts/glossary
CLIとCI
- 終了コード1はエラー(または警告の超過)、2は設定または内部エラーを意味します。 https://eslint.org/docs/latest/use/command-line-interface
--quietは"warn"ルールを実行しません。ただし、--max-warningsも設定されている場合は、実行されますが報告されません。 https://eslint.org/docs/latest/use/command-line-interface- グロブは引用符で囲んでください。
--fixは標準入力で使用するとエラーになります:--fix-dry-run --format jsonを使用してください。 https://eslint.org/docs/latest/use/command-line-interface --cacheはファイル間の依存関係を無視します:型認識ルールやimportルールは古くなる可能性があります。 https://eslint.org/docs/latest/use/command-line-interface- 古いコードに対して
eslint --fix --suppress-allで厳格なルールを適用し、eslint-suppressions.jsonをコミットします。 https://eslint.org/docs/latest/use/suppressions
カスタムルールと統合
- ルールは
{ meta, create }です。meta.fixableとmeta.hasSuggestionsは修正や提案を提供するために必須であり、meta.schemaはオプションを受け入れるために必須です。 https://eslint.org/docs/latest/extend/custom-rules context.sourceCode、context.filename、sourceCode.getScope(node)を使用してください。context.getX()メソッドは廃止されました。 https://eslint.org/docs/latest/use/migrate-to-10.0.0 、 https://eslint.org/docs/latest/use/migrate-to-9.0.0new ESLint({ fix: true })はESLint.outputFixes(results)を実行するまで何も書き込みません。 https://eslint.org/docs/latest/integrate/nodejs-api- プラグインには
meta.name、meta.version、meta.namespaceを設定すると、デバッグが容易になり、キャッシュがより効果的になります。名前空間により、defineConfig()が別の名前でプラグインを見つけられるようになります。 https://eslint.org/docs/latest/extend/plugins
古いパターン
| 古いパターン | 現在のパターン |
|---|---|
.eslintrc.*、package.json の eslintConfig | eslint.config.js と defineConfig() |
"extends": "eslint:recommended" | extends: ["js/recommended"] と plugins: { js } |
env、/* eslint-env */ | languageOptions.globals、/* global */ |
.eslintignore、--ignore-path | globalIgnores()、includeIgnoreFile() |
overrides | files を持つ追加の設定オブジェクト |
--rulesdir、linter.defineRule() | インラインプラグイン plugins: { local: { rules } } |
--no-eslintrc | --no-config-lookup |
context.getScope()、context.getSourceCode() | sourceCode.getScope(node)、context.sourceCode |
| 関数スタイルのルール | { meta, create } |
FlatESLint、LegacyESLint、FlatRuleTester | ESLint、RuleTester |
require-jsdoc、valid-jsdoc | eslint-plugin-jsdoc |
詳細と完全なリスト:references/migration.md。
参照ファイル
| ファイル | 参照する目的 |
|---|---|
| references/configuration.md | 設定ファイル名と検索、TypeScript設定、設定オブジェクトのキー、files のマッチング、extends、言語オプション、グローバル変数、パーサー、プロセッサー、言語、デバッグ |
| references/ignoring-files.md | グローバルおよび非グローバルな ignores、デフォルトの無視、ドットファイル、無視解除のレシピ、.gitignore のインポート |
| references/rules-and-inline-config.md | 重要度、オプションのマージ、インライン設定コメント、無効化ディレクティブ、linterOptions、修正と提案、循環修正 |
| references/cli.md | グロブ、--fix、終了コード、--quiet、フォーマッター、キャッシュ、一括抑制、機能フラグ、並行処理、MCPサーバー |
| references/migration.md | ESLint 9および10の破壊的変更、コードモッド、eslintrcからフラット設定への移行、変更されたルールオプション、壊れたプラグイン |
| references/custom-rules.md | ルールの meta、ローカルルール、context.report、修正、提案、meta.schema、meta.defaultOptions、非推奨、削除された context メソッド |
| references/ast-scope-code-paths.md | セレクター、SourceCode メソッド、スコープ、スコープマネージャーオブジェクト、コードパスイベント、プロファイリング |
| references/testing-rules.md | RuleTester のデフォルト、有効ケースと無効ケース、修正と提案のアサーション、厳格チェック、フック |
| references/plugins-and-shareable-configs.md | プラグインオブジェクトと meta、プラグイン設定、公開、共有設定、カスタムパーサー、プロセッサー、言語、フォーマッター |
| references/nodejs-api.md | ESLint のオプションとメソッド、lintText、outputFixes、loadFormatter、LintResult、Linter、SourceCode |
| references/core-rules-problems.md | TypeScriptがカバーするルール、未使用および未宣言の変数、シャドウイング、条件、非同期エラー、正規表現の落とし穴 |
| references/core-rules-restrictions.md | no-restricted-imports、-globals、-properties、-exports、no-restricted-syntax のレシピ、その他の禁止事項、サイズと複雑さの制限 |
| references/core-rules-style.md | 提案ルールのデフォルト、レイアウトルールのデフォルト、削除されたルール、非推奨のオプション |
クロール済みページ:SOURCES.md。