Sha256: 27af511473f7f18b883d761e24d1cd90bf9caf1b0a7285ef3d9c019af5b3cc06

Contents?: true

Size: 1.92 KB

Versions: 12

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Checks for usage of implicit subject (`is_expected` / `should`).
      #
      # This cop can be configured using the `EnforcedStyle` option
      #
      # @example `EnforcedStyle: single_line_only`
      #   # bad
      #   it do
      #     is_expected.to be_truthy
      #   end
      #
      #   # good
      #   it { is_expected.to be_truthy }
      #   it do
      #     expect(subject).to be_truthy
      #   end
      #
      # @example `EnforcedStyle: disallow`
      #   # bad
      #   it { is_expected.to be_truthy }
      #
      #   # good
      #   it { expect(subject).to be_truthy }
      #
      class ImplicitSubject < Cop
        include ConfigurableEnforcedStyle

        MSG = "Don't use implicit subject."

        def_node_matcher :implicit_subject?, <<-PATTERN
          (send nil? {:should :should_not :is_expected} ...)
        PATTERN

        def on_send(node)
          return unless implicit_subject?(node)
          return if valid_usage?(node)

          add_offense(node)
        end

        def autocorrect(node)
          replacement = 'expect(subject)'
          if node.method_name == :should
            replacement += '.to'
          elsif node.method_name == :should_not
            replacement += '.not_to'
          end

          ->(corrector) { corrector.replace(node.loc.selector, replacement) }
        end

        private

        def valid_usage?(node)
          example = node.ancestors.find { |parent| example?(parent) }
          return false if example.nil?

          example.method_name == :its || allowed_by_style?(example)
        end

        def allowed_by_style?(example)
          if style == :single_line_only
            example.single_line?
          elsif style == :single_statement_only
            !example.body.begin_type?
          else
            false
          end
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
rubocop-rspec-1.41.0 lib/rubocop/cop/rspec/implicit_subject.rb
rubocop-rspec-1.40.0 lib/rubocop/cop/rspec/implicit_subject.rb
rubocop-rspec-1.39.0 lib/rubocop/cop/rspec/implicit_subject.rb
rubocop-rspec-1.38.1 lib/rubocop/cop/rspec/implicit_subject.rb
rubocop-rspec-1.38.0 lib/rubocop/cop/rspec/implicit_subject.rb
rubocop-rspec-1.37.1 lib/rubocop/cop/rspec/implicit_subject.rb
rubocop-rspec-1.37.0 lib/rubocop/cop/rspec/implicit_subject.rb
rubocop-rspec-1.36.0 lib/rubocop/cop/rspec/implicit_subject.rb
rubocop-rspec-1.35.0 lib/rubocop/cop/rspec/implicit_subject.rb
rubocop-rspec-1.34.1 lib/rubocop/cop/rspec/implicit_subject.rb
rubocop-rspec-1.34.0 lib/rubocop/cop/rspec/implicit_subject.rb
rubocop-rspec-1.33.0 lib/rubocop/cop/rspec/implicit_subject.rb