module RuboCop::Cop::MultilineLiteralBraceLayout

Common functionality for checking the closing brace of a literal is either on the same line as the last contained elements, or a new line.

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb, line 25
def autocorrect(node)
  if closing_brace_on_same_line?(node)
    lambda do |corrector|
      corrector.insert_before(node.loc.end, "\n".freeze)
    end
  else
    lambda do |corrector|
      corrector.remove(range_with_surrounding_space(node.loc.end,
                                                    :left))
      corrector.insert_after(children(node).last.source_range,
                             node.loc.end.source)
    end
  end
end
check_brace_layout(node) click to toggle source
# File lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb, line 10
def check_brace_layout(node)
  return unless node.loc.begin # Ignore implicit literals.
  return if children(node).empty? # Ignore empty literals.

  # If the last node is or contains a conflicting HEREDOC, we don't want
  # to adjust the brace layout because this will result in invalid code.
  return if last_line_heredoc?(children(node).last)

  case style
  when :symmetrical then handle_symmetrical(node)
  when :new_line then handle_new_line(node)
  when :same_line then handle_same_line(node)
  end
end

Private Instance Methods

children(node) click to toggle source
# File lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb, line 66
def children(node)
  node.children
end
closing_brace_on_same_line?(node) click to toggle source

This method depends on the fact that we have guarded against implicit and empty literals.

# File lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb, line 78
def closing_brace_on_same_line?(node)
  node.loc.end.line == children(node).last.loc.last_line
end
handle_new_line(node) click to toggle source
# File lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb, line 42
def handle_new_line(node)
  return unless closing_brace_on_same_line?(node)

  add_offense(node, :expression, self.class::ALWAYS_NEW_LINE_MESSAGE)
end
handle_same_line(node) click to toggle source
# File lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb, line 48
def handle_same_line(node)
  return if closing_brace_on_same_line?(node)

  add_offense(node, :expression, self.class::ALWAYS_SAME_LINE_MESSAGE)
end
handle_symmetrical(node) click to toggle source
# File lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb, line 54
def handle_symmetrical(node)
  if opening_brace_on_same_line?(node)
    return if closing_brace_on_same_line?(node)

    add_offense(node, :expression, self.class::SAME_LINE_MESSAGE)
  else
    return unless closing_brace_on_same_line?(node)

    add_offense(node, :expression, self.class::NEW_LINE_MESSAGE)
  end
end
last_line_heredoc?(node, parent = nil) click to toggle source

Starting with the parent node and recursively for the parent node's children, check if the node is a HEREDOC and if the HEREDOC ends below or on the last line of the parent node.

Example:

# node is `b: ...` parameter
# last_line_heredoc?(node) => false
foo(a,
  b: {
    a: 1,
    c: <<-EOM
      baz
    EOM
  }
)

# node is `b: ...` parameter
# last_line_heredoc?(node) => true
foo(a,
  b: <<-EOM
    baz
  EOM
)
# File lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb, line 106
def last_line_heredoc?(node, parent = nil)
  parent ||= node

  return false unless node.respond_to?(:loc)

  if node.loc.respond_to?(:heredoc_end) &&
     node.loc.heredoc_end.last_line >= parent.loc.last_line
    return true
  end

  return false unless node.respond_to?(:children)
  return false if node.children.empty?

  node.children.any? { |child| last_line_heredoc?(child, parent) }
end
opening_brace_on_same_line?(node) click to toggle source

This method depends on the fact that we have guarded against implicit and empty literals.

# File lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb, line 72
def opening_brace_on_same_line?(node)
  node.loc.begin.line == children(node).first.loc.first_line
end