Skip to content

Commit

Permalink
Update auto-gen-config's comment re auto-correct for `SafeAutoCorrect…
Browse files Browse the repository at this point in the history
…: false`

This PR is enhancement: #10384

The auto-gen-config output will say "Cop supports --auto-correct".
However, Cop with `SafeAutoCorrect: false`
outputs the same as follows:

```yaml
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: EnforcedStyle.
# SupportedStyles: always, always_true, never
Style/FrozenStringLiteralComment:
```

We'll want to execute the following commands
when we see it.

```
bundle exec rubocop --auto-correct foo.rb
```

However, it is not automatically corrected.

```
Offenses:

foo.rb:1:1: C: [Correctable] Style/FrozenStringLiteralComment: Missing frozen string literal comment.
[2, 1, 3].sort.first
^

1 file inspected, 5 offenses detected, 5 more offenses can be corrected with `rubocop -A`
```

So, we should suggest --auto-correct-all
for `SafeAutoCorrect: false` Cop as well.
  • Loading branch information
ydah authored and bbatsov committed May 31, 2022
1 parent 5af44d4 commit c40b99e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
@@ -0,0 +1 @@
* [#10673](https://github.com/rubocop/rubocop/pull/10673): Update auto-gen-config's comment re auto-correct for `SafeAutoCorrect: false`. ([@ydah][])
13 changes: 8 additions & 5 deletions lib/rubocop/formatter/disabled_config_formatter.rb
Expand Up @@ -123,7 +123,7 @@ def output_cop_comments(output_buffer, cfg, cop_name, offense_count)
cop_class = Cop::Registry.global.find_by_cop_name(cop_name)
default_cfg = default_config(cop_name)

if supports_safe_auto_correct?(cop_class, default_cfg)
if supports_safe_autocorrect?(cop_class, default_cfg)
output_buffer.puts '# This cop supports safe autocorrection (--autocorrect).'
elsif supports_unsafe_autocorrect?(cop_class, default_cfg)
output_buffer.puts '# This cop supports unsafe autocorrection (--autocorrect-all).'
Expand All @@ -137,13 +137,12 @@ 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?)
def supports_safe_autocorrect?(cop_class, default_cfg)
cop_class&.support_autocorrect? && (default_cfg.nil? || safe_autocorrect?(default_cfg))
end

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

def cop_config_params(default_cfg, cfg)
Expand Down Expand Up @@ -242,6 +241,10 @@ def output_exclude_path(output_buffer, exclude_path, parent)
regexp = exclude_path
output_buffer.puts " - !ruby/regexp /#{regexp.source}/"
end

def safe_autocorrect?(config)
config.fetch('Safe', true) && config.fetch('SafeAutoCorrect', true)
end
end
end
end
4 changes: 2 additions & 2 deletions lib/rubocop/runner.rb
Expand Up @@ -382,15 +382,15 @@ def offenses_to_report(offenses)
if @options[:display_only_fail_level_offenses]
offenses.select { |o| considered_failure?(o) }
elsif @options[:display_only_safe_correctable]
offenses.select { |o| supports_safe_auto_correct?(o) }
offenses.select { |o| supports_safe_autocorrect?(o) }
elsif @options[:display_only_correctable]
offenses.select(&:correctable?)
else
offenses
end
end

def supports_safe_auto_correct?(offense)
def supports_safe_autocorrect?(offense)
cop_class = Cop::Registry.global.find_by_cop_name(offense.cop_name)
default_cfg = default_config(offense.cop_name)

Expand Down
26 changes: 13 additions & 13 deletions spec/rubocop/cli/auto_gen_config_spec.rb
Expand Up @@ -172,7 +172,7 @@ def f
Max: 99
# Offense count: 1
# This cop supports safe autocorrection (--autocorrect).
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: EnforcedStyle.
# SupportedStyles: always, always_true, never
Style/FrozenStringLiteralComment:
Expand Down Expand Up @@ -220,7 +220,7 @@ def f
.to eq(<<~YAML)
# Offense count: 1
# This cop supports safe autocorrection (--autocorrect).
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: EnforcedStyle.
# SupportedStyles: always, always_true, never
Style/FrozenStringLiteralComment:
Expand Down Expand Up @@ -269,7 +269,7 @@ def f
.to eq(<<~YAML)
# Offense count: 1
# This cop supports safe autocorrection (--autocorrect).
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: EnforcedStyle.
# SupportedStyles: always, always_true, never
Style/FrozenStringLiteralComment:
Expand Down Expand Up @@ -370,7 +370,7 @@ def f
" - 'example1.rb'",
'',
'# Offense count: 1',
'# This cop supports safe autocorrection (--autocorrect).',
'# This cop supports unsafe autocorrection (--autocorrect-all).',
'# Configuration parameters: EnforcedStyle.',
'# SupportedStyles: always, always_true, never',
'Style/FrozenStringLiteralComment:',
Expand Down Expand Up @@ -408,7 +408,7 @@ def f
todo_contents = File.read('.rubocop_todo.yml').lines[8..].join
expect(todo_contents).to eq(<<~YAML)
# Offense count: 1
# This cop supports safe autocorrection (--autocorrect).
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: EnforcedStyle.
# SupportedStyles: always, always_true, never
Style/FrozenStringLiteralComment:
Expand Down Expand Up @@ -464,7 +464,7 @@ def f
- 'example1.rb'
# Offense count: 1
# This cop supports safe autocorrection (--autocorrect).
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: EnforcedStyle.
# SupportedStyles: always, always_true, never
Style/FrozenStringLiteralComment:
Expand Down Expand Up @@ -500,7 +500,7 @@ def fooBar; end
EnforcedStyle: camelCase
# Offense count: 1
# This cop supports safe autocorrection (--autocorrect).
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: EnforcedStyle.
# SupportedStyles: always, always_true, never
Style/FrozenStringLiteralComment:
Expand Down Expand Up @@ -570,7 +570,7 @@ def foo; end
todo_contents = File.read('.rubocop_todo.yml').lines[8..].join
expect(todo_contents).to eq(<<~YAML)
# Offense count: 1
# This cop supports safe autocorrection (--autocorrect).
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: EnforcedStyle.
# SupportedStyles: always, always_true, never
Style/FrozenStringLiteralComment:
Expand Down Expand Up @@ -608,7 +608,7 @@ def foo; end
todo_contents = File.read('dir/.rubocop_todo.yml').lines[8..].join
expect(todo_contents).to eq(<<~YAML)
# Offense count: 1
# This cop supports safe autocorrection (--autocorrect).
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: EnforcedStyle.
# SupportedStyles: always, always_true, never
Style/FrozenStringLiteralComment:
Expand Down Expand Up @@ -1216,14 +1216,14 @@ def function(arg1, arg2, arg3, arg4, arg5, arg6, arg7)
expect(File.readlines('.rubocop_todo.yml')[8..].join)
.to eq(<<~YAML)
# Offense count: 3
# This cop supports safe autocorrection (--autocorrect).
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: EnforcedStyle.
# SupportedStyles: always, always_true, never
Style/FrozenStringLiteralComment:
Enabled: false
# Offense count: 2
# This cop supports safe autocorrection (--autocorrect).
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: RequireEnglish, EnforcedStyle.
# SupportedStyles: use_perl_names, use_english_names, use_builtin_english_names
Style/SpecialGlobalVars:
Expand All @@ -1237,7 +1237,7 @@ def function(arg1, arg2, arg3, arg4, arg5, arg6, arg7)
expect(File.readlines('.rubocop_todo.yml')[8..].join)
.to eq(<<~YAML)
# Offense count: 4
# This cop supports safe autocorrection (--autocorrect).
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: EnforcedStyle.
# SupportedStyles: always, always_true, never
Style/FrozenStringLiteralComment:
Expand All @@ -1248,7 +1248,7 @@ def function(arg1, arg2, arg3, arg4, arg5, arg6, arg7)
- 'example4.rb'
# Offense count: 3
# This cop supports safe autocorrection (--autocorrect).
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: RequireEnglish, EnforcedStyle.
# SupportedStyles: use_perl_names, use_english_names, use_builtin_english_names
Style/SpecialGlobalVars:
Expand Down

0 comments on commit c40b99e

Please sign in to comment.