Skip to content

Commit

Permalink
Update auto-gen-config's comment re auto-correct
Browse files Browse the repository at this point in the history
The auto-gen-config output will say "Cop supports --auto-correct" for
cops which do not support --auto-correct, but which do support
--auto-correct-all. Let's update the output to reflect that. When
someone is working on the todo list, this can be helpful information to
have at hand.

I'll often run something like:

`bin/rubocop --only Style/StringConcatenation -a` and then realize
actually that won't work, and this would save me a few seconds.
  • Loading branch information
maxjacobson authored and bbatsov committed Feb 3, 2022
1 parent 38b8c78 commit 2ca25cd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/rubocop/formatter/disabled_config_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,15 @@ 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 cop_class&.support_autocorrect?
if default_cfg.nil? || default_cfg['Safe'] || default_cfg['Safe'].nil?
output_buffer.puts '# Cop supports --auto-correct.'
else
output_buffer.puts '# Cop supports --auto-correct-all.'
end
end

return unless default_cfg

params = cop_config_params(default_cfg, cfg)
Expand Down
35 changes: 35 additions & 0 deletions spec/rubocop/cli/auto_gen_config_spec.rb
Original file line number Diff line number Diff line change
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.
# 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

0 comments on commit 2ca25cd

Please sign in to comment.