rubocop-fixer
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseRubocop 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
undefinedCheck 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
undefinedcat .rubocop_todo.yml 2>/dev/null
undefined2. Run Rubocop
2. 运行Rubocop
bash
undefinedbash
undefinedAll 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
undefinedbundle exec rubocop -A
undefined3. Understand the Violation
3. 理解违规问题
Before fixing, understand why the cop exists:
bash
undefined修复前,先了解该规则存在的原因:
bash
undefinedShow cop documentation
Show cop documentation
bundle exec rubocop --show-cops Style/StringLiterals
undefinedbundle exec rubocop --show-cops Style/StringLiterals
undefinedCommon Violations & Fixes
常见违规问题与修复方案
Style/StringLiterals
Style/StringLiterals
ruby
undefinedruby
undefinedBefore (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}"
undefinedname = "hello #{user}"
undefinedStyle/FrozenStringLiteralComment
Style/FrozenStringLiteralComment
ruby
undefinedruby
undefinedAdd at top of file
Add at top of file
frozen_string_literal: true
frozen_string_literal: true
class User
...
end
undefinedclass User
...
end
undefinedLayout/LineLength
Layout/LineLength
ruby
undefinedruby
undefinedBefore (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
)
undefineddef very_long_method_name(
first_parameter,
second_parameter,
third_parameter,
fourth_parameter
)
undefinedStyle/Documentation
Style/Documentation
ruby
undefinedruby
undefinedBefore (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
undefinedclass UserService # rubocop:disable Style/Documentation
end
undefinedMetrics/MethodLength
Metrics/MethodLength
ruby
undefinedruby
undefinedBefore (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
undefineddef 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
undefinedMetrics/AbcSize
Metrics/AbcSize
ABC = Assignments, Branches, Conditions
ruby
undefinedABC = Assignments, Branches, Conditions
ruby
undefinedBefore (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
undefineddef 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
undefinedRails/HasManyOrHasOneDependent
Rails/HasManyOrHasOneDependent
ruby
undefinedruby
undefinedBefore
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
undefinedhas_many :posts, dependent: :nullify
undefinedRails/InverseOf
Rails/InverseOf
ruby
undefinedruby
undefinedBefore
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
undefinedhas_many :posts, inverse_of: :user
belongs_to :user, inverse_of: :posts
undefinedInline Disabling
行内禁用规则
When a violation is intentional:
ruby
undefined当违规是有意为之的情况:
ruby
undefinedDisable 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
undefinedundefinedGenerating TODO File
生成TODO文件
For legacy codebases with many violations:
bash
undefined对于存在大量违规的遗留代码库:
bash
undefinedGenerate .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
undefinedbundle exec rubocop --only Style/StringLiterals -a
undefinedOutput Format
输出格式
After fixing violations:
- Violations Fixed - List of cops and count
- Manual Fixes - Changes requiring human judgment
- Remaining - Violations that need review
- Verification - output
bundle exec rubocop
修复违规问题后:
- 已修复的违规问题 - 列出规则名称和修复数量
- 手动修复项 - 需要人工判断的修改内容
- 剩余问题 - 需要进一步审核的违规问题
- 验证结果 - 的输出内容
bundle exec rubocop