Skip to content

Commit

Permalink
[Fixes rubocop#8156] CLI options change: auto-correct now safe by def…
Browse files Browse the repository at this point in the history
…ault.

`-a` / `--auto-correct` are now safe only, replacing `--safe-auto-correct`
`-A` / `--auto-correct-all` apply safe and unsafe corrections
  • Loading branch information
marcandre committed Jun 23, 2020
1 parent a8d432b commit ec6f510
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 81 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,7 @@
### Changes

* [#7868](https://github.com/rubocop-hq/rubocop/pull/7868): **(Breaking)** Extensive refactoring of internal classes `Team`, `Commissioner`, `Corrector`. `Cop::Cop#corrections` not completely compatible. See Upgrade Notes. ([@marcandre][])
* [#8156](https://github.com/rubocop-hq/rubocop/issues/8156): **(Breaking)** `rubocop -a / --autocorrect` no longer run unsafe corrections; `rubocop -A / --autocorrect-all` run both safe and unsafe corrections. Options `--safe-autocorrect` is deprecated. ([@marcandre][])

### Bug fixes

Expand Down
12 changes: 8 additions & 4 deletions docs/modules/ROOT/pages/usage/auto_correct.adoc
Expand Up @@ -4,7 +4,9 @@ In auto-correct mode, RuboCop will try to automatically fix offenses:

[source,sh]
----
$ rubocop -a
$ rubocop -A
# or
$ rubocop --auto-correct-all
----

There are a couple of things to keep in mind about auto-correct:
Expand All @@ -21,7 +23,9 @@ TIP: You should always run your test suite after using the auto-correct function

[source,sh]
----
$ rubocop --safe-auto-correct
$ rubocop -a
# or
$ rubocop --auto-correct
----

In RuboCop 0.60, we began to annotate cops as `Safe` or not safe. The definition of
Expand All @@ -36,7 +40,7 @@ design) or not.
does is safe (equivalent) by design. If a cop is unsafe its auto-correct automatically
becomes unsafe as well.

If a cop or its auto-correct is annotated as "not safe", it will be omitted when using `--safe-auto-correct`.
If a cop or its auto-correct is annotated as "not safe", it will be omitted when using `--auto-correct`.

NOTE: Currently there might still be cops that aren't marked as unsafe or
with unsafe auto-correct. Eventually, the safety of each cop will be specified
Expand Down Expand Up @@ -80,7 +84,7 @@ or

[source,sh]
----
$ rubocop --safe-auto-correct --disable-uncorrectable
$ rubocop --auto-correct-all --disable-uncorrectable
----

You can add the flag `--disable-uncorrectable`, which will generate
Expand Down
23 changes: 15 additions & 8 deletions lib/rubocop/options.rb
Expand Up @@ -163,15 +163,24 @@ def add_flags_with_optional_args(opts)
end
end

# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/MethodLength, Metrics/AbcSize
def add_boolean_flags(opts)
option(opts, '-F', '--fail-fast')
option(opts, '-C', '--cache FLAG')
option(opts, '-d', '--debug')
option(opts, '-D', '--[no-]display-cop-names')
option(opts, '-E', '--extra-details')
option(opts, '-S', '--display-style-guide')
option(opts, '-a', '--auto-correct')
option(opts, '-a', '--auto-correct') do
@options[:safe_auto_correct] = true
end
option(opts, '--safe-autocorrect') do
warn '--safe-autocorrect is deprecated; use --autocorrect'
@options[:safe_auto_correct] = @options[:auto_correct] = true
end
option(opts, '-A', '--auto-correct-all') do
@options[:auto_correct] = true
end
option(opts, '--disable-pending-cops')
option(opts, '--enable-pending-cops')
option(opts, '--ignore-disable-comments')
Expand All @@ -184,7 +193,7 @@ def add_boolean_flags(opts)
option(opts, '-V', '--verbose-version')
option(opts, '-P', '--parallel')
end
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize

def add_aliases(opts)
option(opts, '-l', '--lint') do
Expand All @@ -196,9 +205,6 @@ 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 @@ -465,8 +471,9 @@ module OptionsHelp
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.',
auto_correct: 'Auto-correct offenses (only when it\'s safe).',
safe_autocorrect: '(same, deprecated)',
auto_correct_all: 'Auto-correct offenses (safe and unsafe)',
fix_layout: 'Run only layout cops, with auto-correct on.',
color: 'Force color output on or off.',
version: 'Display version.',
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/rake_task.rb
Expand Up @@ -69,7 +69,7 @@ def setup_subtasks(name, *args, &task_block)
task(:auto_correct, *args) do |_, task_args|
RakeFileUtils.verbose(verbose) do
yield(*[self, task_args].slice(0, task_block.arity)) if block_given?
options = full_options.unshift('--auto-correct')
options = full_options.unshift('--auto-correct-all')
options.delete('--parallel')
run_cli(verbose, options)
end
Expand Down

0 comments on commit ec6f510

Please sign in to comment.