mcpField
C
code-formatter-installer

code-formatter-installer

github.com/patricio0312rev开发63264 次安装41 周前更新
代码仓库

关于 code-formatter-installer

code-formatter-installer 是面向开发者的代码格式化与 lint 配置技能,可根据项目技术栈生成配置、脚本和集成建议,覆盖从编辑器到 CI 的设置。

主要功能

  1. 01识别 JavaScript/TypeScript、Python、Go 和 Rust 技术栈
  2. 02选择对应的格式化与 lint 工具
  3. 03生成配置文件和包脚本
  4. 04提供编辑器、Git 钩子及 CI 设置建议
  5. 05提供常见框架的配置模板

适用场景

  1. 01为新项目建立统一的格式化和 lint 规则
  2. 02配置提交前自动格式化代码
  3. 03在 CI 中检查格式与 lint 问题

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/patricio0312rev/skills --skill code-formatter-installer

安装后在 Claude Code 中输入/code-formatter-installer确认已加载。

详细介绍

代码格式化工具与代码检查工具安装

在项目中统一代码格式和代码检查规范。

核心工作流程

  1. 检测技术栈:识别语言/框架(JS/TS、Python、Go、Rust)
  2. 选择工具:选择合适的格式化工具和代码检查工具
  3. 生成配置:创建包含最佳实践规则的配置文件
  4. 添加脚本:添加用于格式化/代码检查的 npm/package 脚本
  5. 配置编辑器:提供 VS Code、IntelliJ、Vim 设置
  6. 设置提交前检查:添加自动格式化的 Git 钩子
  7. 集成 CI:提供 GitHub Actions 或 CI 配置建议

按技术栈选择工具

JavaScript/TypeScript
  • 格式化工具:Prettier
  • 代码检查工具:ESLint + typescript-eslint
  • 编辑器:EditorConfig
  • 钩子:Husky + lint-staged
Python
  • 格式化工具:Black + isort
  • 代码检查工具:Ruff 或 Flake8 + mypy
  • 钩子:pre-commit
Go
  • 格式化工具:gofmt + goimports
  • 代码检查工具:golangci-lint
Rust
  • 格式化工具:rustfmt
  • 代码检查工具:clippy

配置模板

Prettier (.prettierrc.json)
{
  "semi": true,
  "trailingComma": "es5",
  "singleQuote": true,
  "printWidth": 100,
  "tabWidth": 2,
  "arrowParens": "avoid"
}
ESLint (.eslintrc.json)
{
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:react/recommended",
    "prettier"
  ],
  "rules": {
    "no-console": "warn",
    "@typescript-eslint/no-unused-vars": "error"
  }
}
EditorConfig (.editorconfig)
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.py]
indent_size = 4

[*.md]
trim_trailing_whitespace = false
Python (pyproject.toml)
[tool.black]
line-length = 100
target-version = ['py311']

[tool.isort]
profile = "black"
line_length = 100

[tool.mypy]
strict = true
warn_return_any = true

Package 脚本

JavaScript/TypeScript (package.json)
{
  "scripts": {
    "format": "prettier --write .",
    "format:check": "prettier --check .",
    "lint": "eslint . --ext .ts,.tsx,.js,.jsx",
    "lint:fix": "eslint . --ext .ts,.tsx,.js,.jsx --fix",
    "typecheck": "tsc --noEmit"
  }
}
Python
# Makefile or scripts
format:
	black .
	isort .

lint:
	ruff check .
	mypy .

format-check:
	black --check .
	isort --check .

Git 钩子设置

Husky + lint-staged (Node.js)
  1. 安装依赖:
npm install --save-dev husky lint-staged
npx husky init
  1. 配置 lint-staged (.lintstagedrc.json):
{
  "*.{ts,tsx,js,jsx}": ["eslint --fix", "prettier --write"],
  "*.{json,md,yml}": ["prettier --write"]
}
  1. 添加 pre-commit 钩子 (.husky/pre-commit):
#!/bin/sh
npx lint-staged
pre-commit (Python)
  1. 创建 .pre-commit-config.yaml:
repos:
  - repo: https://github.com/psf/black
    rev: 24.1.1
    hooks:
      - id: black

  - repo: https://github.com/pycqa/isort
    rev: 5.13.2
    hooks:
      - id: isort

  - repo: https://github.com/astral-sh/ruff-pre-commit
    rev: v0.2.0
    hooks:
      - id: ruff
        args: [--fix]

  - repo: https://github.com/pre-commit/mirrors-mypy
    rev: v1.8.0
    hooks:
      - id: mypy
  1. 安装:
pip install pre-commit
pre-commit install

编辑器配置

VS Code (settings.json)
{
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "editor.formatOnSave": true,
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
  },
  "[python]": {
    "editor.defaultFormatter": "ms-python.black-formatter"
  }
}
VS Code 扩展 (.vscode/extensions.json)
{
  "recommendations": [
    "esbenp.prettier-vscode",
    "dbaeumer.vscode-eslint",
    "editorconfig.editorconfig",
    "ms-python.black-formatter"
  ]
}
IntelliJ/WebStorm
  • 启用:Settings → Languages & Frameworks → JavaScript → Prettier → On save
  • 启用:Settings → Tools → Actions on Save → Reformat code

CI 集成

GitHub Actions (.github/workflows/lint.yml)
name: Lint

on: [push, pull_request]

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
      - run: npm ci
      - run: npm run format:check
      - run: npm run lint
      - run: npm run typecheck
合并前检查
# .github/workflows/pr-checks.yml
- name: Check formatting
  run: |
    npm run format:check || {
      echo "Code is not formatted. Run 'npm run format' locally."
      exit 1
    }

安装检查清单

对于每种设置,请提供:

  • 配置文件(.prettierrc、.eslintrc、.editorconfig 等)
  • 忽略文件(.prettierignore、.eslintignore)
  • Package 脚本(format、lint、format:check、lint:fix)
  • Git 钩子(husky/pre-commit)
  • 编辑器设置(.vscode/settings.json)
  • CI 工作流(.github/workflows/lint.yml)
  • 文档(README 中关于运行代码检查/格式化的章节)

最佳实践

  1. 最后运行格式化工具:Prettier 应覆盖其他格式规则
  2. 扩展配置:使用流行的预设(Airbnb、Standard 等)
  3. 忽略生成文件:将构建产物添加到忽略文件中
  4. 允许跳过钩子:紧急情况下允许使用 git commit --no-verify
  5. 记录流程:在 CONTRIBUTING.md 中添加“代码风格”章节
  6. 在全新安装环境中测试:确保配置无需本地编辑器设置也能正常工作
  7. 保持规则精简:从严格规则开始,必要时再放宽

常见配置

可在 assets/configs/ 中查看可直接使用的配置文件:

  • Next.js + TypeScript
  • React + TypeScript
  • Node.js + TypeScript
  • Python FastAPI
  • Python Django

绕过说明(紧急情况)

# Skip pre-commit hooks
git commit --no-verify

# Skip CI checks (not recommended)
git push --no-verify

说明哪些情况下可以绕过(仅限热修复和紧急情况)。

评论

支持 Markdown