module RuboCop::Cop::RSpec::ExplicitHelper

A helper for `explicit` style rubocop:disable Metrics/ModuleLength

Constants

BUILT_IN_MATCHERS
MSG_EXPLICIT

Private Instance Methods

autocorrect_explicit(node) click to toggle source
# File lib/rubocop/cop/rspec/predicate_matcher.rb, line 195
def autocorrect_explicit(node)
  autocorrect_explicit_send(node) ||
    autocorrect_explicit_block(node)
end
autocorrect_explicit_block(node) click to toggle source
# File lib/rubocop/cop/rspec/predicate_matcher.rb, line 206
def autocorrect_explicit_block(node)
  predicate_matcher_block?(node) do |actual, matcher|
    to_node = node.send_node
    corrector_explicit(to_node, actual, matcher, to_node)
  end
end
autocorrect_explicit_send(node) click to toggle source
# File lib/rubocop/cop/rspec/predicate_matcher.rb, line 200
def autocorrect_explicit_send(node)
  predicate_matcher?(node) do |actual, matcher|
    corrector_explicit(node, actual, matcher, matcher)
  end
end
check_explicit(node) click to toggle source
# File lib/rubocop/cop/rspec/predicate_matcher.rb, line 142
def check_explicit(node) # rubocop:disable Metrics/MethodLength
  predicate_matcher_block?(node) do |_actual, matcher|
    add_offense(
      node,
      location: :expression,
      message: message_explicit(matcher)
    )
    ignore_node(node.children.first)
    return
  end

  return if part_of_ignored_node?(node)

  predicate_matcher?(node) do |_actual, matcher|
    add_offense(
      node,
      location: :expression,
      message: message_explicit(matcher)
    )
  end
end
corrector_explicit(to_node, actual, matcher, block_child) click to toggle source
# File lib/rubocop/cop/rspec/predicate_matcher.rb, line 213
def corrector_explicit(to_node, actual, matcher, block_child)
  lambda do |corrector|
    replacement_matcher = replacement_matcher(to_node)
    corrector.replace(matcher.loc.expression, replacement_matcher)
    move_predicate(corrector, actual, matcher, block_child)
    corrector.replace(to_node.loc.selector, 'to')
  end
end
message_explicit(matcher) click to toggle source
# File lib/rubocop/cop/rspec/predicate_matcher.rb, line 189
def message_explicit(matcher)
  format(MSG_EXPLICIT,
         predicate_name: to_predicate_method(matcher.method_name),
         matcher_name: matcher.method_name)
end
move_predicate(corrector, actual, matcher, block_child) click to toggle source
# File lib/rubocop/cop/rspec/predicate_matcher.rb, line 222
def move_predicate(corrector, actual, matcher, block_child)
  predicate = to_predicate_method(matcher.method_name)
  args = args_loc(matcher).source
  block_loc = block_loc(block_child)
  block = block_loc ? block_loc.source : ''

  corrector.remove(block_loc) if block_loc
  corrector.insert_after(actual.loc.expression,
                         ".#{predicate}" + args + block)
end
predicate_matcher_name?(name) click to toggle source
# File lib/rubocop/cop/rspec/predicate_matcher.rb, line 181
def predicate_matcher_name?(name)
  name = name.to_s

  return false if BUILT_IN_MATCHERS.include?(name)

  name.start_with?('be_', 'have_') && !name.end_with?('?')
end
replacement_matcher(node) click to toggle source

rubocop:enable Metrics/MethodLength

# File lib/rubocop/cop/rspec/predicate_matcher.rb, line 250
def replacement_matcher(node)
  case [cop_config['Strict'], node.method_name == :to]
  when [true, true]
    'be(true)'
  when [true, false]
    'be(false)'
  when [false, true]
    'be_truthy'
  when [false, false]
    'be_falsey'
  end
end
to_predicate_method(matcher) click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/rubocop/cop/rspec/predicate_matcher.rb, line 234
def to_predicate_method(matcher)
  case matcher = matcher.to_s
  when 'be_a', 'be_an', 'be_a_kind_of', 'a_kind_of', 'be_kind_of'
    'is_a?'
  when 'be_an_instance_of', 'be_instance_of', 'an_instance_of'
    'instance_of?'
  when 'include', 'respond_to'
    matcher + '?'
  when /^have_(.+)/
    "has_#{Regexp.last_match(1)}?"
  else
    matcher[/^be_(.+)/, 1] + '?'
  end
end