Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update auto-gen-config's comment re auto-correct #10384

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 16 additions & 2 deletions lib/rubocop/formatter/disabled_config_formatter.rb
Expand Up @@ -121,9 +121,14 @@ def output_cop_comments(output_buffer, cfg, cop_name, offense_count)
output_buffer.puts "# Offense count: #{offense_count}" if show_offense_counts?

cop_class = Cop::Registry.global.find_by_cop_name(cop_name)
output_buffer.puts '# Cop supports --auto-correct.' if cop_class&.support_autocorrect?

default_cfg = default_config(cop_name)

if supports_safe_auto_correct?(cop_class, default_cfg)
output_buffer.puts '# Cop supports --auto-correct.'
elsif supports_unsafe_autocorrect?(cop_class, default_cfg)
output_buffer.puts '# Cop supports --auto-correct-all.'
end

return unless default_cfg

params = cop_config_params(default_cfg, cfg)
Expand All @@ -132,6 +137,15 @@ def output_cop_comments(output_buffer, cfg, cop_name, offense_count)
output_cop_param_comments(output_buffer, params, default_cfg)
end

def supports_safe_auto_correct?(cop_class, default_cfg)
cop_class&.support_autocorrect? &&
(default_cfg.nil? || default_cfg['Safe'] || default_cfg['Safe'].nil?)
end

def supports_unsafe_autocorrect?(cop_class, default_cfg)
cop_class&.support_autocorrect? && !default_cfg.nil? && default_cfg['Safe'] == false
end

def cop_config_params(default_cfg, cfg)
default_cfg.keys -
%w[Description StyleGuide Reference Enabled Exclude Safe
Expand Down
35 changes: 35 additions & 0 deletions spec/rubocop/cli/auto_gen_config_spec.rb
Expand Up @@ -472,6 +472,41 @@ def fooBar; end
end
end

context 'when cop is not safe to auto-correct' do
it 'can generate a todo list, with the appropriate flag' do
create_file('example1.rb', <<~RUBY)
# frozen_string_literal: true

users = (user.name + ' ' + user.email) * 5
puts users
RUBY
create_file('.rubocop.yml', <<~YAML)
# The following cop supports auto-correction but is not safe
Style/StringConcatenation:
Enabled: true
YAML
expect(cli.run(%w[--auto-gen-config])).to eq(0)
expect($stderr.string).to eq('')
expect(Dir['.*']).to include('.rubocop_todo.yml')
todo_contents = File.read('.rubocop_todo.yml').lines[8..-1].join
expect(todo_contents).to eq(<<~YAML)
# Offense count: 1
# Cop supports --auto-correct-all.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line is the relevant assertion. Other examples would say --auto-correct instead of --auto-correct-all

# Configuration parameters: Mode.
Style/StringConcatenation:
Exclude:
- 'example1.rb'
YAML
expect(File.read('.rubocop.yml')).to eq(<<~YAML)
inherit_from: .rubocop_todo.yml

# The following cop supports auto-correction but is not safe
Style/StringConcatenation:
Enabled: true
YAML
end
end

context 'when existing config file has a YAML document start header' do
it 'inserts `inherit_from` key after hearder' do
create_file('example1.rb', <<~RUBY)
Expand Down