Class: RuboCop::Cop::Layout::HeredocArgumentClosingParenthesis
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
::RuboCop::Cop::AutoCorrector ,
::RuboCop::Cop::Base ,
::RuboCop::ExcludeLimit ,
NodePattern::Macros,
RuboCop::AST::Sexp
|
|
Instance Chain:
self,
::RuboCop::Cop::RangeHelp ,
::RuboCop::Cop::Base ,
::RuboCop::Cop::AutocorrectLogic ,
::RuboCop::Cop::IgnoredNode ,
::RuboCop::Util ,
RuboCop::AST::Sexp
|
|
Inherits: |
RuboCop::Cop::Base
|
Defined in: | lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb |
Overview
Checks for the placement of the closing parenthesis in a method call that passes a HEREDOC string as an argument. It should be placed at the end of the line containing the opening HEREDOC tag.
Constant Summary
-
MSG =
# File 'lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb', line 57'Put the closing parenthesis for a method call with a ' \ 'HEREDOC parameter on the same line as the HEREDOC opening.'
::RuboCop::Cop::Base
- Inherited
EMPTY_OFFENSES, RESTRICT_ON_SEND
::RuboCop::Cop::RangeHelp
- Included
Class Attribute Summary
::RuboCop::Cop::AutoCorrector
- Extended
::RuboCop::Cop::Base
- Inherited
.gem_requirements, .lint?, | |
.support_autocorrect? | Returns if class supports autocorrect. |
.support_multiple_source? | Override if your cop should be called repeatedly for multiple investigations Between calls to |
Class Method Summary
::RuboCop::Cop::Base
- Inherited
.autocorrect_incompatible_with | List of cops that should not try to autocorrect at the same time as this cop. |
.badge | Naming. |
.callbacks_needed, .cop_name, .department, | |
.documentation_url | Returns a url to view this cops documentation online. |
.exclude_from_registry | Call for abstract Cop classes. |
.inherited, | |
.joining_forces | Override and return the Force class(es) you need to join. |
.match? | Returns true if the cop name or the cop namespace matches any of the given names. |
.new, | |
.requires_gem | Register a version requirement for the given gem name. |
.restrict_on_send |
::RuboCop::ExcludeLimit
- Extended
exclude_limit | Sets up a configuration option to have an exclude limit tracked. |
transform |
Instance Attribute Summary
::RuboCop::Cop::Base
- Inherited
::RuboCop::Cop::AutocorrectLogic
- Included
Instance Method Summary
- #on_send(node)
- #add_correct_closing_paren(node, corrector) private
- #add_correct_external_trailing_comma(node, corrector) private
-
#autocorrect(corrector, node)
private
Autocorrection note:
-
#end_keyword_before_closing_parenthesis?(parenthesized_send_node) ⇒ Boolean
private
Closing parenthesis helpers.
- #exist_argument_between_heredoc_end_and_closing_parentheses?(node) ⇒ Boolean private
- #external_trailing_comma?(node) ⇒ Boolean private
-
#external_trailing_comma_offset_from_loc_end(node)
private
Returns nil if no trailing external comma.
- #extract_heredoc(node) private
- #extract_heredoc_argument(node) private
- #find_most_bottom_of_heredoc_end(arguments) private
- #fix_closing_parenthesis(node, corrector) private
-
#fix_external_trailing_comma(node, corrector)
private
External trailing comma helpers.
- #heredoc_node?(node) ⇒ Boolean private
- #incorrect_parenthesis_removal_begin(node) private
- #incorrect_parenthesis_removal_end(node) private
- #internal_trailing_comma?(node) ⇒ Boolean private
-
#internal_trailing_comma_offset_from_last_arg(node)
private
Returns nil if no trailing internal comma.
- #outermost_send_on_same_line(heredoc) private
- #remove_incorrect_closing_paren(node, corrector) private
- #remove_incorrect_external_trailing_comma(node, corrector) private
-
#remove_internal_trailing_comma(node, corrector)
private
Internal trailing comma helpers.
- #safe_to_remove_line_containing_closing_paren?(node) ⇒ Boolean private
- #send_missing_closing_parens?(parent, child, heredoc) ⇒ Boolean private
- #single_line_send_with_heredoc_receiver?(node) ⇒ Boolean private
- #space?(pos) ⇒ Boolean private
- #subsequent_closing_parentheses_in_same_line?(outermost_send) ⇒ Boolean private
::RuboCop::Cop::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 |
::RuboCop::Cop::Base
- Inherited
#add_global_offense | Adds an offense that has no particular location. |
#add_offense | Adds an offense on the specified range (or node with an expression) Unless that offense is disabled for this range, a corrector will be yielded to provide the cop the opportunity to autocorrect the offense. |
#begin_investigation | Called before any investigation. |
#callbacks_needed, | |
#cop_config | Configuration Helpers. |
#cop_name, #excluded_file?, | |
#external_dependency_checksum | This method should be overridden when a cop’s behavior depends on state that lives outside of these locations: |
#inspect, | |
#message | Gets called if no message is specified when calling |
#name | Alias for Base#cop_name. |
#offenses, | |
#on_investigation_end | Called after all on_… |
#on_new_investigation | Called before all on_… |
#on_other_file | Called instead of all on_… |
#parse | There should be very limited reasons for a Cop to do it’s own parsing. |
#parser_engine, | |
#ready | Called between investigations. |
#relevant_file?, | |
#target_gem_version | Returns a gems locked versions (i.e. |
#target_rails_version, #target_ruby_version, #annotate, #apply_correction, #attempt_correction, | |
#callback_argument | Reserved for Cop::Cop. |
#complete_investigation | Called to complete an investigation. |
#correct, #current_corrector, | |
#current_offense_locations | Reserved for Commissioner: |
#current_offenses, #currently_disabled_lines, #custom_severity, #default_severity, #disable_uncorrectable, #enabled_line?, #file_name_matches_any?, #find_message, #find_severity, #range_for_original, #range_from_node_or_range, | |
#reset_investigation | Actually private methods. |
#use_corrector |
::RuboCop::Cop::AutocorrectLogic
- Included
#disable_offense, #disable_offense_at_end_of_line, #disable_offense_before_and_after, #disable_offense_with_eol_or_surround_comment, #heredoc_range, #max_line_length, #multiline_ranges, #multiline_string?, | |
#range_by_lines | Expand the given range to include all of any lines it covers. |
#range_of_first_line, #range_overlaps_offense?, #string_continuation?, #surrounding_heredoc?, #surrounding_percent_array? |
::RuboCop::Cop::IgnoredNode
- Included
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Class Method Details
.autocorrect_incompatible_with
[ GitHub ]# File 'lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb', line 60
def self.autocorrect_incompatible_with [Style::TrailingCommaInArguments] end
Instance Method Details
#add_correct_closing_paren(node, corrector) (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb', line 184
def add_correct_closing_paren(node, corrector) corrector.insert_after(node.last_argument, ')') end
#add_correct_external_trailing_comma(node, corrector) (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb', line 271
def add_correct_external_trailing_comma(node, corrector) return unless external_trailing_comma?(node) corrector.insert_after(node.last_argument, ',') end
#autocorrect(corrector, node) (private)
Autocorrection note:
Commas are a bit tricky to handle when the method call is embedded in another expression. Here’s an example:
[ first_array_value, foo(<←SQL, 123, 456, SELECT * FROM db SQL ), third_array_value, ]
The "internal" trailing comma is after 456
.
The "external" trailing comma is after )
.
To autocorrect, we remove the latter, and move the former up:
[ first_array_value, foo(<←SQL, 123, 456), SELECT * FROM db SQL third_array_value, ]
# File 'lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb', line 107
def autocorrect(corrector, node) fix_closing_parenthesis(node, corrector) remove_internal_trailing_comma(node, corrector) if internal_trailing_comma?(node) fix_external_trailing_comma(node, corrector) if external_trailing_comma?(node) end
#end_keyword_before_closing_parenthesis?(parenthesized_send_node) ⇒ Boolean
(private)
Closing parenthesis helpers.
# File 'lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb', line 162
def end_keyword_before_closing_parenthesis?(parenthesized_send_node) parenthesized_send_node.ancestors.any? do |ancestor| ancestor.loc.respond_to?(:end) && ancestor.loc.end&.source == 'end' end end
#exist_argument_between_heredoc_end_and_closing_parentheses?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb', line 222
def exist_argument_between_heredoc_end_and_closing_parentheses?(node) return true unless node.loc.end return false unless (heredoc_end = find_most_bottom_of_heredoc_end(node.arguments)) heredoc_end < node.loc.end.begin_pos && range_between(heredoc_end, node.loc.end.begin_pos).source.strip != '' end
#external_trailing_comma?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb', line 289
def external_trailing_comma?(node) !external_trailing_comma_offset_from_loc_end(node).nil? end
#external_trailing_comma_offset_from_loc_end(node) (private)
Returns nil if no trailing external comma.
# File 'lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb', line 294
def external_trailing_comma_offset_from_loc_end(node) end_pos = node.source_range.end_pos offset = 0 limit = 20 offset += 1 while offset < limit && space?(end_pos + offset) char = processed_source.buffer.source[end_pos + offset] return unless char == ',' offset + 1 # Add one to include the comma. end
#extract_heredoc(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb', line 137
def extract_heredoc(node) return node if heredoc_node?(node) return node.receiver if single_line_send_with_heredoc_receiver?(node) return unless node.hash_type? node.values.find do |v| heredoc = extract_heredoc(v) return heredoc if heredoc end end
#extract_heredoc_argument(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb', line 133
def extract_heredoc_argument(node) node.arguments.find { |arg_node| extract_heredoc(arg_node) } end
#find_most_bottom_of_heredoc_end(arguments) (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb', line 230
def find_most_bottom_of_heredoc_end(arguments) arguments.filter_map do |argument| argument.loc.heredoc_end.end_pos if argument.loc.respond_to?(:heredoc_end) end.max end
#fix_closing_parenthesis(node, corrector) (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb', line 179
def fix_closing_parenthesis(node, corrector) remove_incorrect_closing_paren(node, corrector) add_correct_closing_paren(node, corrector) end
#fix_external_trailing_comma(node, corrector) (private)
External trailing comma helpers.
# File 'lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb', line 266
def fix_external_trailing_comma(node, corrector) remove_incorrect_external_trailing_comma(node, corrector) add_correct_external_trailing_comma(node, corrector) end
#heredoc_node?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb', line 149
def heredoc_node?(node) node.respond_to?(:heredoc?) && node.heredoc? end
#incorrect_parenthesis_removal_begin(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb', line 197
def incorrect_parenthesis_removal_begin(node) end_pos = node.source_range.end_pos if safe_to_remove_line_containing_closing_paren?(node) last_line_length = node.source.scan(/\n(.*)$/).last[0].size end_pos - last_line_length - 1 # Add one for the line break itself. else end_pos - 1 # Just the `)` at the end of the string end end
#incorrect_parenthesis_removal_end(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb', line 213
def incorrect_parenthesis_removal_end(node) end_pos = node.source_range.end_pos if processed_source.buffer.source[end_pos] == ',' end_pos + 1 else end_pos end end
#internal_trailing_comma?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb', line 244
def internal_trailing_comma?(node) !internal_trailing_comma_offset_from_last_arg(node).nil? end
#internal_trailing_comma_offset_from_last_arg(node) (private)
Returns nil if no trailing internal comma.
# File 'lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb', line 249
def internal_trailing_comma_offset_from_last_arg(node) source_after_last_arg = range_between( node.children.last.source_range.end_pos, node.loc.end.begin_pos ).source first_comma_offset = source_after_last_arg.index(',') first_new_line_offset = source_after_last_arg.index("\n") return if first_comma_offset.nil? return if first_new_line_offset.nil? return if first_comma_offset > first_new_line_offset first_comma_offset + 1 end
#on_send(node)
[ GitHub ]# File 'lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb', line 64
def on_send(node) heredoc_arg = extract_heredoc_argument(node) return unless heredoc_arg outermost_send = outermost_send_on_same_line(heredoc_arg) return unless outermost_send return if end_keyword_before_closing_parenthesis?(node) return if subsequent_closing_parentheses_in_same_line?(outermost_send) return if exist_argument_between_heredoc_end_and_closing_parentheses?(node) add_offense(outermost_send.loc.end) do |corrector| autocorrect(corrector, outermost_send) end end
#outermost_send_on_same_line(heredoc) (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb', line 115
def outermost_send_on_same_line(heredoc) previous = heredoc current = previous.parent until send_missing_closing_parens?(current, previous, heredoc) previous = current current = current.parent return unless previous && current end current end
#remove_incorrect_closing_paren(node, corrector) (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb', line 188
def remove_incorrect_closing_paren(node, corrector) corrector.remove( range_between( incorrect_parenthesis_removal_begin(node), incorrect_parenthesis_removal_end(node) ) ) end
#remove_incorrect_external_trailing_comma(node, corrector) (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb', line 277
def remove_incorrect_external_trailing_comma(node, corrector) end_pos = node.source_range.end_pos return unless external_trailing_comma?(node) corrector.remove( range_between( end_pos, end_pos + external_trailing_comma_offset_from_loc_end(node) ) ) end
#remove_internal_trailing_comma(node, corrector) (private)
Internal trailing comma helpers.
# File 'lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb', line 238
def remove_internal_trailing_comma(node, corrector) offset = internal_trailing_comma_offset_from_last_arg(node) last_arg_end_pos = node.children.last.source_range.end_pos corrector.remove(range_between(last_arg_end_pos, last_arg_end_pos + offset)) end
#safe_to_remove_line_containing_closing_paren?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb', line 207
def safe_to_remove_line_containing_closing_paren?(node) last_line = processed_source[node.loc.end.line - 1] # Safe to remove if last line only contains `)`, `,`, and whitespace. last_line.match?(/^ *\) {0,20},{0,1} *$/) end
#send_missing_closing_parens?(parent, child, heredoc) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb', line 126
def send_missing_closing_parens?(parent, child, heredoc) parent&.call_type? && parent.arguments.include?(child) && parent.loc.begin && parent.loc.end.line != heredoc.last_line end
#single_line_send_with_heredoc_receiver?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb', line 153
def single_line_send_with_heredoc_receiver?(node) return false unless node.send_type? return false unless heredoc_node?(node.receiver) node.receiver.location.heredoc_end.end_pos > node.source_range.end_pos end
#space?(pos) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb', line 305
def space?(pos) processed_source.buffer.source[pos] == ' ' end
#subsequent_closing_parentheses_in_same_line?(outermost_send) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb', line 168
def subsequent_closing_parentheses_in_same_line?(outermost_send) last_arg_of_outer_send = outermost_send.last_argument return false unless last_arg_of_outer_send&.loc.respond_to?(:end) && (end_of_last_arg_of_outer_send = last_arg_of_outer_send.loc.end) end_of_outer_send = outermost_send.loc.end same_line?(end_of_outer_send, end_of_last_arg_of_outer_send) && end_of_outer_send.column == end_of_last_arg_of_outer_send.column + 1 end