class RuboCop::Cop::Style::SingleLineMethods


def no_op; end
# bad
@example AllowIfMethodIsEmpty: false
def no_op; end
# good
@example AllowIfMethodIsEmpty: true (default)
def some_method() = body
def @table.columns; end
def self.resource_class=(klass); end
# good
def @table.columns; super; end
def link_to(url); {:name => url}; end
def some_method; body end
# bad
@example
methods if there is only one statement in the body.
‘allow_always`, single-line methods will be auto-corrected to endless
If `Style/EndlessMethod` is enabled with `EnforcedStyle: allow_single_line` or
Endless methods added in Ruby 3.0 are also accepted by this cop.
It will accept single-line methods with no body.
This cop checks for single-line method definitions that contain a body.

def allow_empty?

def allow_empty?
  cop_config['AllowIfMethodIsEmpty']
end

def autocorrect(corrector, node)

def autocorrect(corrector, node)
  if correct_to_endless?(node.body)
    correct_to_endless(corrector, node)
  else
    correct_to_multiline(corrector, node)
  end
end

def correct_to_endless(corrector, node)

def correct_to_endless(corrector, node)
  self_receiver = node.self_receiver? ? 'self.' : ''
  arguments = node.arguments.any? ? node.arguments.source : '()'
  replacement = "def #{self_receiver}#{node.method_name}#{arguments} = #{node.body.source}"
  corrector.replace(node, replacement)
end

def correct_to_endless?(body_node)

def correct_to_endless?(body_node)
  return false if target_ruby_version < 3.0
  endless_method_config = config.for_cop('Style/EndlessMethod')
  return false unless endless_method_config['Enabled']
  return false if endless_method_config['EnforcedStyle'] == 'disallow'
  return false unless body_node
  !(body_node.begin_type? || body_node.kwbegin_type?)
end

def correct_to_multiline(corrector, node)

def correct_to_multiline(corrector, node)
  each_part(node.body) do |part|
    LineBreakCorrector.break_line_before(
      range: part, node: node, corrector: corrector,
      configured_width: configured_indentation_width
    )
  end
  LineBreakCorrector.break_line_before(
    range: node.loc.end, node: node, corrector: corrector,
    indent_steps: 0, configured_width: configured_indentation_width
  )
  move_comment(node, corrector)
end

def each_part(body)

def each_part(body)
  return unless body
  if body.begin_type?
    body.each_child_node { |part| yield part.source_range }
  else
    yield body.source_range
  end
end

def move_comment(node, corrector)

def move_comment(node, corrector)
  LineBreakCorrector.move_comment(
    eol_comment: processed_source.comment_at_line(node.source_range.line),
    node: node, corrector: corrector
  )
end

def on_def(node)

def on_def(node)
  return unless node.single_line?
  return if node.endless?
  return if allow_empty? && !node.body
  add_offense(node) do |corrector|
    autocorrect(corrector, node)
  end
end