rubocop-fixer について
rubocop-fixerは、プロジェクト固有の設定に従ってRailsプロジェクトのRubocop違反を修正するスキルです。ルールの確認、一般的な修正、意図的な例外の扱い、結果の検証を支援します。
主な機能
- 01Rubocop設定の確認
- 02プロジェクトやファイルの検査
- 03ルール文書による違反内容の把握
- 04RubyとRailsの一般的な修正
- 05既存違反向けTODOファイルの生成
- 06残存項目の検証と報告
活用シーン
- 01Railsプロジェクトのスタイル違反を修正
- 02特定ファイルやルールを検査
- 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. 違反を理解する
修正する前に、なぜそのコップがあるのかを理解します。
# 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
出力形式
違反を修正した後:
- 修正した違反 - コップと件数の一覧
- 手動での修正 - 人の判断を必要とする変更
- 残り - レビューが必要な違反
- 検証 -
bundle exec rubocopの出力
コメント
Markdown 対応