Sha256: c08b7c37dfc41115487f44710fa195eba7133ffe8ddb7c68829417334d459986

Contents?: true

Size: 1.18 KB

Versions: 14

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Ensure that subject is defined using subject helper.
      #
      # @example
      #
      #   # bad
      #   let(:subject) { foo }
      #   let!(:subject) { foo }
      #   subject(:subject) { foo }
      #   subject!(:subject) { foo }
      #
      #   # bad
      #   block = -> {}
      #   let(:subject, &block)
      #
      #   # good
      #   subject(:test_subject) { foo }
      #
      class SubjectDeclaration < Base
        MSG_LET = 'Use subject explicitly rather than using let'
        MSG_REDUNDANT = 'Ambiguous declaration of subject'

        # @!method offensive_subject_declaration?(node)
        def_node_matcher :offensive_subject_declaration?, <<~PATTERN
          (send nil? ${#Subjects.all #Helpers.all} {(sym :subject) (str "subject")} ...)
        PATTERN

        def on_send(node)
          offense = offensive_subject_declaration?(node)
          return unless offense

          add_offense(node, message: message_for(offense))
        end

        private

        def message_for(offense)
          Helpers.all(offense) ? MSG_LET : MSG_REDUNDANT
        end
      end
    end
  end
end

Version data entries

14 entries across 12 versions & 2 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.12.1/lib/rubocop/cop/rspec/subject_declaration.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.9.0/lib/rubocop/cop/rspec/subject_declaration.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.12.1/lib/rubocop/cop/rspec/subject_declaration.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.9.0/lib/rubocop/cop/rspec/subject_declaration.rb
rubocop-rspec-2.12.1 lib/rubocop/cop/rspec/subject_declaration.rb
rubocop-rspec-2.12.0 lib/rubocop/cop/rspec/subject_declaration.rb
rubocop-rspec-2.11.1 lib/rubocop/cop/rspec/subject_declaration.rb
rubocop-rspec-2.11.0 lib/rubocop/cop/rspec/subject_declaration.rb
rubocop-rspec-2.10.0 lib/rubocop/cop/rspec/subject_declaration.rb
rubocop-rspec-2.9.0 lib/rubocop/cop/rspec/subject_declaration.rb
rubocop-rspec-2.8.0 lib/rubocop/cop/rspec/subject_declaration.rb
rubocop-rspec-2.7.0 lib/rubocop/cop/rspec/subject_declaration.rb
rubocop-rspec-2.6.0 lib/rubocop/cop/rspec/subject_declaration.rb
rubocop-rspec-2.5.0 lib/rubocop/cop/rspec/subject_declaration.rb