Sha256: 9e7043256228c0e7a3012c71f60d70f68d0bb1f9c7b9a3a8061fd48ead5a5fc0

Contents?: true

Size: 1.16 KB

Versions: 48

Compression:

Stored size: 1.16 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 str} #Subjects.all) ...)
        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

48 entries across 48 versions & 7 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.3/lib/rubocop/cop/rspec/subject_declaration.rb
rubocop-rspec-3.3.0 lib/rubocop/cop/rspec/subject_declaration.rb
rubocop-rspec-3.2.0 lib/rubocop/cop/rspec/subject_declaration.rb
rubocop-rspec-3.1.0 lib/rubocop/cop/rspec/subject_declaration.rb
rubocop-rspec-3.0.5 lib/rubocop/cop/rspec/subject_declaration.rb
rubocop-rspec-3.0.4 lib/rubocop/cop/rspec/subject_declaration.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/subject_declaration.rb
rubocop-rspec-3.0.3 lib/rubocop/cop/rspec/subject_declaration.rb
rubocop-rspec-3.0.2 lib/rubocop/cop/rspec/subject_declaration.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/subject_declaration.rb
rubocop-rspec-3.0.1 lib/rubocop/cop/rspec/subject_declaration.rb
rubocop-rspec-3.0.0 lib/rubocop/cop/rspec/subject_declaration.rb
rubocop-rspec-3.0.0.pre lib/rubocop/cop/rspec/subject_declaration.rb
rubocop-rspec-2.31.0 lib/rubocop/cop/rspec/subject_declaration.rb
rubocop-rspec-2.30.0 lib/rubocop/cop/rspec/subject_declaration.rb
rubocop-rspec-2.29.2 lib/rubocop/cop/rspec/subject_declaration.rb
rubocop-rspec-2.29.1 lib/rubocop/cop/rspec/subject_declaration.rb
rubocop-rspec-2.29.0 lib/rubocop/cop/rspec/subject_declaration.rb
rubocop-rspec-2.28.0 lib/rubocop/cop/rspec/subject_declaration.rb
rubocop-rspec-2.27.1 lib/rubocop/cop/rspec/subject_declaration.rb