Skip to content

Commit

Permalink
[Fix #5980] Implement safe and safe-auto-correct options
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim Krizhanovsky committed Sep 28, 2018
1 parent 073acc2 commit efb16af
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### New features

* [#5980](https://github.com/rubocop-hq/rubocop/issues/5980): Add --safe and --safe-auto-correct options. ([@Darhazer][])

### Bug fixes

* [#6330](https://github.com/rubocop-hq/rubocop/issues/6330): Fix an error for `Rails/ReversibleMigration` when using variable assignment. ([@koic][], [@scottmatthewman][])
Expand Down
4 changes: 2 additions & 2 deletions config/default.yml
Expand Up @@ -2152,7 +2152,7 @@ Performance/TimesMap:
Enabled: true
VersionAdded: 0.36
VersionChanged: 0.50
SafeAutocorrect: false # see https://github.com/rubocop-hq/rubocop/issues/4658
SafeAutoCorrect: false # see https://github.com/rubocop-hq/rubocop/issues/4658

Performance/UnfreezeString:
Description: 'Use unary plus to get an unfrozen string literal.'
Expand Down Expand Up @@ -3640,7 +3640,7 @@ Style/NumericPredicate:
# This will change to a new method call which isn't guaranteed to be on the
# object. Switching these methods has to be done with knowledge of the types
# of the variables which rubocop doesn't have.
SafeAutocorrect: false
SafeAutoCorrect: false
AutoCorrect: false
Enabled: true
VersionAdded: 0.42
Expand Down
8 changes: 7 additions & 1 deletion lib/rubocop/cop/autocorrect_logic.rb
Expand Up @@ -20,7 +20,13 @@ def autocorrect_enabled?
# allow turning off autocorrect on a cop by cop basis
return true unless cop_config

cop_config['AutoCorrect'] != false
return false if cop_config['AutoCorrect'] == false

if @options.fetch(:safe_auto_correct, false)
return cop_config.fetch('SafeAutoCorrect', true)
end

true
end
end
end
Expand Down
13 changes: 11 additions & 2 deletions lib/rubocop/cop/registry.rb
Expand Up @@ -117,9 +117,18 @@ def length
@registry.size
end

def enabled(config, only)
def enabled(config, only, only_safe = false)
select do |cop|
config.for_cop(cop).fetch('Enabled') || only.include?(cop.cop_name)
only.include?(cop.cop_name) || enabled?(cop, config, only_safe)
end
end

def enabled?(cop, config, only_safe)
cfg = config.for_cop(cop)
if only_safe
cfg.fetch('Enabled') && cfg.fetch('Safe', true)
else
cfg.fetch('Enabled')
end
end

Expand Down
5 changes: 3 additions & 2 deletions lib/rubocop/cop/team.rb
Expand Up @@ -45,8 +45,9 @@ def inspect_file(processed_source)
end

def cops
only_options = @options.fetch(:only, [])
@cops ||= @cop_classes.enabled(@config, only_options).map do |cop_class|
only = @options.fetch(:only, [])
safe = @options.fetch(:safe, false)
@cops ||= @cop_classes.enabled(@config, only, safe).map do |cop_class|
cop_class.new(@config, @options)
end
end
Expand Down
7 changes: 7 additions & 0 deletions lib/rubocop/options.rb
Expand Up @@ -156,6 +156,8 @@ def add_boolean_flags(opts)
option(opts, '-R', '--rails')
option(opts, '-a', '--auto-correct')

option(opts, '--safe')

option(opts, '--[no-]color')

option(opts, '-v', '--version')
Expand All @@ -173,6 +175,9 @@ def add_aliases(opts)
@options[:only] << 'Layout'
@options[:auto_correct] = true
end
option(opts, '--safe-auto-correct') do
@options[:auto_correct] = true
end
end

def add_list_options(opts)
Expand Down Expand Up @@ -410,8 +415,10 @@ module OptionsHelp
extra_details: 'Display extra details in offense messages.',
rails: 'Run extra Rails cops.',
lint: 'Run only lint cops.',
safe: 'Run only safe cops.',
list_target_files: 'List all files RuboCop will inspect.',
auto_correct: 'Auto-correct offenses.',
safe_auto_correct: 'Run auto-correct only when it\'s safe.',
fix_layout: 'Run only layout cops, with auto-correct on.',
color: 'Force color output on or off.',
version: 'Display version.',
Expand Down
3 changes: 1 addition & 2 deletions manual/cops_performance.md
Expand Up @@ -857,7 +857,7 @@ This cop identifies places where `gsub` can be replaced by

Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
--- | --- | --- | --- | ---
Enabled | Yes | Yes | 0.36 | 0.5
Enabled | Yes | Yes (Unsafe) | 0.36 | 0.5

This cop checks for .times.map calls.
In most cases such calls can be replaced
Expand All @@ -882,7 +882,6 @@ end
Name | Default value | Configurable values
--- | --- | ---
AutoCorrect | `false` | Boolean
SafeAutocorrect | `false` | Boolean

## Performance/UnfreezeString

Expand Down
3 changes: 1 addition & 2 deletions manual/cops_style.md
Expand Up @@ -4005,7 +4005,7 @@ Strict | `false` | Boolean

Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
--- | --- | --- | --- | ---
Enabled | No | Yes | 0.42 | 0.59
Enabled | No | Yes (Unsafe) | 0.42 | 0.59

This cop checks for usage of comparison operators (`==`,
`>`, `<`) to test numbers as zero, positive, or negative.
Expand Down Expand Up @@ -4057,7 +4057,6 @@ bar.baz > 0

Name | Default value | Configurable values
--- | --- | ---
SafeAutocorrect | `false` | Boolean
AutoCorrect | `false` | Boolean
EnforcedStyle | `predicate` | `predicate`, `comparison`
IgnoredMethods | `[]` | Array
Expand Down
10 changes: 9 additions & 1 deletion spec/rubocop/cop/registry_spec.rb
Expand Up @@ -138,7 +138,10 @@ class Foo < Cop

context '#enabled' do
let(:config) do
RuboCop::Config.new('Test/IndentArray' => { 'Enabled' => false })
RuboCop::Config.new(
'Test/IndentArray' => { 'Enabled' => false },
'RSpec/Foo' => { 'Safe' => false }
)
end

it 'selects cops which are enabled in the config' do
Expand All @@ -148,6 +151,11 @@ class Foo < Cop
it 'overrides config if :only includes the cop' do
expect(registry.enabled(config, ['Test/IndentArray'])).to eql(cops)
end

it 'selects only safe cops if :safe passed' do
enabled_cops = registry.enabled(config, [], true)
expect(enabled_cops).not_to include(RuboCop::Cop::RSpec::Foo)
end
end

it 'exposes a list of cop names' do
Expand Down
2 changes: 2 additions & 0 deletions spec/rubocop/options_spec.rb
Expand Up @@ -101,13 +101,15 @@ def abs(path)
-S, --display-style-guide Display style guide URLs in offense messages.
-R, --rails Run extra Rails cops.
-a, --auto-correct Auto-correct offenses.
--safe Run only safe cops.
--[no-]color Force color output on or off.
-v, --version Display version.
-V, --verbose-version Display verbose version.
-P, --parallel Use available CPUs to execute inspection in
parallel.
-l, --lint Run only lint cops.
-x, --fix-layout Run only layout cops, with auto-correct on.
--safe-auto-correct Run auto-correct only when it's safe.
-s, --stdin FILE Pipe source from STDIN, using FILE in offense
reports. This is useful for editor integration.
OUTPUT
Expand Down

0 comments on commit efb16af

Please sign in to comment.