From 1f07985041ad916b1199b545187f387242ca4873 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 19 Feb 2022 04:58:22 +0900 Subject: [PATCH] Tweak the offense highlighting for `Style/DefWithParentheses` --- lib/rubocop/cop/style/def_with_parentheses.rb | 7 +++---- spec/rubocop/cop/style/def_with_parentheses_spec.rb | 11 +++++++++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/rubocop/cop/style/def_with_parentheses.rb b/lib/rubocop/cop/style/def_with_parentheses.rb index ed029662b1d..7a3c43f4830 100644 --- a/lib/rubocop/cop/style/def_with_parentheses.rb +++ b/lib/rubocop/cop/style/def_with_parentheses.rb @@ -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 diff --git a/spec/rubocop/cop/style/def_with_parentheses_spec.rb b/spec/rubocop/cop/style/def_with_parentheses_spec.rb index a9ea6e4199e..52ffe16bbc0 100644 --- a/spec/rubocop/cop/style/def_with_parentheses_spec.rb +++ b/spec/rubocop/cop/style/def_with_parentheses_spec.rb @@ -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 @@ -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 @@ -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