123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::Layout::MultilineOperationIndentation

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

Overview

Checks the indentation of the right hand side operand in binary operations that span more than one line.

The aligned style checks that operators are aligned if they are part of an if or while condition, an explicit return statement, etc. In other contexts, the second operand should be indented regardless of enforced style.

Examples:

EnforcedStyle: aligned (default)

# bad
if a +
    b
  something &&
  something_else
end

# good
if a +
   b
  something &&
    something_else
end

EnforcedStyle: indented

# bad
if a +
   b
  something &&
  something_else
end

# good
if a +
    b
  something &&
    something_else
end

Constant Summary

::RuboCop::Cop::Base - Inherited

EMPTY_OFFENSES, RESTRICT_ON_SEND

::RuboCop::Cop::ConfigurableEnforcedStyle - Included

SYMBOL_TO_STRING_CACHE

::RuboCop::Cop::Alignment - Included

SPACE

::RuboCop::Cop::MultilineExpressionIndentation - Included

ASSIGNMENT_MESSAGE_TAIL, DEFAULT_MESSAGE_TAIL, KEYWORD_ANCESTOR_TYPES, KEYWORD_MESSAGE_TAIL, UNALIGNED_RHS_TYPES

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::MultilineExpressionIndentation - Included

#on_csend
#on_send,
#argument_in_method_call

rubocop:todo Metrics/CyclomaticComplexity.

#assignment_rhs, #check,
#correct_indentation

The correct indentation of node is usually IndentationWidth, with one exception: prefix keywords.

#disqualified_rhs?, #grouped_expression?, #incorrect_style_detected, #indentation, #indented_keyword_expression, #inside_arg_list_parentheses?, #keyword_message_tail, #kw_node_with_special_indentation,
#left_hand_side

In a chain of method calls, we regard the top call node as the base for indentation of all lines following the first.

#not_for_this_cop?, #operation_description, #part_of_assignment_rhs, #part_of_block_body?,
#postfix_conditional?

Returns true if node is a conditional whose body and condition begin on the same line.

#valid_method_rhs_candidate?

The []= operator and setters (a.b = c) are parsed as :send nodes.

#valid_rhs?, #valid_rhs_candidate?, #within_node?

::RuboCop::Cop::Alignment - Included

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

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 Method Details

#autocorrect(corrector, node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/multiline_operation_indentation.rb', line 68

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

#check_and_or(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/multiline_operation_indentation.rb', line 78

def check_and_or(node)
  lhs, rhs = *node
  range = offending_range(node, lhs, rhs.source_range, style)
  check(range, node, lhs, rhs.source_range)
end

#message(node, lhs, rhs) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/multiline_operation_indentation.rb', line 110

def message(node, lhs, rhs)
  what = operation_description(node, rhs)
  if should_align?(node, rhs, style)
    "Align the operands of #{what} spanning multiple lines."
  else
    used_indentation = rhs.column - indentation(lhs)
    "Use #{correct_indentation(node)} (not #{used_indentation}) " \
      "spaces for indenting #{what} spanning multiple lines."
  end
end

#offending_range(node, lhs, rhs, given_style) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/multiline_operation_indentation.rb', line 84

def offending_range(node, lhs, rhs, given_style)
  return false unless begins_its_line?(rhs)
  return false if not_for_this_cop?(node)

  correct_column = if should_align?(node, rhs, given_style)
                     node.loc.column
                   else
                     indentation(lhs) + correct_indentation(node)
                   end
  @column_delta = correct_column - rhs.column
  rhs if @column_delta.nonzero?
end

#on_and(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/multiline_operation_indentation.rb', line 49

def on_and(node)
  check_and_or(node)
end

#on_or(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/multiline_operation_indentation.rb', line 53

def on_or(node)
  check_and_or(node)
end

#relevant_node?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/multiline_operation_indentation.rb', line 72

def relevant_node?(node)
  return false if node.send_type? && node.unary_operation?

  !node.loc.dot # Don't check method calls with dot operator.
end

#right_hand_side(send_node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/multiline_operation_indentation.rb', line 121

def right_hand_side(send_node)
  send_node.first_argument.source_range
end

#should_align?(node, rhs, given_style) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/multiline_operation_indentation.rb', line 97

def should_align?(node, rhs, given_style)
  assignment_node = part_of_assignment_rhs(node, rhs)
  if assignment_node
    assignment_rhs = CheckAssignment.extract_rhs(assignment_node)
    return true if begins_its_line?(assignment_rhs.source_range)
  end

  given_style == :aligned &&
    (kw_node_with_special_indentation(node) ||
     assignment_node ||
     argument_in_method_call(node, :with_or_without_parentheses))
end

#validate_config

Raises:

[ GitHub ]

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

def validate_config
  return unless style == :aligned && cop_config['IndentationWidth']

  raise ValidationError, 'The `Layout/MultilineOperationIndentation` ' \
                         'cop only accepts an `IndentationWidth` ' \
                         'configuration parameter when ' \
                         '`EnforcedStyle` is `indented`.'
end