Class: RuboCop::Cop::Lint::SafeNavigationConsistency

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
NilMethods
Defined in:
lib/rubocop/cop/lint/safe_navigation_consistency.rb

Overview

Check to make sure that if safe navigation is used in an ‘&&` or `||` condition, consistent and appropriate safe navigation, without excess or deficiency, is used for all method calls on the same object.

Examples:

# bad
foo&.bar && foo&.baz

# good
foo&.bar && foo.baz

# bad
foo.bar && foo&.baz

# good
foo.bar && foo.baz

# bad
foo&.bar || foo.baz

# good
foo&.bar || foo&.baz

# bad
foo.bar || foo&.baz

# good
foo.bar || foo.baz

# bad
foo&.bar && (foobar.baz || foo&.baz)

# good
foo&.bar && (foobar.baz || foo.baz)

Constant Summary collapse

USE_DOT_MSG =
'Use `.` instead of unnecessary `&.`.'
USE_SAFE_NAVIGATION_MSG =
'Use `&.` for consistency with safe navigation.'

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

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

Instance Method Details

#on_and(node) ⇒ Object Also known as: on_or



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rubocop/cop/lint/safe_navigation_consistency.rb', line 48

def on_and(node)
  all_operands = collect_operands(node, [])
  operand_groups = all_operands.group_by { |operand| receiver_name_as_key(operand, +'') }

  operand_groups.each_value do |grouped_operands|
    next unless (dot_op, begin_of_rest_operands = find_consistent_parts(grouped_operands))

    rest_operands = grouped_operands[begin_of_rest_operands..]
    rest_operands.each do |operand|
      next if already_appropriate_call?(operand, dot_op)

      register_offense(operand, dot_op)
    end
  end
end