MCP HubMCP Hub
返回技能列表

code-formatter

jeremylongshore
更新于 Today
80 次查看
712
74
712
在 GitHub 上查看
文档automation

关于

This Claude Skill automatically formats and validates code using Prettier for languages like JavaScript, TypeScript, and CSS. Use it when a developer asks to format, clean up, or check code consistency. It handles multiple file types and integrates with standard development tools.

快速安装

Claude Code

推荐
插件命令推荐
/plugin add https://github.com/jeremylongshore/claude-code-plugins-plus
Git 克隆备选方式
git clone https://github.com/jeremylongshore/claude-code-plugins-plus.git ~/.claude/skills/code-formatter

在 Claude Code 中复制并粘贴此命令以安装该技能

技能文档

Code Formatter Skill

This skill provides comprehensive code formatting and validation capabilities using industry-standard tools like Prettier.

Prerequisites

  • Node.js and npm/npx installed
  • Prettier available globally or locally
  • Write permissions for target files
  • Supported file types in the project

Instructions

1. Analyze Current Formatting

First, check the current formatting state of files:

# Check if prettier is available
npx prettier --version || npm install -g prettier

# Find all formattable files
find . -type f \( -name "*.js" -o -name "*.jsx" -o -name "*.ts" -o -name "*.tsx" -o -name "*.json" -o -name "*.css" -o -name "*.md" \) -not -path "*/node_modules/*" -not -path "*/dist/*"

# Check which files need formatting
npx prettier --check "**/*.{js,jsx,ts,tsx,json,css,md}" --ignore-path .prettierignore

2. Configure Formatting Rules

Create or check for existing Prettier configuration:

// .prettierrc
{
  "semi": true,
  "singleQuote": true,
  "tabWidth": 2,
  "trailingComma": "es5",
  "printWidth": 100,
  "bracketSpacing": true,
  "arrowParens": "avoid"
}

3. Apply Formatting

Format individual files or entire directories:

# Format a single file
npx prettier --write src/app.js

# Format all JavaScript files
npx prettier --write "**/*.js"

# Format with specific config
npx prettier --write --config .prettierrc "src/**/*.{js,jsx,ts,tsx}"

# Dry run to see what would change
npx prettier --check src/

4. Set Up Ignore Patterns

Create .prettierignore for files to skip:

# Dependencies
node_modules/
vendor/

# Build outputs
dist/
build/
*.min.js
*.min.css

# Generated files
coverage/
*.lock

5. Integrate with Git Hooks (Optional)

Set up pre-commit formatting:

# Install husky and lint-staged
npm install --save-dev husky lint-staged

# Configure in package.json
{
  "lint-staged": {
    "*.{js,jsx,ts,tsx,json,css,md}": [
      "prettier --write"
    ]
  }
}

Output

The formatter provides detailed feedback:

Success Output

✓ Formatted: src/app.js
✓ Formatted: src/components/Button.jsx
✓ Formatted: package.json
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✅ Successfully formatted 3 files

Validation Output

Checking formatting...
[warn] src/app.js
[warn] src/utils/helper.ts
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Found 2 files that need formatting

Error Output

✗ Error: Cannot format src/broken.js
  SyntaxError: Unexpected token (line 15:8)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
❌ Failed to format 1 file

Error Handling

Common issues and solutions:

1. Prettier Not Found

# Install globally
npm install -g prettier

# Or use npx (no installation needed)
npx prettier --version

2. Syntax Errors

# Validate JavaScript syntax first
npx eslint src/app.js --fix-dry-run

# Check for parsing errors
npx prettier --debug-check src/app.js

3. Configuration Conflicts

# Find all config files
find . -name ".prettier*" -o -name "prettier.config.js"

# Use specific config
npx prettier --config ./custom-prettier.json --write src/

4. Permission Issues

# Check file permissions
ls -la src/app.js

# Fix permissions if needed
chmod u+w src/app.js

Resources

File Type Support

ExtensionLanguageNotes
.js, .jsxJavaScriptES6+ supported
.ts, .tsxTypeScriptType annotations preserved
.jsonJSONSorts keys optionally
.css, .scssCSS/SASSVendor prefixes handled
.md, .mdxMarkdownTable formatting
.yaml, .ymlYAMLPreserves comments
.htmlHTMLAttribute formatting
.vueVueTemplate + script + style
.svelteSvelteFull component support

Configuration Options

{
  // Semicolons
  "semi": true,              // Add semicolons

  // Quotes
  "singleQuote": true,       // Use single quotes
  "jsxSingleQuote": false,   // JSX double quotes

  // Indentation
  "tabWidth": 2,             // Spaces per tab
  "useTabs": false,          // Use spaces

  // Line Length
  "printWidth": 100,         // Line wrap length
  "proseWrap": "preserve",   // Markdown wrapping

  // Trailing Commas
  "trailingComma": "es5",    // Valid in ES5

  // Brackets
  "bracketSpacing": true,    // { foo: bar }
  "bracketSameLine": false,  // JSX brackets

  // Arrow Functions
  "arrowParens": "avoid",    // x => x vs (x) => x

  // Formatting
  "htmlWhitespaceSensitivity": "css",
  "endOfLine": "lf",
  "embeddedLanguageFormatting": "auto"
}

Command Reference

# Basic formatting
prettier --write <file>           # Format file
prettier --check <file>           # Check if formatted
prettier --debug-check <file>     # Debug parse errors

# Multiple files
prettier --write "src/**/*.js"    # Glob pattern
prettier --write . --ignore-path .gitignore

# Configuration
prettier --config <path>          # Use specific config
prettier --no-config              # Ignore config files
prettier --find-config-path <file> # Show config used

# Output options
prettier --list-different         # List unformatted files
prettier --log-level debug        # Verbose output
prettier --no-color              # Disable colors

# Special options
prettier --require-pragma         # Only format with @format
prettier --insert-pragma          # Add @format comment
prettier --range-start 10 --range-end 50  # Partial format

Integration Examples

VS Code Integration:

{
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "editor.formatOnSave": true,
  "[javascript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  }
}

CI/CD Pipeline:

# GitHub Actions
- name: Check formatting
  run: npx prettier --check .

# Fail on unformatted code
- name: Enforce formatting
  run: |
    npx prettier --check . || {
      echo "Code is not formatted!"
      exit 1
    }

Pre-commit Hook:

#!/bin/sh
# .git/hooks/pre-commit

files=$(git diff --cached --name-only --diff-filter=ACMR | grep -E '\.(js|jsx|ts|tsx|json|css|md)$')

if [ -n "$files" ]; then
  npx prettier --write $files
  git add $files
fi

Performance Tips

  1. Use .prettierignore to skip large/generated files
  2. Cache node_modules in CI environments
  3. Run in parallel for multiple files:
    find . -name "*.js" | xargs -P 4 -I {} npx prettier --write {}
    
  4. Use --cache flag for incremental formatting:
    npx prettier --write --cache src/
    

Related Tools

  • ESLint - Linting and code quality
  • Stylelint - CSS/SCSS linting
  • Markdownlint - Markdown style checking
  • Black - Python formatting
  • rustfmt - Rust formatting
  • gofmt - Go formatting

Version: 1.0.0 Last Updated: 2025-12-12 Compatibility: Claude Code v1.5.0+

GitHub 仓库

jeremylongshore/claude-code-plugins-plus
路径: plugins/examples/formatter/skills/code-formatter
aiautomationclaude-codedevopsmarketplacemcp

相关推荐技能

content-collections

Content Collections 是一个 TypeScript 优先的构建工具,可将本地 Markdown/MDX 文件转换为类型安全的数据集合。它专为构建博客、文档站和内容密集型 Vite+React 应用而设计,提供基于 Zod 的自动模式验证。该工具涵盖从 Vite 插件配置、MDX 编译到生产环境部署的完整工作流。

查看技能

sglang

SGLang是一个专为LLM设计的高性能推理框架,特别适用于需要结构化输出的场景。它通过RadixAttention前缀缓存技术,在处理JSON、正则表达式、工具调用等具有重复前缀的复杂工作流时,能实现极速生成。如果你正在构建智能体或多轮对话系统,并追求远超vLLM的推理性能,SGLang是理想选择。

查看技能

Algorithmic Art Generation

这个Claude Skill帮助开发者使用p5.js创建算法艺术,特别适用于生成式艺术和交互式可视化项目。它支持种子随机性、流场和粒子系统等关键技术,确保艺术作品的重复性和独特性。当讨论生成艺术、算法艺术或计算美学时,该技能会自动激活,指导开发者完成从概念设计到技术实现的全过程。

查看技能

cloudflare-turnstile

这个Skill提供完整的Cloudflare Turnstile集成知识,用于在表单、登录页面和API端点中实现无验证码的机器人防护。它支持React/Next.js/Hono等框架集成,涵盖令牌验证、错误代码调试和端到端测试等场景。通过运行后台不可见挑战,在保持用户体验的同时有效阻止自动化流量和垃圾信息。

查看技能