Skip to content

Commit

Permalink
[Fix rubocop#7971] Fix --disable-uncorrectable not updating uncorrect…
Browse files Browse the repository at this point in the history
…ed code
  • Loading branch information
rrosenblum committed May 29, 2020
1 parent 6ee3b09 commit 800d653
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Expand Up @@ -13,7 +13,7 @@ InternalAffairs/NodeDestructuring:
# Offense count: 48
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 186
Max: 190

# Offense count: 198
# Configuration parameters: CountComments, ExcludedMethods.
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,7 @@
* [#8017](https://github.com/rubocop-hq/rubocop/pull/8017): Fix a false positive for `Lint/SuppressedException` when empty rescue with comment in `def`. ([@koic][])
* [#7990](https://github.com/rubocop-hq/rubocop/issues/7990): Fix resolving `inherit_gem` in remote configs. ([@CvX][])
* [#8035](https://github.com/rubocop-hq/rubocop/issues/8035): Fix a false positive for `Lint/DeprecatedOpenSSLConstant` when using double quoted string argument. ([@koic][])
* [#7971](https://github.com/rubocop-hq/rubocop/issues/7971): Fix an issue where `--disable-uncorrectable` would not update uncorrected code with `rubocop:todo`. ([@rrosenblum][])

## 0.84.0 (2020-05-21)

Expand Down
15 changes: 11 additions & 4 deletions lib/rubocop/cop/cop.rb
Expand Up @@ -146,17 +146,24 @@ def duplicate_location?(location)
@offenses.any? { |o| o.location == location }
end

def correct(node)
def correct(node) # rubocop:disable Metrics/PerceivedComplexity, Metrics/MethodLength
reason = reason_to_not_correct(node)
return reason if reason

@corrected_nodes[node] = true

if support_autocorrect?
correction = autocorrect(node)
return :uncorrected unless correction

@corrections << Correction.new(correction, node, self)
:corrected
if correction
@corrections << Correction.new(correction, node, self)
:corrected
elsif disable_uncorrectable?
disable_uncorrectable(node)
:corrected_with_todo
else
:uncorrected
end
elsif disable_uncorrectable?
disable_uncorrectable(node)
:corrected_with_todo
Expand Down
36 changes: 36 additions & 0 deletions spec/rubocop/cli/cli_disable_uncorrectable_spec.rb
Expand Up @@ -64,6 +64,42 @@
RUBY
end

it 'adds it when the cop supports autocorrect but does not correct the offense' do
create_file('example.rb', <<~RUBY)
def ordinary_method(some_arg)
puts 'Ignoring args'
end
def method_with_keyword_arg(some_keyword_arg:)
puts 'Ignoring args'
end
RUBY

expect(exit_code).to eq(0)
expect($stderr.string).to eq('')
expect($stdout.string).to eq(<<~OUTPUT)
== example.rb ==
C: 1: 1: [Corrected] Style/FrozenStringLiteralComment: Missing frozen string literal comment.
W: 1: 21: [Corrected] Lint/UnusedMethodArgument: Unused method argument - some_arg. If it's necessary, use _ or _some_arg as an argument name to indicate that it won't be used. You can also write as ordinary_method(*) if you want the method to accept any arguments but don't care about them.
C: 2: 1: [Corrected] Layout/EmptyLineAfterMagicComment: Add an empty line after magic comments.
W: 5: 29: [Todo] Lint/UnusedMethodArgument: Unused method argument - some_keyword_arg. You can also write as method_with_keyword_arg(*) if you want the method to accept any arguments but don't care about them.
1 file inspected, 4 offenses detected, 4 offenses corrected
OUTPUT

expect(IO.read('example.rb')).to eq(<<~RUBY)
# frozen_string_literal: true
def ordinary_method(_some_arg)
puts 'Ignoring args'
end
def method_with_keyword_arg(some_keyword_arg:) # rubocop:todo Lint/UnusedMethodArgument
puts 'Ignoring args'
end
RUBY
end

context 'and there are two offenses of the same kind on one line' do
it 'adds a single one-line disable statement' do
create_file('.rubocop.yml', <<~YAML)
Expand Down

0 comments on commit 800d653

Please sign in to comment.