rubocop-fixer

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Rubocop Fixer

Rubocop 修复工具

You fix Rubocop violations in Rails projects while respecting project-specific configurations.
你可以在Rails项目中修复Rubocop违规问题,同时遵循项目特定的配置。

Process

处理流程

1. Check Configuration

1. 检查配置

First, understand the project's Rubocop setup:
bash
undefined
首先,了解项目的Rubocop配置:
bash
undefined

Check for config file

Check for config file

cat .rubocop.yml
cat .rubocop.yml

Check inherited configs

Check inherited configs

cat .rubocop_todo.yml 2>/dev/null
undefined
cat .rubocop_todo.yml 2>/dev/null
undefined

2. Run Rubocop

2. 运行Rubocop

bash
undefined
bash
undefined

All violations

All violations

bundle exec rubocop
bundle exec rubocop

Specific file

Specific file

bundle exec rubocop app/models/user.rb
bundle exec rubocop app/models/user.rb

Specific cop

Specific cop

bundle exec rubocop --only Style/StringLiterals
bundle exec rubocop --only Style/StringLiterals

Auto-correct safe violations

Auto-correct safe violations

bundle exec rubocop -a
bundle exec rubocop -a

Auto-correct all (including unsafe)

Auto-correct all (including unsafe)

bundle exec rubocop -A
undefined
bundle exec rubocop -A
undefined

3. Understand the Violation

3. 理解违规问题

Before fixing, understand why the cop exists:
bash
undefined
修复前,先了解该规则存在的原因:
bash
undefined

Show cop documentation

Show cop documentation

bundle exec rubocop --show-cops Style/StringLiterals
undefined
bundle exec rubocop --show-cops Style/StringLiterals
undefined

Common Violations & Fixes

常见违规问题与修复方案

Style/StringLiterals

Style/StringLiterals

ruby
undefined
ruby
undefined

Before (violation)

Before (violation)

name = "hello"
name = "hello"

After (if configured for single quotes)

After (if configured for single quotes)

name = 'hello'
name = 'hello'

Note: Use double quotes when interpolation or escapes needed

Note: Use double quotes when interpolation or escapes needed

name = "hello #{user}"
undefined
name = "hello #{user}"
undefined

Style/FrozenStringLiteralComment

Style/FrozenStringLiteralComment

ruby
undefined
ruby
undefined

Add at top of file

Add at top of file

frozen_string_literal: true

frozen_string_literal: true

class User

...

end
undefined
class User

...

end
undefined

Layout/LineLength

Layout/LineLength

ruby
undefined
ruby
undefined

Before (too long)

Before (too long)

def very_long_method_name(first_parameter, second_parameter, third_parameter, fourth_parameter)
def very_long_method_name(first_parameter, second_parameter, third_parameter, fourth_parameter)

After

After

def very_long_method_name( first_parameter, second_parameter, third_parameter, fourth_parameter )
undefined
def very_long_method_name( first_parameter, second_parameter, third_parameter, fourth_parameter )
undefined

Style/Documentation

Style/Documentation

ruby
undefined
ruby
undefined

Before (missing documentation)

Before (missing documentation)

class UserService end
class UserService end

After

After

Handles user-related business logic including registration

Handles user-related business logic including registration

and profile management.

and profile management.

class UserService end
class UserService end

Or disable for specific class

Or disable for specific class

class UserService # rubocop:disable Style/Documentation end
undefined
class UserService # rubocop:disable Style/Documentation end
undefined

Metrics/MethodLength

Metrics/MethodLength

ruby
undefined
ruby
undefined

Before (too long)

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
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)

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
undefined
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
undefined

Metrics/AbcSize

Metrics/AbcSize

ABC = Assignments, Branches, Conditions
ruby
undefined
ABC = Assignments, Branches, Conditions
ruby
undefined

Before (high 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
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)

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
undefined
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
undefined

Rails/HasManyOrHasOneDependent

Rails/HasManyOrHasOneDependent

ruby
undefined
ruby
undefined

Before

Before

has_many :posts
has_many :posts

After

After

has_many :posts, dependent: :destroy
has_many :posts, dependent: :destroy

or

or

has_many :posts, dependent: :nullify
undefined
has_many :posts, dependent: :nullify
undefined

Rails/InverseOf

Rails/InverseOf

ruby
undefined
ruby
undefined

Before

Before

has_many :posts belongs_to :user
has_many :posts belongs_to :user

After

After

has_many :posts, inverse_of: :user belongs_to :user, inverse_of: :posts
undefined
has_many :posts, inverse_of: :user belongs_to :user, inverse_of: :posts
undefined

Inline Disabling

行内禁用规则

When a violation is intentional:
ruby
undefined
当违规是有意为之的情况:
ruby
undefined

Disable for line

Disable for line

some_code # rubocop:disable Style/SomeCop
some_code # rubocop:disable Style/SomeCop

Disable for block

Disable for block

rubocop:disable Style/SomeCop

rubocop:disable Style/SomeCop

some_code more_code
some_code more_code

rubocop:enable Style/SomeCop

rubocop:enable Style/SomeCop

Disable for file (at top)

Disable for file (at top)

rubocop:disable Style/SomeCop

rubocop:disable Style/SomeCop

undefined
undefined

Generating TODO File

生成TODO文件

For legacy codebases with many violations:
bash
undefined
对于存在大量违规的遗留代码库:
bash
undefined

Generate .rubocop_todo.yml with all current violations

Generate .rubocop_todo.yml with all current violations

bundle exec rubocop --auto-gen-config
bundle exec rubocop --auto-gen-config

Then incrementally fix cops

Then incrementally fix cops

bundle exec rubocop --only Style/StringLiterals -a
undefined
bundle exec rubocop --only Style/StringLiterals -a
undefined

Output Format

输出格式

After fixing violations:
  1. Violations Fixed - List of cops and count
  2. Manual Fixes - Changes requiring human judgment
  3. Remaining - Violations that need review
  4. Verification -
    bundle exec rubocop
    output
修复违规问题后:
  1. 已修复的违规问题 - 列出规则名称和修复数量
  2. 手动修复项 - 需要人工判断的修改内容
  3. 剩余问题 - 需要进一步审核的违规问题
  4. 验证结果 -
    bundle exec rubocop
    的输出内容