class RuboCop::Cop::RSpec::ImplicitSubject


it { expect(subject).to be_truthy }
# good
it { is_expected.to be_truthy }
# bad
@example ‘EnforcedStyle: disallow`
end
is_expected.to be_truthy
it do
end
expect(subject).to be_truthy
foo = 1
it do
# good
end
is_expected.to be_truthy
foo = 1
it do
# bad
@example `EnforcedStyle: single_statement_only`
end
expect(subject).to be_truthy
it do
it { is_expected.to be_truthy }
# good
end
is_expected.to be_truthy
it do
# bad
@example `EnforcedStyle: single_line_only` (default)
This cop can be configured using the `EnforcedStyle` option
Checks for usage of implicit subject (`is_expected` / `should`).

def allowed_by_style?(example)

def allowed_by_style?(example)
  case style
  when :single_line_only
    example.single_line?
  when :single_statement_only
    !example.body.begin_type?
  else
    false
  end
end

def autocorrect(corrector, node)

def autocorrect(corrector, node)
  replacement = 'expect(subject)'
  case node.method_name
  when :should
    replacement += '.to'
  when :should_not
    replacement += '.not_to'
  end
  corrector.replace(node.loc.selector, replacement)
end

def on_send(node)

def on_send(node)
  return unless implicit_subject?(node)
  return if valid_usage?(node)
  add_offense(node) do |corrector|
    autocorrect(corrector, node)
  end
end

def valid_usage?(node)

def valid_usage?(node)
  example = node.ancestors.find { |parent| example?(parent) }
  return false if example.nil?
  example.method?(:its) || allowed_by_style?(example)
end