ESLint:Claude Code 实战指南
了解eslint:Claude Code 实战. 包含实用技巧和代码示例。
ESLintでコード品質を自動管理する
ESLintはJavaScript/TypeScriptの静的解析ツールで、コードの品質と样式を自動的にチェックします。ESLint v9以降のFlat Config形式により、配置がよりシンプルになりました。Claude Code Flat Configの设计からカスタムルールの创建まで的確にサポートします。
Flat Configの基本配置
让 Claude Code最適なESLint配置を依頼吧。
> Next.js + TypeScript项目用のESLint Flat Config创建。
> 厳格な类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类型チェック、导入順序整理、アクセシビリティチェックを含めて。
// eslint.config.mjs
import eslint from "@eslint/js";
import tseslint from "typescript-eslint";
import reactPlugin from "eslint-plugin-react";
import reactHooksPlugin from "eslint-plugin-react-hooks";
import jsxA11yPlugin from "eslint-plugin-jsx-a11y";
import importPlugin from "eslint-plugin-import";
export default tseslint.config(
// 基本配置
eslint.configs.recommended,
...tseslint.configs.strictTypeChecked,
...tseslint.configs.stylisticTypeChecked,
// グローバル無視パターン
{
ignores: [
"node_modules/",
".next/",
"out/",
"dist/",
"coverage/",
"*.config.{js,mjs}",
],
},
// TypeScript配置
{
languageOptions: {
parserOptions: {
project: true,
tsconfigRootDir: import.meta.dirname,
},
},
},
// React配置
{
plugins: {
react: reactPlugin,
"react-hooks": reactHooksPlugin,
"jsx-a11y": jsxA11yPlugin,
},
rules: {
...reactPlugin.configs.recommended.rules,
...reactHooksPlugin.configs.recommended.rules,
...jsxA11yPlugin.configs.recommended.rules,
"react/react-in-jsx-scope": "off",
"react/prop-types": "off",
"react/jsx-no-target-blank": "error",
},
settings: {
react: { version: "detect" },
},
},
// 导入順序
{
plugins: { import: importPlugin },
rules: {
"import/order": [
"error",
{
groups: [
"builtin",
"external",
"internal",
"parent",
"sibling",
"index",
],
"newlines-between": "always",
alphabetize: { order: "asc", caseInsensitive: true },
},
],
"import/no-duplicates": "error",
},
},
// 项目固有のルール
{
rules: {
"@typescript-eslint/no-unused-vars": [
"error",
{ argsIgnorePattern: "^_", varsIgnorePattern: "^_" },
],
"@typescript-eslint/consistent-type-imports": [
"error",
{ prefer: "type-imports" },
],
"@typescript-eslint/no-misused-promises": [
"error",
{ checksVoidReturn: { attributes: false } },
],
"no-console": ["warn", { allow: ["warn", "error"] }],
},
}
);
文件種類ごとの配置分離
测试文件やスクリプト文件で異なるルールを適用するパターンです。
// eslint.config.mjs(続き)
export default tseslint.config(
// ...上記の配置
// 测试文件用の緩和配置
{
files: ["**/*.test.{ts,tsx}", "**/*.spec.{ts,tsx}"],
rules: {
"@typescript-eslint/no-unsafe-assignment": "off",
"@typescript-eslint/no-unsafe-member-access": "off",
"@typescript-eslint/no-explicit-any": "off",
"no-console": "off",
},
},
// 配置文件用
{
files: ["*.config.ts", "*.config.mts"],
rules: {
"import/no-default-export": "off",
},
},
// 页面组件(Next.js)
{
files: ["src/app/**/page.tsx", "src/app/**/layout.tsx"],
rules: {
"import/no-default-export": "off",
},
}
);
カスタムルールの创建
项目固有のルールを让 Claude Code依頼して创建可以。
// eslint-rules/no-hardcoded-strings.ts
import { ESLintUtils } from "@typescript-eslint/utils";
const createRule = ESLintUtils.RuleCreator(
(name) => `https://example.com/rules/${name}`
);
export const noHardcodedStrings = createRule({
name: "no-hardcoded-strings",
meta: {
type: "suggestion",
docs: {
description: "JSX内のハードコード文字列を禁止し、i18nの使用を促す",
},
messages: {
noHardcodedString:
"ハードコードされた文字列を直接使用しないでください。t()関数を使用してください。",
},
schema: [],
},
defaultOptions: [],
create(context) {
return {
JSXText(node) {
const text = node.value.trim();
if (text.length > 0 && !/^[\s\d\W]+$/.test(text)) {
context.report({ node, messageId: "noHardcodedString" });
}
},
};
},
});
VS Code集成
ESLintをVS Codeで自動実行する配置です。
// .vscode/settings.json
{
"eslint.useFlatConfig": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
},
"eslint.validate": [
"javascript",
"javascriptreact",
"typescript",
"typescriptreact"
],
"eslint.rules.customizations": [
{ "rule": "no-console", "severity": "warn" },
{ "rule": "import/order", "severity": "warn" }
]
}
lint-stagedとの联动
コミット時に更改文件のみlintを実行する配置です。
// package.json
{
"lint-staged": {
"*.{ts,tsx}": ["eslint --fix", "prettier --write"],
"*.{json,md,yml}": ["prettier --write"]
}
}
总结
ESLintのFlat Configにより、配置の見通しが大幅に改善されました。Claude Codeを活用すれば、项目に最適なルール選定、TypeScript集成、カスタムルール创建まで高效地构建是可能的。
コードフォーマットの配置はPrettier配置自定义指南を、コミット時の自動実行はHusky + lint-staged配置指南。ESLint官方文档也建议确认一下。
#Claude Code
#ESLint
#コード品質
#TypeScript
#開発環境
Related Posts
Tips & Tricks
Tips & Tricks
10 个技巧让你的 Claude Code 生产力翻三倍
分享 10 个实用的 Claude Code 使用技巧。从提示词策略到工作流优化,这些方法让你今天就能提升效率。
Tips & Tricks
Tips & Tricks
Canvas/WebGL Optimization:Claude Code 实战指南
了解canvas/webgl optimization:Claude Code 实战. 包含实用技巧和代码示例。
Tips & Tricks
Tips & Tricks
Markdown Implementation:Claude Code 实战指南
了解markdown implementation:Claude Code 实战. 包含实用技巧和代码示例。