class RubbyCop::Cop::Style::SingleLineMethods

This cop checks for single-line method definitions. It can optionally accept single-line methods with no body.

Constants

MSG

Public Instance Methods

allow_empty?() click to toggle source
# File lib/rubbycop/cop/style/single_line_methods.rb, line 14
def allow_empty?
  cop_config['AllowIfMethodIsEmpty']
end

Private Instance Methods

autocorrect(node) click to toggle source
# File lib/rubbycop/cop/style/single_line_methods.rb, line 31
def autocorrect(node)
  body = @body

  lambda do |corrector|
    each_part(body) do |part|
      break_line_before(part, node, corrector, 1)
    end

    break_line_before(node.loc.end, node, corrector, 0)

    eol_comment = end_of_line_comment(node.source_range.line)
    move_comment(eol_comment, node, corrector) if eol_comment
  end
end
break_line_before(range, node, corrector, indent_steps) click to toggle source
# File lib/rubbycop/cop/style/single_line_methods.rb, line 60
def break_line_before(range, node, corrector, indent_steps)
  corrector.insert_before(
    range,
    "\n" + ' ' * (node.loc.keyword.column +
                  indent_steps * configured_indentation_width)
  )
end
each_part(body) { |source_range| ... } click to toggle source
# File lib/rubbycop/cop/style/single_line_methods.rb, line 50
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
end_of_line_comment(line) click to toggle source
# File lib/rubbycop/cop/style/single_line_methods.rb, line 46
def end_of_line_comment(line)
  processed_source.comments.find { |c| c.loc.line == line }
end
move_comment(eol_comment, node, corrector) click to toggle source
# File lib/rubbycop/cop/style/single_line_methods.rb, line 68
def move_comment(eol_comment, node, corrector)
  text = eol_comment.loc.expression.source
  corrector.insert_before(node.source_range,
                          text + "\n" + (' ' * node.loc.keyword.column))
  corrector.remove(eol_comment.loc.expression)
end
on_method_def(node, _method_name, _args, body) click to toggle source
# File lib/rubbycop/cop/style/single_line_methods.rb, line 20
def on_method_def(node, _method_name, _args, body)
  start_line = node.loc.keyword.line
  end_line = node.loc.end.line

  empty_body = body.nil?
  return unless start_line == end_line && !(allow_empty? && empty_body)

  @body = body
  add_offense(node, :expression)
end