mcpField
R
rubocop-fixer

rubocop-fixer

github.com/majesticlabs-dev测试与质量5062 次安装7 周前更新
代码仓库

关于 rubocop-fixer

rubocop-fixer 是一项用于修复 Rails 项目中 Rubocop 违规项的技能,并遵循项目自身配置。它涵盖检查规则、应用常见修复、处理有意保留的例外以及验证结果。

主要功能

  1. 01检查 Rubocop 配置
  2. 02运行项目或文件检查
  3. 03依据规则文档理解违规项
  4. 04应用常见 Ruby 与 Rails 修复
  5. 05为遗留违规生成 TODO 文件
  6. 06验证并报告剩余问题

适用场景

  1. 01修复 Rails 项目中的风格违规
  2. 02检查指定文件或规则
  3. 03处理大量遗留违规项

安装

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

$npx skills add https://github.com/majesticlabs-dev/majestic-marketplace --skill rubocop-fixer

安装后在 Claude Code 中输入/rubocop-fixer确认已加载。

详细介绍

Rubocop 修复器

修复 Rails 项目中的 Rubocop 违规,同时遵循项目专属配置。

流程

1. 检查配置

首先,了解项目的 Rubocop 设置:

# Check for config file
cat .rubocop.yml

# Check inherited configs
cat .rubocop_todo.yml 2>/dev/null
2. 运行 Rubocop
# All violations
bundle exec rubocop

# Specific file
bundle exec rubocop app/models/user.rb

# Specific cop
bundle exec rubocop --only Style/StringLiterals

# Auto-correct safe violations
bundle exec rubocop -a

# Auto-correct all (including unsafe)
bundle exec rubocop -A
3. 了解违规原因

修复前,先了解该 cop 存在的原因:

# Show cop documentation
bundle exec rubocop --show-cops Style/StringLiterals

常见违规及修复方法

Style/StringLiterals
# Before (violation)
name = "hello"

# After (if configured for single quotes)
name = 'hello'

# Note: Use double quotes when interpolation or escapes needed
name = "hello #{user}"
Style/FrozenStringLiteralComment
# Add at top of file
# frozen_string_literal: true

class User
  # ...
end
Layout/LineLength
# Before (too long)
def very_long_method_name(first_parameter, second_parameter, third_parameter, fourth_parameter)

# After
def very_long_method_name(
  first_parameter,
  second_parameter,
  third_parameter,
  fourth_parameter
)
Style/Documentation
# Before (missing documentation)
class UserService
end

# After
# Handles user-related business logic including registration
# and profile management.
class UserService
end

# Or disable for specific class
class UserService # rubocop:disable Style/Documentation
end
Metrics/MethodLength
# Before (too long)
def process_order
  validate_items
  calculate_subtotal
  apply_discounts
  calculate_tax
  calculate_shipping
  finalize_total
  create_invoice
  send_confirmation
  update_inventory
  notify_warehouse
end

# After (extract methods)
def process_order
  prepare_order
  complete_order
  post_order_tasks
end

private

def prepare_order
  validate_items
  calculate_totals
end

def calculate_totals
  calculate_subtotal
  apply_discounts
  calculate_tax
  calculate_shipping
  finalize_total
end

def complete_order
  create_invoice
  send_confirmation
end

def post_order_tasks
  update_inventory
  notify_warehouse
end
Metrics/AbcSize

ABC = 赋值、分支、条件

# Before (high ABC)
def process(user)
  if user.active?
    user.name = params[:name]
    user.email = params[:email]
    user.role = params[:role]
    user.save!
    notify(user)
  end
end

# After (lower ABC)
def process(user)
  return unless user.active?

  update_user_attributes(user)
  user.save!
  notify(user)
end

def update_user_attributes(user)
  user.assign_attributes(user_params)
end

def user_params
  params.slice(:name, :email, :role)
end
Rails/HasManyOrHasOneDependent
# Before
has_many :posts

# After
has_many :posts, dependent: :destroy
# or
has_many :posts, dependent: :nullify
Rails/InverseOf
# Before
has_many :posts
belongs_to :user

# After
has_many :posts, inverse_of: :user
belongs_to :user, inverse_of: :posts

内联禁用

当有意保留某项违规时:

# Disable for line
some_code # rubocop:disable Style/SomeCop

# Disable for block
# rubocop:disable Style/SomeCop
some_code
more_code
# rubocop:enable Style/SomeCop

# Disable for file (at top)
# rubocop:disable Style/SomeCop

生成 TODO 文件

对于有许多违规的旧代码库:

# Generate .rubocop_todo.yml with all current violations
bundle exec rubocop --auto-gen-config

# Then incrementally fix cops
bundle exec rubocop --only Style/StringLiterals -a

输出格式

修复违规后:

  1. 已修复的违规 - 列出 cop 和数量
  2. 手动修复 - 需要人工判断的更改
  3. 剩余项 - 需要审查的违规
  4. 验证 - bundle exec rubocop 的输出

评论

支持 Markdown