Module: RuboCop::Cop::MultilineLiteralBraceLayout

Overview

Common functionality for checking that the closing brace of a literal is symmetrical with respect to the opening brace and contained elements.

Instance Method Summary collapse

Instance Method Details

#autocorrect(node) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb', line 24

def autocorrect(node)
  if closing_brace_on_same_line?(node)
    lambda do |corrector|
      corrector.insert_before(node.loc.end, "\n".freeze)
    end
  else
    range = Parser::Source::Range.new(
      node.source_range.source_buffer,
      children(node).last.source_range.end_pos,
      node.loc.end.begin_pos)

    ->(corrector) { corrector.remove(range) }
  end
end

#check_brace_layout(node) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb', line 9

def check_brace_layout(node)
  return unless node.loc.begin # Ignore implicit literals.
  return if children(node).empty? # Ignore empty literals.

  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