code-formatter-installer
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCode Formatter & Linter Installer
代码格式化与代码检查工具安装器
Establish consistent code formatting and linting across your project.
在项目中建立统一的代码格式化与代码检查规范。
Core Workflow
核心流程
- Detect stack: Identify language/framework (JS/TS, Python, Go, Rust)
- Choose tools: Select appropriate formatters and linters
- Generate configs: Create config files with best-practice rules
- Add scripts: Include npm/package scripts for format/lint
- Configure editor: Provide VS Code, IntelliJ, Vim settings
- Setup pre-commit: Add git hooks for automatic formatting
- CI integration: Suggest GitHub Actions or CI config
- 检测技术栈:识别语言/框架(JS/TS、Python、Go、Rust)
- 选择工具:挑选合适的格式化工具与代码检查工具
- 生成配置:创建遵循最佳实践规则的配置文件
- 添加脚本:包含用于格式化/检查的npm/package脚本
- 配置编辑器:提供VS Code、IntelliJ、Vim的设置方案
- 设置Git钩子:添加自动格式化的git hooks
- CI集成:建议GitHub Actions或CI配置
Tool Selection by Stack
按技术栈选择工具
JavaScript/TypeScript
JavaScript/TypeScript
- Formatter: Prettier
- Linter: ESLint + typescript-eslint
- Editor: EditorConfig
- Hooks: Husky + lint-staged
- 格式化工具:Prettier
- 代码检查工具:ESLint + typescript-eslint
- 编辑器配置:EditorConfig
- 钩子工具:Husky + lint-staged
Python
Python
- Formatter: Black + isort
- Linter: Ruff or Flake8 + mypy
- Hooks: pre-commit
- 格式化工具:Black + isort
- 代码检查工具:Ruff 或 Flake8 + mypy
- 钩子工具:pre-commit
Go
Go
- Formatter: gofmt + goimports
- Linter: golangci-lint
- 格式化工具:gofmt + goimports
- 代码检查工具:golangci-lint
Rust
Rust
- Formatter: rustfmt
- Linter: clippy
- 格式化工具:rustfmt
- 代码检查工具:clippy
Configuration Templates
配置模板
Prettier (.prettierrc.json)
Prettier (.prettierrc.json)
json
{
"semi": true,
"trailingComma": "es5",
"singleQuote": true,
"printWidth": 100,
"tabWidth": 2,
"arrowParens": "avoid"
}json
{
"semi": true,
"trailingComma": "es5",
"singleQuote": true,
"printWidth": 100,
"tabWidth": 2,
"arrowParens": "avoid"
}ESLint (.eslintrc.json)
ESLint (.eslintrc.json)
json
{
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:react/recommended",
"prettier"
],
"rules": {
"no-console": "warn",
"@typescript-eslint/no-unused-vars": "error"
}
}json
{
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:react/recommended",
"prettier"
],
"rules": {
"no-console": "warn",
"@typescript-eslint/no-unused-vars": "error"
}
}EditorConfig (.editorconfig)
EditorConfig (.editorconfig)
ini
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 = falseini
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 = falsePython (pyproject.toml)
Python (pyproject.toml)
toml
[tool.black]
line-length = 100
target-version = ['py311']
[tool.isort]
profile = "black"
line_length = 100
[tool.mypy]
strict = true
warn_return_any = truetoml
[tool.black]
line-length = 100
target-version = ['py311']
[tool.isort]
profile = "black"
line_length = 100
[tool.mypy]
strict = true
warn_return_any = truePackage Scripts
包脚本
JavaScript/TypeScript (package.json)
JavaScript/TypeScript (package.json)
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"
}
}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
Python
bash
undefinedbash
undefinedMakefile or scripts
Makefile 或脚本
format:
black .
isort .
lint:
ruff check .
mypy .
format-check:
black --check .
isort --check .
undefinedformat:
black .
isort .
lint:
ruff check .
mypy .
format-check:
black --check .
isort --check .
undefinedGit Hooks Setup
Git钩子设置
Husky + lint-staged (Node.js)
Husky + lint-staged (Node.js)
- Install dependencies:
bash
npm install --save-dev husky lint-staged
npx husky init- Configure lint-staged (.lintstagedrc.json):
json
{
"*.{ts,tsx,js,jsx}": ["eslint --fix", "prettier --write"],
"*.{json,md,yml}": ["prettier --write"]
}- Add pre-commit hook (.husky/pre-commit):
bash
#!/bin/sh
npx lint-staged- 安装依赖:
bash
npm install --save-dev husky lint-staged
npx husky init- 配置lint-staged (.lintstagedrc.json):
json
{
"*.{ts,tsx,js,jsx}": ["eslint --fix", "prettier --write"],
"*.{json,md,yml}": ["prettier --write"]
}- 添加pre-commit钩子 (.husky/pre-commit):
bash
#!/bin/sh
npx lint-stagedpre-commit (Python)
pre-commit (Python)
- Create .pre-commit-config.yaml:
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- Install:
bash
pip install pre-commit
pre-commit install- 创建 .pre-commit-config.yaml:
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- 安装:
bash
pip install pre-commit
pre-commit installEditor Configuration
编辑器配置
VS Code (settings.json)
VS Code (settings.json)
json
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
}
}json
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
}
}VS Code Extensions (.vscode/extensions.json)
VS Code 扩展 (.vscode/extensions.json)
json
{
"recommendations": [
"esbenp.prettier-vscode",
"dbaeumer.vscode-eslint",
"editorconfig.editorconfig",
"ms-python.black-formatter"
]
}json
{
"recommendations": [
"esbenp.prettier-vscode",
"dbaeumer.vscode-eslint",
"editorconfig.editorconfig",
"ms-python.black-formatter"
]
}IntelliJ/WebStorm
IntelliJ/WebStorm
- Enable: Settings → Languages & Frameworks → JavaScript → Prettier → On save
- Enable: Settings → Tools → Actions on Save → Reformat code
- 启用:设置 → 语言与框架 → JavaScript → Prettier → 保存时自动格式化
- 启用:设置 → 工具 → 保存时执行的操作 → 重新格式化代码
CI Integration
CI集成
GitHub Actions (.github/workflows/lint.yml)
GitHub Actions (.github/workflows/lint.yml)
yaml
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 typecheckyaml
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 typecheckPre-merge checks
合并前检查
yaml
undefinedyaml
undefined.github/workflows/pr-checks.yml
.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 }
undefined- name: Check formatting run: | npm run format:check || { echo "Code is not formatted. Run 'npm run format' locally." exit 1 }
undefinedInstallation Checklist
安装检查清单
For every setup, provide:
- Config files (.prettierrc, .eslintrc, .editorconfig, etc.)
- Ignore files (.prettierignore, .eslintignore)
- Package scripts (format, lint, format:check, lint:fix)
- Git hooks (husky/pre-commit)
- Editor settings (.vscode/settings.json)
- CI workflow (.github/workflows/lint.yml)
- Documentation (README section on running lint/format)
每次设置时,需确保包含以下内容:
- 配置文件(.prettierrc、.eslintrc、.editorconfig等)
- 忽略文件(.prettierignore、.eslintignore)
- 包脚本(format、lint、format:check、lint:fix)
- Git钩子(husky/pre-commit)
- 编辑器设置(.vscode/settings.json)
- CI工作流(.github/workflows/lint.yml)
- 文档说明(README中关于运行检查/格式化的章节)
Best Practices
最佳实践
- Run formatter last: Prettier should override other formatting rules
- Extend configs: Use popular presets (Airbnb, Standard, etc.)
- Ignore generated files: Add build outputs to ignore files
- Make hooks skippable: Allow for emergencies
git commit --no-verify - Document process: Add "Code Style" section to CONTRIBUTING.md
- Test on clean install: Ensure configs work without local editor setup
- Keep rules minimal: Start strict, relax if needed
- 最后运行格式化工具:Prettier应覆盖其他格式化规则
- 扩展配置:使用流行的预设(Airbnb、Standard等)
- 忽略生成文件:将构建输出添加到忽略文件中
- 允许跳过钩子:紧急情况下允许使用
git commit --no-verify - 记录流程:在CONTRIBUTING.md中添加「代码风格」章节
- 在全新环境测试:确保无需本地编辑器设置即可正常使用配置
- 保持规则精简:初始严格,必要时再放宽
Common Configurations
常见配置
See for ready-to-use config files:
assets/configs/- Next.js + TypeScript
- React + TypeScript
- Node.js + TypeScript
- Python FastAPI
- Python Django
可查看获取即用型配置文件:
assets/configs/- Next.js + TypeScript
- React + TypeScript
- Node.js + TypeScript
- Python FastAPI
- Python Django
Bypass Instructions (Emergencies)
紧急绕过说明
bash
undefinedbash
undefinedSkip pre-commit hooks
跳过pre-commit钩子
git commit --no-verify
git commit --no-verify
Skip CI checks (not recommended)
跳过CI检查(不推荐)
git push --no-verify
Document when bypass is acceptable (hotfixes, emergencies only).git push --no-verify
请记录可接受绕过的场景(仅适用于热修复、紧急情况)。