Skip to content

Commit

Permalink
[Fix #8514] Correct multiple Style/MethodDefParentheses per file.
Browse files Browse the repository at this point in the history
Fix the ability to correctly identify and auto-correct multiple
MethodDefParentheses violations in a single file.
  • Loading branch information
rdunlop committed Oct 1, 2020
1 parent 751edc7 commit 0570df8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,7 @@
* [#8810](https://github.com/rubocop-hq/rubocop/pull/8810): Fix multiple offense detection for `Style/RaiseArgs`. ([@pbernays][])
* [#8809](https://github.com/rubocop-hq/rubocop/pull/8809): Fix multiple offense detection for `Style/For`. ([@pbernays][])
* [#8801](https://github.com/rubocop-hq/rubocop/issues/8801): Fix `Layout/SpaceAroundEqualsInParameterDefault` only registered once in a line. ([@rdunlop][])
* [#8514](https://github.com/rubocop-hq/rubocop/issues/8514): Correct multiple `Style/MethodDefParentheses` per file. ([@rdunlop][])

### Changes

Expand Down
4 changes: 0 additions & 4 deletions lib/rubocop/cop/style/method_def_parentheses.rb
Expand Up @@ -140,16 +140,12 @@ def arguments_without_parentheses?(node)
def missing_parentheses(node)
location = node.arguments.source_range

return unless unexpected_style_detected(:require_no_parentheses)

add_offense(location, message: MSG_MISSING) do |corrector|
correct_definition(node, corrector)
end
end

def unwanted_parentheses(args)
return unless unexpected_style_detected(:require_parentheses)

add_offense(args, message: MSG_PRESENT) do |corrector|
# offense is registered on args node when parentheses are unwanted
correct_arguments(args, corrector)
Expand Down
20 changes: 20 additions & 0 deletions spec/rubocop/cop/style/method_def_parentheses_spec.rb
Expand Up @@ -65,6 +65,26 @@ def self.test(param); end
RUBY
end

it 'auto-adds required parens for a defs after a passing method' do
expect_offense(<<~RUBY)
def self.fine; end
def self.test param; end
^^^^^ Use def with parentheses when there are parameters.
def self.test2 param; end
^^^^^ Use def with parentheses when there are parameters.
RUBY

expect_correction(<<~RUBY)
def self.fine; end
def self.test(param); end
def self.test2(param); end
RUBY
end

it 'auto-adds required parens to argument lists on multiple lines' do
expect_offense(<<~RUBY)
def test one,
Expand Down

0 comments on commit 0570df8

Please sign in to comment.