Class: RuboCop::Cop::Webit::MultilineMethodCalls

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/webit/multiline_method_calls.rb

Overview

Checks for the usage of parentheses on multi-line method calls.

Examples:


# bad (multiple lines without parentheses)
puts foo,
  bar,
  baz

# good (multiple lines with parentheses)
puts(
  foo,
  bar,
  baz
)

# good (one line)
puts foo, bar, baz

# good (assignment is special method call)
obj.attr =
  if foo
    bar
  else
    baz
  end

Constant Summary collapse

MSG_PARENTHESES =
"Multiline method calls must have parentheses."

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object Also known as: on_csend



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rubocop/cop/webit/multiline_method_calls.rb', line 38

def on_send(node)
  return if node.children.empty? || !multiline_method_call?(node)
  return if node.assignment_method?
  return if node.operator_method?

  unless node.parenthesized_call?
    add_offense(node, message: MSG_PARENTHESES) do |corrector|
      corrector.replace(node, "#{node.source.sub("#{node.method_name} ", "#{node.method_name}(")})")
    end
  end
end