Class: RuboCop::Cop::MultilineLiteralBraceCorrector
| Relationships & Source Files | |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Instance Chain:
|
|
| Inherits: | Object |
| 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
ConfigurableEnforcedStyle - Included
RangeHelp - Included
Class Method Summary
Instance Attribute Summary
- #corrector readonly private
- #nested_closing_bracket_moved_before_comment? ⇒ Boolean readonly private
- #node readonly private
- #processed_source readonly private
ConfigurableEnforcedStyle - Included
Instance Method Summary
- #call
- #closing_bracket_moved_before_comment?(literal) ⇒ Boolean private
- #closing_bracket_on_own_line?(descendant, line) ⇒ Boolean private
- #comment_before_closing_brace?(literal, last_element) ⇒ Boolean private
- #content_if_comment_present(corrector, node) private
- #correct_heredoc_argument_method_chain(corrector, end_range) private
- #correct_next_line_brace(corrector, end_range) private
- #correct_same_line_brace(corrector) private
- #last_element_range_with_trailing_comma(node) private
- #last_element_trailing_comma_range(node) private
- #nested_closing_bracket_to_move?(descendant, line) ⇒ Boolean private
- #remove_trailing_content_of_comment(corrector, range) private
- #same_kind_as_node?(descendant) ⇒ Boolean private
- #select_content_to_be_inserted_after_last_element(corrector, node) private
- #use_heredoc_argument_method_chain?(parent) ⇒ Boolean private
RangeHelp - Included
| #add_range, | |
| #arguments_range | A range containing the first to the last argument of a method call or method definition. |
| #column_offset_between, | |
| #contents_range | A range containing only the contents of a literal with delimiters (e.g. |
| #directions, | |
| #effective_column | Returns the column attribute of the range, except if the range is on the first line and there’s a byte order mark at the beginning of that line, in which case 1 is subtracted from the column value. |
| #final_pos | rubocop:disable-next Metrics/ParameterLists. |
| #move_pos, #move_pos_str, #range_between, #range_by_whole_lines, #range_with_comments, #range_with_comments_and_lines, #range_with_surrounding_comma, #range_with_surrounding_space, #source_range | |
MultilineLiteralBraceLayout - Included
| #check, #check_brace_layout, #check_new_line, #check_same_line, #check_symmetrical, #children, | |
| #closing_brace_on_same_line? | This method depends on the fact that we have guarded against implicit and empty literals. |
| #empty_literal?, #heredoc_opened_on_closing_brace_line?, #ignored_literal?, #implicit_literal?, | |
| #last_line_heredoc? | 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. |
| #new_line_needed_before_closing_brace? | Returns true for the case. |
| #opening_brace_on_same_line? | This method depends on the fact that we have guarded against implicit and empty literals. |
ConfigurableEnforcedStyle - Included
Constructor Details
.new(corrector, node, processed_source) ⇒ MultilineLiteralBraceCorrector
# File 'lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb', line 15
def initialize(corrector, node, processed_source) @corrector = corrector @node = node @processed_source = processed_source end
Class Method Details
.correct(corrector, node, processed_source)
[ GitHub ]# File 'lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb', line 11
def self.correct(corrector, node, processed_source) new(corrector, node, processed_source).call end
Instance Attribute Details
#corrector (readonly, private)
[ GitHub ]# File 'lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb', line 41
attr_reader :corrector, :node, :processed_source
#nested_closing_bracket_moved_before_comment? ⇒ Boolean (readonly, private)
[ GitHub ]
# File 'lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb', line 47
def nested_closing_bracket_moved_before_comment? last_line = children(node).last.last_line node.each_descendant.any? { |nested| nested_closing_bracket_to_move?(nested, last_line) } end
#node (readonly, private)
[ GitHub ]# File 'lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb', line 41
attr_reader :corrector, :node, :processed_source
#processed_source (readonly, private)
[ GitHub ]Instance Method Details
#call
[ GitHub ]# File 'lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb', line 21
def call if closing_brace_on_same_line?(node) correct_same_line_brace(corrector) 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) return if nested_closing_bracket_moved_before_comment? end_range = last_element_range_with_trailing_comma(node).end correct_next_line_brace(corrector, end_range) correct_heredoc_argument_method_chain(corrector, end_range) end end
#closing_bracket_moved_before_comment?(literal) ⇒ Boolean (private)
# File 'lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb', line 73
def closing_bracket_moved_before_comment?(literal) last_element = literal.call_type? ? literal.last_argument : literal.children.last return false if last_element.nil? return false if new_line_needed_before_closing_brace?(literal) comment_before_closing_brace?(literal, last_element) end
#closing_bracket_on_own_line?(descendant, line) ⇒ Boolean (private)
# File 'lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb', line 64
def closing_bracket_on_own_line?(descendant, line) return false unless descendant.loc?(:end) && descendant.loc.end.is?('}', ']', ')') closing_bracket = descendant.loc.end return false if closing_bracket.line != line closing_bracket.source_line[0...closing_bracket.column].strip.empty? end
#comment_before_closing_brace?(literal, last_element) ⇒ Boolean (private)
# File 'lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb', line 104
def comment_before_closing_brace?(literal, last_element) lines_before_closing_brace = last_element.last_line...literal.loc.end.line processed_source.each_comment_in_lines(lines_before_closing_brace).any? end
#content_if_comment_present(corrector, node) (private)
[ GitHub ]# File 'lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb', line 96
def content_if_comment_present(corrector, node) if comment_before_closing_brace?(node, children(node).last) select_content_to_be_inserted_after_last_element(corrector, node) else node.loc.end.source end end
#correct_heredoc_argument_method_chain(corrector, end_range) (private)
[ GitHub ]# File 'lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb', line 86
def correct_heredoc_argument_method_chain(corrector, end_range) return unless (parent = node.parent) return unless use_heredoc_argument_method_chain?(parent) chained_method = range_between(parent.loc.dot.begin_pos, parent.source_range.end_pos) corrector.remove(chained_method) corrector.insert_after(end_range, chained_method.source) end
#correct_next_line_brace(corrector, end_range) (private)
[ GitHub ]#correct_same_line_brace(corrector) (private)
[ GitHub ]#last_element_range_with_trailing_comma(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb', line 131
def last_element_range_with_trailing_comma(node) trailing_comma_range = last_element_trailing_comma_range(node) if trailing_comma_range children(node).last.source_range.join(trailing_comma_range) else children(node).last.source_range end end
#last_element_trailing_comma_range(node) (private)
[ GitHub ]
#nested_closing_bracket_to_move?(descendant, line) ⇒ Boolean (private)
# File 'lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb', line 53
def nested_closing_bracket_to_move?(descendant, line) return false unless same_kind_as_node?(descendant) return false unless closing_bracket_on_own_line?(descendant, line) closing_bracket_moved_before_comment?(descendant) end
#remove_trailing_content_of_comment(corrector, range) (private)
[ GitHub ]
#same_kind_as_node?(descendant) ⇒ Boolean (private)
#select_content_to_be_inserted_after_last_element(corrector, node) (private)
[ GitHub ]# File 'lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb', line 117
def select_content_to_be_inserted_after_last_element(corrector, node) range = range_between( node.loc.end.begin_pos, range_by_whole_lines(node.source_range).end.end_pos ) remove_trailing_content_of_comment(corrector, range) range.source end
#use_heredoc_argument_method_chain?(parent) ⇒ Boolean (private)
# File 'lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb', line 110
def use_heredoc_argument_method_chain?(parent) return false unless node.respond_to?(:first_argument) return false unless (first_argument = node.first_argument) parent.call_type? && first_argument.str_type? && first_argument.heredoc? end