Class: RuboCop::Cop::Layout::RescueEnsureAlignment
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:
|
|
Inherits: |
RuboCop::Cop::Base
|
Defined in: | lib/rubocop/cop/layout/rescue_ensure_alignment.rb |
Overview
Checks whether the rescue and ensure keywords are aligned properly.
Constant Summary
-
ALTERNATIVE_ACCESS_MODIFIERS =
# File 'lib/rubocop/cop/layout/rescue_ensure_alignment.rb', line 34%i[public_class_method private_class_method].freeze
-
ANCESTOR_TYPES =
# File 'lib/rubocop/cop/layout/rescue_ensure_alignment.rb', line 32%i[kwbegin def defs class module block numblock].freeze
-
ANCESTOR_TYPES_WITH_ACCESS_MODIFIERS =
# File 'lib/rubocop/cop/layout/rescue_ensure_alignment.rb', line 33%i[def defs].freeze
-
MSG =
# File 'lib/rubocop/cop/layout/rescue_ensure_alignment.rb', line 29'`%<kw_loc>s` at %<kw_loc_line>d, %<kw_loc_column>d is not ' \ 'aligned with `%<beginning>s` at ' \ '%<begin_loc_line>d, %<begin_loc_column>d.'
::RuboCop::Cop::Base
- Inherited
EMPTY_OFFENSES, RESTRICT_ON_SEND
::RuboCop::Cop::RangeHelp
- Included
::RuboCop::Cop::ConfigurableEnforcedStyle
- Included
::RuboCop::Cop::EndKeywordAlignment
- 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 #on_new_investigation and |
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::ConfigurableEnforcedStyle
- Included
::RuboCop::Cop::Base
- Inherited
::RuboCop::Cop::AutocorrectLogic
- Included
Instance Method Summary
- #on_ensure(node)
- #on_new_investigation
- #on_resbody(node)
- #access_modifier?(node) ⇒ Boolean private
- #access_modifier_node(node) private
- #aligned_with_leading_dot?(do_keyword_line, send_node_loc, rescue_keyword_column) ⇒ Boolean private
- #aligned_with_line_break_method?(ancestor_node, node) ⇒ Boolean private
- #alignment_location(alignment_node) private
-
#alignment_node(node)
private
We will use ancestor or wrapper with access modifier.
- #alignment_source(node, starting_loc) private
- #ancestor_node(node) private
- #assignment_node(node) private
- #autocorrect(corrector, node, alignment_location) private
- #begin_end_alignment_style private
-
#check(node)
private
Check alignment of node with rescue or ensure modifiers.
- #format_message(alignment_node, alignment_loc, kw_loc) private
- #modifier?(node) ⇒ Boolean private
- #whitespace_range(node) private
::RuboCop::Cop::EndKeywordAlignment
- Included
::RuboCop::Cop::ConfigurableEnforcedStyle
- Included
::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
Instance Method Details
#access_modifier?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/layout/rescue_ensure_alignment.rb', line 187
def access_modifier?(node) return true if node.respond_to?(:access_modifier?) && node.access_modifier? return true if node.respond_to?(:method_name) && ALTERNATIVE_ACCESS_MODIFIERS.include?(node.method_name) false end
#access_modifier_node(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/rescue_ensure_alignment.rb', line 164
def access_modifier_node(node) return nil unless ANCESTOR_TYPES_WITH_ACCESS_MODIFIERS.include?(node.type) access_modifier_node = node.ancestors.first return nil unless access_modifier?(access_modifier_node) access_modifier_node end
#aligned_with_leading_dot?(do_keyword_line, send_node_loc, rescue_keyword_column) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/layout/rescue_ensure_alignment.rb', line 150
def aligned_with_leading_dot?(do_keyword_line, send_node_loc, rescue_keyword_column) return false unless send_node_loc.respond_to?(:dot) && (dot = send_node_loc.dot) do_keyword_line == dot.line && rescue_keyword_column == dot.column end
#aligned_with_line_break_method?(ancestor_node, node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/layout/rescue_ensure_alignment.rb', line 137
def aligned_with_line_break_method?(ancestor_node, node) send_node_loc = ancestor_node.send_node.loc do_keyword_line = ancestor_node.loc.begin.line rescue_keyword_column = node.loc.keyword.column selector = send_node_loc.respond_to?(:selector) ? send_node_loc.selector : send_node_loc if aligned_with_leading_dot?(do_keyword_line, send_node_loc, rescue_keyword_column) return true end do_keyword_line == selector&.line && rescue_keyword_column == selector.column end
#alignment_location(alignment_node) (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/rescue_ensure_alignment.rb', line 196
def alignment_location(alignment_node) if begin_end_alignment_style == 'start_of_line' start_line_range(alignment_node) else alignment_node.source_range end end
#alignment_node(node) (private)
We will use ancestor or wrapper with access modifier.
# File 'lib/rubocop/cop/layout/rescue_ensure_alignment.rb', line 117
def alignment_node(node) ancestor_node = ancestor_node(node) return ancestor_node if ancestor_node.nil? || ancestor_node.kwbegin_type? return if ancestor_node.respond_to?(:send_node) && aligned_with_line_break_method?(ancestor_node, node) assignment_node = assignment_node(ancestor_node) return assignment_node if same_line?(ancestor_node, assignment_node) access_modifier_node = access_modifier_node(ancestor_node) return access_modifier_node unless access_modifier_node.nil? ancestor_node end
#alignment_source(node, starting_loc) (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/rescue_ensure_alignment.rb', line 96
def alignment_source(node, starting_loc) ending_loc = case node.type when :block, :numblock, :kwbegin node.loc.begin when :def, :defs, :class, :module, :lvasgn, :ivasgn, :cvasgn, :gvasgn, :casgn node.loc.name when :masgn node.lhs.source_range else # It is a wrapper with receiver of object attribute or access modifier. node.receiver&.source_range || node.child_nodes.first.loc.name end range_between(starting_loc.begin_pos, ending_loc.end_pos).source end
#ancestor_node(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/rescue_ensure_alignment.rb', line 133
def ancestor_node(node) node.each_ancestor(*ANCESTOR_TYPES).first end
#assignment_node(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/rescue_ensure_alignment.rb', line 156
def assignment_node(node) assignment_node = node.ancestors.first return nil unless assignment_node&.assignment? assignment_node end
#autocorrect(corrector, node, alignment_location) (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/rescue_ensure_alignment.rb', line 73
def autocorrect(corrector, node, alignment_location) whitespace = whitespace_range(node) # Some inline node is sitting before current node. return nil unless whitespace.source.strip.empty? new_column = alignment_location.column corrector.replace(whitespace, ' ' * new_column) end
#begin_end_alignment_style (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/rescue_ensure_alignment.rb', line 204
def begin_end_alignment_style begin_end_alignment_conf = config.for_cop('Layout/BeginEndAlignment') begin_end_alignment_conf['Enabled'] && begin_end_alignment_conf['EnforcedStyleAlignWith'] end
#check(node) (private)
Check alignment of node with rescue or ensure modifiers.
# File 'lib/rubocop/cop/layout/rescue_ensure_alignment.rb', line 57
def check(node) alignment_node = alignment_node(node) return if alignment_node.nil? alignment_loc = alignment_location(alignment_node) kw_loc = node.loc.keyword return if alignment_loc.column == kw_loc.column || same_line?(alignment_loc, kw_loc) add_offense( kw_loc, message: (alignment_node, alignment_loc, kw_loc) ) do |corrector| autocorrect(corrector, node, alignment_loc) end end
#format_message(alignment_node, alignment_loc, kw_loc) (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/rescue_ensure_alignment.rb', line 83
def (alignment_node, alignment_loc, kw_loc) format( MSG, kw_loc: kw_loc.source, kw_loc_line: kw_loc.line, kw_loc_column: kw_loc.column, beginning: alignment_source(alignment_node, alignment_loc), begin_loc_line: alignment_loc.line, begin_loc_column: alignment_loc.column ) end
#modifier?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/layout/rescue_ensure_alignment.rb', line 174
def modifier?(node) return false unless @modifier_locations.respond_to?(:include?) @modifier_locations.include?(node.loc.keyword) end
#on_ensure(node)
[ GitHub ]# File 'lib/rubocop/cop/layout/rescue_ensure_alignment.rb', line 40
def on_ensure(node) check(node) end
#on_new_investigation
[ GitHub ]# File 'lib/rubocop/cop/layout/rescue_ensure_alignment.rb', line 44
def on_new_investigation @modifier_locations = processed_source.tokens.each_with_object([]) do |token, locations| next unless token.rescue_modifier? locations << token.pos end end
#on_resbody(node)
[ GitHub ]#whitespace_range(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/rescue_ensure_alignment.rb', line 180
def whitespace_range(node) begin_pos = node.loc.keyword.begin_pos current_column = node.loc.keyword.column range_between(begin_pos - current_column, begin_pos) end