Sha256: eac89029cc83f4dc0403efd4f9e347a16215771dd4cf89a9586abf7bbcab04ec

Contents?: true

Size: 1.84 KB

Versions: 2

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Checks that example descriptions do not start with "should".
      #
      # @see http://betterspecs.org/#should
      #
      # The autocorrect is experimental - use with care! It can be configured
      # with CustomTransform (e.g. have => has) and IgnoredWords (e.g. only).
      #
      # @example
      #   # bad
      #   it 'should find nothing' do
      #   end
      #
      #   # good
      #   it 'finds nothing' do
      #   end
      class ExampleWording < Cop
        include RuboCop::RSpec::SpecOnly

        MSG = 'Do not use should when describing your tests.'.freeze

        def on_block(node) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Metrics/LineLength
          method, = *node
          _, method_name, *args = *method

          return unless method_name.equal?(:it)

          arguments = args.first.to_a
          message = arguments.first.to_s
          return unless message.downcase.start_with?('should')

          arg1 = args.first.loc.expression
          message = Parser::Source::Range.new(arg1.source_buffer,
                                              arg1.begin_pos + 1,
                                              arg1.end_pos - 1)

          add_offense(message, message)
        end

        def autocorrect(range)
          lambda do |corrector|
            corrector.replace(
              range,
              RuboCop::RSpec::Wording.new(
                range.source,
                ignore: ignored_words,
                replace: custom_transform
              ).rewrite
            )
          end
        end

        private

        def custom_transform
          cop_config.fetch('CustomTransform', {})
        end

        def ignored_words
          cop_config.fetch('IgnoredWords', [])
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-rspec-1.8.0 lib/rubocop/cop/rspec/example_wording.rb
rubocop-rspec-1.7.0 lib/rubocop/cop/rspec/example_wording.rb