Class: RuboCop::Cop::MultilineLiteralBraceCorrector

Inherits:
Object
  • Object
show all
Extended by:
MultilineLiteralBraceLayout, Util
Defined in:
lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb

Overview

Autocorrection logic for the closing brace of a literal either on the same line as the last contained elements, or a new line.

Constant Summary

Constants included from Util

Util::ASGN_NODES, Util::BYTE_ORDER_MARK, Util::CONDITIONAL_NODES, Util::EQUALS_ASGN_NODES, Util::LITERAL_REGEX, Util::LOGICAL_OPERATOR_NODES, Util::MODIFIER_NODES, Util::OPERATOR_METHODS, Util::SHORTHAND_ASGN_NODES

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from ConfigurableEnforcedStyle

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

Methods included from Util

begins_its_line?, comment_line?, double_quotes_required?, effective_column, ends_its_line?, escape_string, first_part_of_call_chain, interpret_string_escapes, line_range, needs_escaping?, on_node, operator?, parentheses?, parenthesized_call?, precede?, range_between, range_by_whole_lines, range_with_surrounding_comma, range_with_surrounding_space, same_line?, source_range, strip_quotes, stripped_source_upto, symbol_without_quote?, to_string_literal, to_supported_styles, to_symbol_literal, within_node?

Methods included from AST::Sexp

#s

Methods included from PathUtil

absolute?, find_file_upwards, match_path?, pwd, relative_path, reset_pwd, smart_path

Class Attribute Details

.processed_sourceObject (readonly)

Returns the value of attribute processed_source.



12
13
14
# File 'lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb', line 12

def processed_source
  @processed_source
end

Class Method Details

.correct(processed_source, node) ⇒ Object

rubocop:disable Metrics/MethodLength



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb', line 15

def correct(processed_source, node)
  @processed_source = processed_source
  if closing_brace_on_same_line?(node)
    lambda do |corrector|
      corrector.insert_before(node.loc.end, "\n".freeze)
    end
  else
    # When a comment immediately before the closing brace gets in the
    # way of an easy correction, the offense is reported but not auto-
    # corrected. The user must handle the delicate decision of where to
    # put the comment.
    return if new_line_needed_before_closing_brace?(node)

    lambda do |corrector|
      corrector.remove(range_with_surrounding_space(range: node.loc.end,
                                                    side: :left))

      corrector.insert_after(
        last_element_range_with_trailing_comma(node),
        node.loc.end.source
      )
    end
  end
end