Class: RuboCop::Cop::RSpec::LeadingSubject

Inherits:
RuboCop::Cop show all
Includes:
RSpec::Language, RSpec::SpecOnly
Defined in:
lib/rubocop/cop/rspec/leading_subject.rb

Overview

Checks for ‘subject` definitions that come after `let` definitions.

Examples:

# bad
RSpec.describe User do
  let(:params) { blah }
  subject { described_class.new(params) }

  it 'is valid' do
    expect(subject.valid?).to be(true)
  end
end

# good
RSpec.describe User do
  subject { described_class.new(params) }

  let(:params) { blah }

  it 'is valid' do
    expect(subject.valid?).to be(true)
  end
end

Constant Summary collapse

MSG =
'Declare `subject` above any other `let` declarations'.freeze

Constants included from RSpec::SpecOnly

RSpec::SpecOnly::DEFAULT_CONFIGURATION

Constants included from RSpec::Language

RSpec::Language::ALL

Instance Method Summary collapse

Methods included from RSpec::SpecOnly

#relevant_file?

Instance Method Details

#on_block(node) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rubocop/cop/rspec/leading_subject.rb', line 36

def on_block(node)
  return unless subject?(node) && !in_spec_block?(node)

  node.parent.each_child_node do |sibling|
    break if sibling.equal?(node)

    if sibling.method_name.equal?(:let)
      break add_offense(node, :expression)
    end
  end
end