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

#active_support_extensions_enabled?, #add_global_offense, #add_offense, autocorrect_incompatible_with, badge, #begin_investigation, #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, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_other_file, #parse, #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?, #autocorrect_with_disable_uncorrectable?, #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

#correct_style_detected_check(node) ⇒ Object



70
71
72
# File 'lib/rubocop/cop/internal_affairs/style_detected_api_use.rb', line 70

def_node_matcher :correct_style_detected_check, <<~PATTERN
  (send nil? :correct_style_detected)
PATTERN

#negative_style_detected_method_check(node) ⇒ Object



75
76
77
# File 'lib/rubocop/cop/internal_affairs/style_detected_api_use.rb', line 75

def_node_matcher :negative_style_detected_method_check, <<~PATTERN
  (send nil? /(?:opposite|unexpected|ambiguous|unrecognized)_style_detected|conflicting_styles_detected/ ...)
PATTERN

#no_acceptable_style_check(node) ⇒ Object



80
81
82
# File 'lib/rubocop/cop/internal_affairs/style_detected_api_use.rb', line 80

def_node_matcher :no_acceptable_style_check, <<~PATTERN
  (send nil? :no_acceptable_style!)
PATTERN

#on_if(node) ⇒ Object



113
114
115
116
117
# File 'lib/rubocop/cop/internal_affairs/style_detected_api_use.rb', line 113

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



95
96
97
98
99
100
101
# File 'lib/rubocop/cop/internal_affairs/style_detected_api_use.rb', line 95

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



89
90
91
92
93
# File 'lib/rubocop/cop/internal_affairs/style_detected_api_use.rb', line 89

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

#on_send(node) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/rubocop/cop/internal_affairs/style_detected_api_use.rb', line 103

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

#style_detected_check(node) ⇒ Object



85
86
87
# File 'lib/rubocop/cop/internal_affairs/style_detected_api_use.rb', line 85

def_node_matcher :style_detected_check, <<~PATTERN
  (send nil? :style_detected ...)
PATTERN