Module: RuboCop::Cop::MultilineLiteralBraceLayout

Overview

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.

Instance Method Summary collapse

Methods included from ConfigurableEnforcedStyle

#alternative_style, #ambiguous_style_detected, #correct_style_detected, #detected_style, #detected_style=, #no_acceptable_style!, #no_acceptable_style?, #opposite_style_detected, #parameter_name, #style, #style_detected, #supported_styles, #unexpected_style_detected

Instance Method Details

#autocorrect(node) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb', line 27

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(last_element_range_with_trailing_comma(node),
                             node.loc.end.source)
    end
  end
end

#check_brace_layout(node) ⇒ Object



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

def check_brace_layout(node)
  return unless node.loc.begin # Ignore implicit literals.
  return if children(node).empty? # Ignore empty literals.
  return if node.single_line? # Ignore single-line 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