module RubbyCop::Cop::FirstElementLineBreak

Common functionality for checking for a line break before the first element in a multi-line collection.

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubbycop/cop/mixin/first_element_line_break.rb, line 8
def autocorrect(node)
  ->(corrector) { corrector.insert_before(node.source_range, "\n") }
end

Private Instance Methods

check_children_line_break(node, children, start = node) click to toggle source
# File lib/rubbycop/cop/mixin/first_element_line_break.rb, line 27
def check_children_line_break(node, children, start = node)
  return if children.size < 2

  line = start.loc.line
  min = children.min_by { |n| n.loc.first_line }
  return if line != min.loc.first_line

  max = children.max_by { |n| n.loc.last_line }
  return if line == max.loc.last_line

  add_offense(min, :expression, self.class::MSG)
end
check_method_line_break(node, children) click to toggle source
# File lib/rubbycop/cop/mixin/first_element_line_break.rb, line 14
def check_method_line_break(node, children)
  return if children.empty?

  return unless method_uses_parens?(node, children.first)

  check_children_line_break(node, children)
end
method_uses_parens?(node, limit) click to toggle source
# File lib/rubbycop/cop/mixin/first_element_line_break.rb, line 22
def method_uses_parens?(node, limit)
  source = node.source_range.source_line[0...limit.loc.column]
  source =~ /\s*\(\s*$/
end