Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #7682] Fix Style/InverseMethods autofix leaving parenthesis #7732

Merged
merged 1 commit into from Feb 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@

* [#7719](https://github.com/rubocop-hq/rubocop/issues/7719): Fix `Style/NestedParenthesizedCalls` cop for newline. ([@tejasbubane][])
* [#7709](https://github.com/rubocop-hq/rubocop/issues/7709): Fix correction of `Style/RedundantCondition` when the else branch contains a range. ([@rrosenblum][])
* [#7682](https://github.com/rubocop-hq/rubocop/issues/7682): Fix `Style/InverseMethods` autofix leaving parenthesis. ([@tejasbubane][])

## 0.80.0 (2020-02-18)

Expand Down
12 changes: 8 additions & 4 deletions lib/rubocop/cop/style/inverse_methods.rb
Expand Up @@ -109,10 +109,7 @@ def correct_inverse_method(node)
corrector.remove(not_to_receiver(node, method_call))
corrector.replace(method_call.loc.selector,
inverse_methods[method].to_s)

if EQUALITY_METHODS.include?(method)
corrector.remove(end_parentheses(node, method_call))
end
remove_end_parenthesis(corrector, node, method, method_call)
end
end

Expand Down Expand Up @@ -187,6 +184,13 @@ def camel_case_constant?(node)
def dot_range(loc)
range_between(loc.dot.begin_pos, loc.expression.end_pos)
end

def remove_end_parenthesis(corrector, node, method, method_call)
return unless EQUALITY_METHODS.include?(method) ||
method_call.parent.begin_type?

corrector.remove(end_parentheses(node, method_call))
end
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/style/inverse_methods_spec.rb
Expand Up @@ -76,6 +76,12 @@ def test_method

expect(new_source).to eq('foo.any? { |f| f.even? }')
end

it 'corrects inverse any? inside parens' do
new_source = autocorrect_source('!(foo.any? &:working?)')

expect(new_source).to eq('foo.none? &:working?')
end
end

shared_examples 'all variable types' do |variable|
Expand Down