class RuboCop::Cop::InternalAffairs::StyleDetectedApiUse

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.

@example

# 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

Constants

MSG_FOR_CONDITIONAL_USE
MSG_FOR_NEGATIVE_WITHOUT_POSITIVE
MSG_FOR_POSITIVE_WITHOUT_NEGATIVE
RESTRICT_ON_SEND

Attributes

correct_style_detected_called[R]
negative_style_detected_methods_called[R]
style_detected_called[R]

Public Instance Methods

on_if(node) click to toggle source
# 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_end() click to toggle source
# 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_investigation() click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

negative_without_positive?() click to toggle source
# File lib/rubocop/cop/internal_affairs/style_detected_api_use.rb, line 129
def negative_without_positive?
  negative_style_detected_methods_called && !correct_style_detected_called
end
positive_without_negative?() click to toggle source
# File lib/rubocop/cop/internal_affairs/style_detected_api_use.rb, line 125
def positive_without_negative?
  correct_style_detected_called && !negative_style_detected_methods_called
end
style_detected_api_used?(node) click to toggle source
# File lib/rubocop/cop/internal_affairs/style_detected_api_use.rb, line 133
def style_detected_api_used?(node)
  correct_style_detected_check(node) ||
    negative_style_detected_method_check(node) ||
    no_acceptable_style_check(node) ||
    style_detected_check(node)
end
traverse_condition(condition) { |condition| ... } click to toggle source
# File lib/rubocop/cop/internal_affairs/style_detected_api_use.rb, line 140
def traverse_condition(condition, &block)
  yield condition if condition.send_type?

  condition.each_child_node { |child| traverse_condition(child, &block) }
end