Class: RuboCop::Cop::InternalAffairs::StyleDetectedApiUse

Inherits:
Base
  • Object
show all
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/internal_affairs/style_detected_api_use.rb

Overview

Checks for correct use of the style_detected API provided by `ConfigurableEnforcedStyle`. If `correct_style_detected` is used then `opposite_style_detected`, `unexpected_style_detected`, `ambiguous_style_detected`, `conflicting_styles_detected`, `unrecognized_style_detected` or `no_acceptable_style!` should be used too, and vice versa. The `xxx_style_detected` methods should not be used as predicates either.

Examples:


# bad
def on_send(node)
  return add_offense(node) if opposite_style_detected

  correct_style_detected
end

def on_send(node)
  if offense?
    add_offense(node)
  else
    correct_style_detected
  end
end

def on_send(node)
  return unless offense?

  add_offense(node)
  opposite_style_detected
end

# good
def on_send(node)
  if offense?
    add_offense(node)
    opposite_style_detected
  else
    correct_style_detected
  end
end

def on_send(node)
  add_offense(node) if offense?
end

Constant Summary collapse

MSG_FOR_POSITIVE_WITHOUT_NEGATIVE =
'`correct_style_detected` method called without ' \
'calling a negative `*_style_detected` method.'
MSG_FOR_NEGATIVE_WITHOUT_POSITIVE =
'negative `*_style_detected` methods called without ' \
'calling `correct_style_detected` method.'
MSG_FOR_CONDITIONAL_USE =
'`*_style_detected` method called in conditional.'
RESTRICT_ON_SEND =
%i[
  correct_style_detected opposite_style_detected
  unexpected_style_detected ambiguous_style_detected
  conflicting_styles_detected unrecognized_style_detected
  no_acceptable_style! style_detected
].freeze

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods inherited from Base

#add_global_offense, #add_offense, autocorrect_incompatible_with, badge, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, joining_forces, lint?, match?, #message, #offenses, #on_other_file, #ready, #relevant_file?, 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?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from RuboCop::Cop::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_if(node) ⇒ Object


110
111
112
113
114
# File 'lib/rubocop/cop/internal_affairs/style_detected_api_use.rb', line 110

def on_if(node)
  traverse_condition(node.condition) do |cond|
    add_offense(cond, message: MSG_FOR_CONDITIONAL_USE) if style_detected_api_used?(cond)
  end
end

#on_investigation_endObject


92
93
94
95
96
97
98
# File 'lib/rubocop/cop/internal_affairs/style_detected_api_use.rb', line 92

def on_investigation_end
  return if style_detected_called
  return unless correct_style_detected_called ^ negative_style_detected_methods_called

  add_global_offense(MSG_FOR_POSITIVE_WITHOUT_NEGATIVE) if positive_without_negative?
  add_global_offense(MSG_FOR_NEGATIVE_WITHOUT_POSITIVE) if negative_without_positive?
end

#on_new_investigationObject


86
87
88
89
90
# File 'lib/rubocop/cop/internal_affairs/style_detected_api_use.rb', line 86

def on_new_investigation
  @correct_style_detected_called = false
  @negative_style_detected_methods_called = false
  @style_detected_called = false
end

#on_send(node) ⇒ Object


100
101
102
103
104
105
106
107
108
# File 'lib/rubocop/cop/internal_affairs/style_detected_api_use.rb', line 100

def on_send(node)
  if correct_style_detected_check(node)
    @correct_style_detected_called = true
  elsif negative_style_detected_method_check(node) || no_acceptable_style_check(node)
    @negative_style_detected_methods_called = true
  elsif style_detected_check(node)
    @style_detected_called = true
  end
end