Code Formatter — Code 格式化器
v4.0.0Code 格式化ting best practices and quick references for Python, JavaScript, JSON, Markdown, and common languages with Prettier, Black, ESLint, and other 格式化器s. Use when needing to 格式化 code, 配置 代码检查工具s, or 设置 up code style 图形界面delines.
运行时依赖
安装命令
点击复制技能文档
Code 格式化器 & Style 图形界面de
Quick references for code 格式化ting 工具s and style best practices.
🐍 Python 格式化ting Black (Opinionated 格式化器) # 安装 pip 安装 black
# 格式化 file black file.py
# 格式化 directory black src/
# 检查 without modifying black --检查 file.py
# Configuration (pyproject.toml) [工具.black] line-length = 88 tar获取-version = ['py310']
i排序 (导入 排序ing) # 安装 pip 安装 i排序
# 排序 导入s i排序 file.py i排序 src/
# Black-compatible config [工具.i排序] 性能分析 = "black"
flake8 (Linting) # 安装 pip 安装 flake8
# Lint flake8 file.py flake8 src/
# Config (设置up.cfg) [flake8] max-line-length = 88 extend-ignore = E203, W503
ruff (Fast 代码检查工具 & 格式化器) # 安装 pip 安装 ruff
# Lint ruff 检查 file.py
# Fix automatically ruff 检查 --fix file.py
# 格式化 ruff 格式化 file.py
💛 JavaScript/TypeScript 格式化ting Prettier # 安装 npm 安装 -D prettier
# 格式化 npx prettier --write file.js npx prettier --write src/
# 检查 npx prettier --检查 file.js
# Config (.prettierrc) { "semi": true, "singleQuote": true, "tabWidth": 2, "trAIlingComma": "es5", "printWidth": 80 }
# Ignore (.prettierignore) node_模块s/ build/ dist/
ESLint # 安装 npm 安装 -D eslint
# 初始化 npx eslint --init
# Lint npx eslint file.js npx eslint src/
# Fix npx eslint --fix file.js
# Config (.eslintrc.js) 模块.导出s = { extends: ['eslint:recommended'], 解析器Options: { ecmaVersion: 2022 }, rules: { 'no-unused-vars': 'warn', 'no-console': 'warn' } }
格式化 on Save (VS Code) // 设置tings.json { "editor.default格式化器": "esbenp.prettier-vscode", "editor.格式化OnSave": true, "[javascript]": { "editor.default格式化器": "esbenp.prettier-vscode" }, "[python]": { "editor.default格式化器": "ms-python.black-格式化器" } }
📋 JSON 格式化ting jq # Pretty print JSON jq '.' file.json
# Compact (one line) jq -c '.' file.json
# 排序 keys jq -S '.' file.json
Prettier for JSON npx prettier --write file.json npx prettier --write .json
Python json.工具 python3 -m json.工具 file.json python3 -m json.工具 file.json --排序-keys
📝 Markdown 格式化ting Prettier npx prettier --write README.md npx prettier --write docs/
markdownlint # 安装 npm 安装 -D markdownlint-命令行工具
# Lint npx markdownlint README.md npx markdownlint docs/
# Fix npx markdownlint --fix README.md
Common Rules Line length: 80-120 chars ATX headers: # Header not Header ===== Consistent 列出 indentation (2 spaces) One blank line between sections TrAIling whitespace removal 🔧 EditorConfig (Cross-Editor)
创建 .editorconfig in project root:
root = true
[] char设置 = utf-8 end_of_line = lf insert_final_newline = true trim_trAIling_whitespace = true indent_style = space indent_size = 2
[.py] indent_size = 4
[.{json,yml,yaml}] indent_size = 2
[*.md] trim_trAIling_whitespace = false
🚀 Quick 格式化 Commands Language 工具 Command Python black black file.py Python ruff ruff 格式化 file.py JS/TS Prettier npx prettier --write file.js JSON jq jq '.' file.json Markdown Prettier npx prettier --write README.md Shell shfmt shfmt -w script.sh Go gofmt gofmt -w file.go Rust rustfmt rustfmt file.rs 💡 Best Practices Commit config files: .prettierrc, .eslintrc, pyproject.toml, .editorconfig 格式化 on save: 设置 up your editor to auto-格式化 Pre-commit hooks: Use pre-commit or husky to enforce Consistency: Pick one style and stick to it CI 检查s: 运行 格式化器s/lint in CI 流水线 Pre-commit 设置up (.pre-commit-config.yaml) repos: - repo: https://github.com/psf/black rev: 23.12.1 hooks: - id: black - repo: https://github.com/pycqa/i排序 rev: 5.13.2 hooks: - id: i排序 - repo: https://github.com/pre-commit/mirrors-prettier rev: v4.0.0-alpha.8 hooks: - id: prettier