123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::Layout::CaseIndentation

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

Overview

Checks how the when and ins of a case expression are indented in relation to its case or end keyword.

It will register a separate offense for each misaligned when and in.

Examples:

# If Layout/EndAlignment is set to keyword style (default)
# *case* and *end* should always be aligned to same depth,
# and therefore *when* should always be aligned to both -
# regardless of configuration.

# bad for all styles
case n
  when 0
    x * 2
  else
    y / 3
end

case n
  in pattern
    x * 2
  else
    y / 3
end

# good for all styles
case n
when 0
  x * 2
else
  y / 3
end

case n
in pattern
  x * 2
else
  y / 3
end

EnforcedStyle: case (default)

# if EndAlignment is set to other style such as
# start_of_line (as shown below), then *when* alignment
# configuration does have an effect.

# bad
a = case n
when 0
  x * 2
else
  y / 3
end

a = case n
in pattern
  x * 2
else
  y / 3
end

# good
a = case n
    when 0
      x * 2
    else
      y / 3
end

a = case n
    in pattern
      x * 2
    else
      y / 3
end

EnforcedStyle: end

# bad
a = case n
    when 0
      x * 2
    else
      y / 3
end

a = case n
    in pattern
      x * 2
    else
      y / 3
end

# good
a = case n
when 0
  x * 2
else
  y / 3
end

a = case n
in pattern
  x * 2
else
  y / 3
end

Constant Summary

::RuboCop::Cop::Base - Inherited

EMPTY_OFFENSES, RESTRICT_ON_SEND

::RuboCop::Cop::Alignment - Included

SPACE

::RuboCop::Cop::ConfigurableEnforcedStyle - Included

SYMBOL_TO_STRING_CACHE

::RuboCop::Cop::RangeHelp - Included

BYTE_ORDER_MARK, NOT_GIVEN

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.

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

Instance Method Summary

::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::Alignment - 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_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

::RuboCop::Cop::IgnoredNode - Included

Constructor Details

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

Instance Attribute Details

#enforced_style_end?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/case_indentation.rb', line 146

def enforced_style_end?
  cop_config[style_parameter_name] == 'end'
end

#indent_one_step?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/case_indentation.rb', line 161

def indent_one_step?
  cop_config['IndentOneStep']
end

Instance Method Details

#base_column(case_node, base) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/case_indentation.rb', line 193

def base_column(case_node, base)
  case base
  when :case then case_node.location.keyword.column
  when :end  then case_node.location.end.column
  end
end

#check_when(when_node, branch_type) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/case_indentation.rb', line 150

def check_when(when_node, branch_type)
  when_column = when_node.loc.keyword.column
  base_column = base_column(when_node.parent, style)

  if when_column == base_column + indentation_width
    correct_style_detected
  else
    incorrect_style(when_node, branch_type)
  end
end

#detect_incorrect_style(when_node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/case_indentation.rb', line 182

def detect_incorrect_style(when_node)
  when_column = when_node.loc.keyword.column
  base_column = base_column(when_node.parent, alternative_style)

  if when_column == base_column
    opposite_style_detected
  else
    unrecognized_style_detected
  end
end

#end_and_last_conditional_same_line?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/case_indentation.rb', line 136

def end_and_last_conditional_same_line?(node)
  end_line = node.loc.end&.line
  last_conditional_line = if node.loc.else
                            node.loc.else.line
                          else
                            node.child_nodes.last.loc.begin&.line
                          end
  end_line && last_conditional_line && end_line == last_conditional_line
end

#incorrect_style(when_node, branch_type) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/case_indentation.rb', line 169

def incorrect_style(when_node, branch_type)
  depth = indent_one_step? ? 'one step more than' : 'as deep as'
  message = format(MSG, branch_type: branch_type, depth: depth, base: style)

  add_offense(when_node.loc.keyword, message: message) do |corrector|
    detect_incorrect_style(when_node)

    whitespace = whitespace_range(when_node)

    corrector.replace(whitespace, replacement(when_node)) if whitespace.source.strip.empty?
  end
end

#indentation_width (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/case_indentation.rb', line 165

def indentation_width
  indent_one_step? ? configured_indentation_width : 0
end

#on_case(case_node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/case_indentation.rb', line 120

def on_case(case_node)
  return if case_node.single_line?
  return if enforced_style_end? && end_and_last_conditional_same_line?(case_node)

  case_node.when_branches.each { |when_node| check_when(when_node, 'when') }
end

#on_case_match(case_match_node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/case_indentation.rb', line 127

def on_case_match(case_match_node)
  return if case_match_node.single_line?
  return if enforced_style_end? && end_and_last_conditional_same_line?(case_match_node)

  case_match_node.each_in_pattern { |in_pattern_node| check_when(in_pattern_node, 'in') }
end

#replacement(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/case_indentation.rb', line 207

def replacement(node)
  case_node = node.each_ancestor(:case, :case_match).first
  base_type = cop_config[style_parameter_name] == 'end' ? :end : :case

  column = base_column(case_node, base_type)
  column += indentation_width

  ' ' * column
end

#whitespace_range(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/case_indentation.rb', line 200

def whitespace_range(node)
  when_column = node.location.keyword.column
  begin_pos = node.loc.keyword.begin_pos

  range_between(begin_pos - when_column, begin_pos)
end