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
- #node readonly private
- #processed_source readonly private
ConfigurableEnforcedStyle
- Included
Instance Method Summary
- #call
- #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
- #remove_trailing_content_of_comment(corrector, range) private
- #select_content_to_be_inserted_after_last_element(corrector, node) private
- #use_heredoc_argument_method_chain?(parent) ⇒ Boolean private
RangeHelp
- Included
#add_range, #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, #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?, #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 40
attr_reader :corrector, :node, :processed_source
#node (readonly, private)
[ GitHub ]# File 'lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb', line 40
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) 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
#content_if_comment_present(corrector, node) (private)
[ GitHub ]# File 'lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb', line 61
def content_if_comment_present(corrector, node) range = range_with_surrounding_space( children(node).last.source_range, side: :right ).end.resize(1) if range.source == '#' 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 51
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 94
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 ]#remove_trailing_content_of_comment(corrector, range) (private)
[ GitHub ]#select_content_to_be_inserted_after_last_element(corrector, node) (private)
[ GitHub ]# File 'lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb', line 80
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 73
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