Skip to content

Commit

Permalink
Fix an error for Style/SingleLineMethods
Browse files Browse the repository at this point in the history
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 rubocop#9704.
  • Loading branch information
koic committed Apr 17, 2021
1 parent f504a4e commit 42e9d89
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/single_line_methods.rb
Expand Up @@ -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(', ')
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/style/single_line_methods_spec.rb
Expand Up @@ -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)
Expand Down

0 comments on commit 42e9d89

Please sign in to comment.