123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::Layout::ElseAlignment

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/else_alignment.rb

Overview

Checks the alignment of else keywords. Normally they should be aligned with an if/unless/while/until/begin/def/rescue keyword, but there are special cases when they should follow the same rules as the alignment of end.

Examples:

# bad
if something
  code
 else
  code
end

# bad
if something
  code
 elsif something
  code
end

# good
if something
  code
else
  code
end

Constant Summary

::RuboCop::Cop::Base - Inherited

EMPTY_OFFENSES, RESTRICT_ON_SEND

::RuboCop::Cop::ConfigurableEnforcedStyle - Included

SYMBOL_TO_STRING_CACHE

::RuboCop::Cop::RangeHelp - Included

BYTE_ORDER_MARK, NOT_GIVEN

::RuboCop::Cop::EndKeywordAlignment - Included

MSG

::RuboCop::Cop::Alignment - Included

SPACE

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 on_investigation_end, the result of processed_source will remain constant.

.builtin?

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

Cops (other than builtin) are encouraged to implement this.

.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

Instance Method Summary

::RuboCop::Cop::CheckAssignment - Included

::RuboCop::Cop::Alignment - Included

::RuboCop::Cop::EndKeywordAlignment - 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::ConfigurableEnforcedStyle - Included

::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 add_offense or add_global_offense Cops are discouraged to override this; instead pass your message directly.

#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_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, #use_corrector

::RuboCop::Cop::AutocorrectLogic - Included

::RuboCop::Cop::IgnoredNode - Included

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#assignment_node(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/else_alignment.rb', line 147

def assignment_node(node)
  assignment_node = node.ancestors.first
  return unless assignment_node&.assignment?

  assignment_node
end

#autocorrect(corrector, node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/else_alignment.rb', line 71

def autocorrect(corrector, node)
  AlignmentCorrector.correct(corrector, processed_source, node, column_delta)
end

#base_for_method_definition(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/else_alignment.rb', line 106

def base_for_method_definition(node)
  parent = node.parent
  if parent&.send_type?
    parent.loc.selector # For example "private def ..."
  else
    node.loc.keyword
  end
end

#base_range_of_if(node, base) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/else_alignment.rb', line 80

def base_range_of_if(node, base)
  if base
    base.source_range
  else
    lineage = [node, *node.each_ancestor(:if)]
    lineage.find { |parent| parent.if? || parent.unless? }.loc.keyword
  end
end

#base_range_of_rescue(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/else_alignment.rb', line 89

def base_range_of_rescue(node)
  parent = node.parent
  parent = parent.parent if parent.ensure_type?
  case parent.type
  when :def, :defs then base_for_method_definition(parent)
  when :kwbegin then parent.loc.begin
  when :block
    assignment_node = assignment_node(parent)
    if same_line?(parent, assignment_node)
      assignment_node.source_range
    else
      parent.send_node.source_range
    end
  else node.loc.keyword
  end
end

#check_alignment(base_range, else_range) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/else_alignment.rb', line 131

def check_alignment(base_range, else_range)
  return unless begins_its_line?(else_range)

  @column_delta = column_offset_between(base_range, else_range)
  return if @column_delta.zero?

  message = format(
    MSG,
    else_range: else_range.source,
    base_range: base_range.source[/^\S*/]
  )
  add_offense(else_range, message: message) do |corrector|
    autocorrect(corrector, else_range)
  end
end

#check_assignment(node, rhs) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/else_alignment.rb', line 115

def check_assignment(node, rhs)
  # If there are method calls chained to the right hand side of the
  # assignment, we let rhs be the receiver of those method calls before
  # we check its indentation.
  rhs = first_part_of_call_chain(rhs)
  return unless rhs

  end_config = config.for_cop('Layout/EndAlignment')
  style = end_config['EnforcedStyleAlignWith'] || 'keyword'
  base = variable_alignment?(node.loc, rhs, style.to_sym) ? node : rhs

  return unless rhs.if_type?

  check_nested(rhs, base)
end

#check_nested(node, base) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/else_alignment.rb', line 75

def check_nested(node, base)
  on_if(node, base)
  ignore_node(node)
end

#on_case(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/else_alignment.rb', line 57

def on_case(node)
  return unless node.else?

  check_alignment(node.when_branches.last.loc.keyword, node.loc.else)
end

#on_case_match(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/else_alignment.rb', line 63

def on_case_match(node)
  return unless node.else?

  check_alignment(node.in_pattern_branches.last.loc.keyword, node.loc.else)
end

#on_if(node, base = nil)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/else_alignment.rb', line 40

def on_if(node, base = nil)
  return if ignored_node?(node)
  return unless node.else? && begins_its_line?(node.loc.else)

  check_alignment(base_range_of_if(node, base), node.loc.else)

  return unless node.elsif_conditional?

  check_nested(node.else_branch, base)
end

#on_rescue(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/else_alignment.rb', line 51

def on_rescue(node)
  return unless node.loc.respond_to?(:else) && node.loc.else

  check_alignment(base_range_of_rescue(node), node.loc.else)
end