From 42e9d8997a8099690c7ae813b7e9b55bb910470f Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sun, 18 Apr 2021 02:18:20 +0900 Subject: [PATCH] Fix an error for `Style/SingleLineMethods` This PR fixes the following error for `Style/SingleLineMethods`. ```console % cat example.rb def some_method; 42 end % bundle exec rubocop --only Style/SingleLineMethods -d (snip) undefined method `arguments' for s(:int, 42):RuboCop::AST::IntNode Did you mean? argument? /Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/style/single_line_methods.rb:118:in `method_body_source' ``` This is a regression of #9704. --- lib/rubocop/cop/style/single_line_methods.rb | 2 +- spec/rubocop/cop/style/single_line_methods_spec.rb | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/rubocop/cop/style/single_line_methods.rb b/lib/rubocop/cop/style/single_line_methods.rb index 4764db44cde..41bde1fbe92 100644 --- a/lib/rubocop/cop/style/single_line_methods.rb +++ b/lib/rubocop/cop/style/single_line_methods.rb @@ -115,7 +115,7 @@ def move_comment(node, corrector) end def method_body_source(method_body) - if method_body.arguments.empty? || method_body.parenthesized? + if !method_body.send_type? || method_body.arguments.empty? || method_body.parenthesized? method_body.source else arguments_source = method_body.arguments.map(&:source).join(', ') diff --git a/spec/rubocop/cop/style/single_line_methods_spec.rb b/spec/rubocop/cop/style/single_line_methods_spec.rb index 0ad0502cdf4..2802bb8f6d8 100644 --- a/spec/rubocop/cop/style/single_line_methods_spec.rb +++ b/spec/rubocop/cop/style/single_line_methods_spec.rb @@ -187,6 +187,12 @@ def some_method(a, b, c) = body RUBY end + it 'corrects to an endless method definition when method body is a literal' do + expect_correction(<<~RUBY.strip, source: 'def some_method; 42 end') + def some_method() = 42 + RUBY + end + it 'corrects to an endless method definition when single line method call with parentheses' do expect_correction(<<~RUBY.strip, source: 'def index() head(:ok) end') def index() = head(:ok)