class RuboCop::Cop::Style::MultilineMethodSignature

Checks for method signatures that span multiple lines.

@example

# good

def foo(bar, baz)
end

# bad

def foo(bar,
        baz)
end

Constants

MSG

Public Instance Methods

on_def(node) click to toggle source
# File lib/rubocop/cop/style/multiline_method_signature.rb, line 27
def on_def(node)
  return unless node.arguments?
  return if opening_line(node) == closing_line(node)
  return if correction_exceeds_max_line_length?(node)
  return unless (begin_of_arguments = node.arguments.loc.begin)

  add_offense(node) do |corrector|
    autocorrect(corrector, node, begin_of_arguments)
  end
end
Also aliased as: on_defs
on_defs(node)
Alias for: on_def

Private Instance Methods

arguments_range(node) click to toggle source
# File lib/rubocop/cop/style/multiline_method_signature.rb, line 69
def arguments_range(node)
  range = range_between(
    node.first_argument.source_range.begin_pos, node.last_argument.source_range.end_pos
  )

  range_with_surrounding_space(range, side: :left)
end
autocorrect(corrector, node, begin_of_arguments) click to toggle source

rubocop:disable Metrics/AbcSize

# File lib/rubocop/cop/style/multiline_method_signature.rb, line 42
def autocorrect(corrector, node, begin_of_arguments)
  arguments = node.arguments
  joined_arguments = arguments.map(&:source).join(', ')
  last_line_source_of_arguments = last_line_source_of_arguments(arguments)

  if last_line_source_of_arguments.start_with?(')')
    joined_arguments = "#{joined_arguments}#{last_line_source_of_arguments}"

    corrector.remove(range_by_whole_lines(arguments.loc.end, include_final_newline: true))
  end

  arguments_range = arguments_range(node)
  # If the method name isn't on the same line as def, move it directly after def
  if arguments_range.first_line != opening_line(node)
    corrector.remove(node.loc.name)
    corrector.insert_after(node.loc.keyword, " #{node.loc.name.source}")
  end

  corrector.remove(arguments_range)
  corrector.insert_after(begin_of_arguments, joined_arguments)
end
closing_line(node) click to toggle source
# File lib/rubocop/cop/style/multiline_method_signature.rb, line 81
def closing_line(node)
  node.arguments.last_line
end
correction_exceeds_max_line_length?(node) click to toggle source
# File lib/rubocop/cop/style/multiline_method_signature.rb, line 85
def correction_exceeds_max_line_length?(node)
  indentation_width(node) + definition_width(node) > max_line_length
end
definition_width(node) click to toggle source
# File lib/rubocop/cop/style/multiline_method_signature.rb, line 93
def definition_width(node)
  node.source_range.begin.join(node.arguments.source_range.end).length
end
indentation_width(node) click to toggle source
# File lib/rubocop/cop/style/multiline_method_signature.rb, line 89
def indentation_width(node)
  processed_source.line_indentation(node.source_range.line)
end
last_line_source_of_arguments(arguments) click to toggle source

rubocop:enable Metrics/AbcSize

# File lib/rubocop/cop/style/multiline_method_signature.rb, line 65
def last_line_source_of_arguments(arguments)
  processed_source[arguments.last_line - 1].strip
end
max_line_length() click to toggle source
# File lib/rubocop/cop/style/multiline_method_signature.rb, line 97
def max_line_length
  config.for_cop('Layout/LineLength')['Max'] || 120
end
opening_line(node) click to toggle source
# File lib/rubocop/cop/style/multiline_method_signature.rb, line 77
def opening_line(node)
  node.first_line
end