Skip to content

Commit

Permalink
Tweak the offense highlighting for Style/DefWithParentheses
Browse files Browse the repository at this point in the history
  • Loading branch information
koic committed Feb 18, 2022
1 parent 9f82e0f commit 1f07985
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
7 changes: 3 additions & 4 deletions lib/rubocop/cop/style/def_with_parentheses.rb
Expand Up @@ -40,11 +40,10 @@ class DefWithParentheses < Base

def on_def(node)
return if node.single_line?
return unless !node.arguments? && (node_arguments_loc_begin = node.arguments.loc.begin)
return unless !node.arguments? && (node_arguments = node.arguments.source_range)

add_offense(node_arguments_loc_begin) do |corrector|
corrector.remove(node_arguments_loc_begin)
corrector.remove(node.arguments.loc.end)
add_offense(node_arguments) do |corrector|
corrector.remove(node_arguments)
end
end
alias on_defs on_def
Expand Down
11 changes: 9 additions & 2 deletions spec/rubocop/cop/style/def_with_parentheses_spec.rb
Expand Up @@ -4,7 +4,7 @@
it 'reports an offense for def with empty parens' do
expect_offense(<<~RUBY)
def func()
^ Omit the parentheses in defs when the method doesn't accept any arguments.
^^ Omit the parentheses in defs when the method doesn't accept any arguments.
end
RUBY

Expand All @@ -17,7 +17,7 @@ def func
it 'reports an offense for class def with empty parens' do
expect_offense(<<~RUBY)
def Test.func()
^ Omit the parentheses in defs when the method doesn't accept any arguments.
^^ Omit the parentheses in defs when the method doesn't accept any arguments.
something
end
RUBY
Expand All @@ -36,6 +36,13 @@ def func(a)
RUBY
end

it 'accepts def without arguments' do
expect_no_offenses(<<~RUBY)
def func
end
RUBY
end

it 'accepts empty parentheses in one liners' do
expect_no_offenses("def to_s() join '/' end")
end
Expand Down

0 comments on commit 1f07985

Please sign in to comment.