Skip to content

Commit

Permalink
[Fix rubocop#3445] Handle properly setter methods auto-correct in Sty…
Browse files Browse the repository at this point in the history
…le/AndOr (rubocop#3454)
  • Loading branch information
mikezter authored and Neodelf committed Oct 15, 2016
1 parent a00aa0f commit d45d31b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

### Bug fixes

* [#3445](https://github.com/bbatsov/rubocop/issues/3445): Fix bad autocorrect for `Style/AndOr` cop. ([@mikezter][])
* [#3349](https://github.com/bbatsov/rubocop/issues/3349): Fix bad autocorrect for `Style/Lambda` cop. ([@metcalf][])
* [#3351](https://github.com/bbatsov/rubocop/issues/3351): Fix bad auto-correct for `Performance/RedundantMatch` cop. ([@annaswims][])
* [#3347](https://github.com/bbatsov/rubocop/issues/3347): Prevent infinite loop in `Style/TernaryParentheses` cop when used together with `Style/RedundantParentheses`. ([@drenmi][])
Expand Down Expand Up @@ -2335,3 +2336,4 @@
[@annaswims]: https://github.com/annaswims
[@soutaro]: https://github.com/soutaro
[@nicklamuro]: https://github.com/nicklamuro
[@mikezter]: https://github.com/mikezter
11 changes: 11 additions & 0 deletions lib/rubocop/cop/style/and_or.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,19 @@ def autocorrect(node)
def correct_send(node, corrector)
receiver, method_name, *args = *node
return correct_not(node, receiver, corrector) if method_name == :!
return correct_setter(node, corrector) if setter_method?(method_name)
return unless correctable_send?(node)

corrector.replace(whitespace_before_arg(node), '('.freeze)
corrector.insert_after(args.last.source_range, ')'.freeze)
end

def correct_setter(node, corrector)
receiver, _method_name, *args = *node
corrector.insert_before(receiver.source_range, '('.freeze)
corrector.insert_after(args.last.source_range, ')'.freeze)
end

# ! is a special case:
# 'x and !obj.method arg' can be auto-corrected if we
# recurse down a level and add parens to 'obj.method arg'
Expand All @@ -106,6 +113,10 @@ def correct_other(node, corrector)
corrector.insert_after(node.source_range, ')')
end

def setter_method?(method_name)
method_name.to_s.end_with?('=')
end

def correctable_send?(node)
_receiver, method_name, *args = *node
# don't clobber if we already have a starting paren
Expand Down
14 changes: 14 additions & 0 deletions spec/rubocop/cop/style/and_or_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,20 @@
end
end

context 'with obj.method = arg on left' do
it 'autocorrects "and" with && and adds parens' do
new_source = autocorrect_source(cop, 'obj.method = arg and x')
expect(new_source).to eq('(obj.method = arg) && x')
end
end

context 'with obj.method= arg on left' do
it 'autocorrects "and" with && and adds parens' do
new_source = autocorrect_source(cop, 'obj.method= arg and x')
expect(new_source).to eq('(obj.method= arg) && x')
end
end

context 'with predicate method with arg without space on right' do
it 'autocorrects "or" with || and adds parens' do
new_source = autocorrect_source(cop, 'false or 3.is_a?Integer')
Expand Down

0 comments on commit d45d31b

Please sign in to comment.