Sha256: 2e0978252cd0d66a86d44f13de7609f8051a9d25ea0e2863eaba8d4b840e8239

Contents?: true

Size: 1.96 KB

Versions: 42

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

module RuboCop
  module RSpec
    # RSpec example wording rewriter
    class Wording
      SHOULDNT_PREFIX    = /\Ashould(?:n't| not)\b/i.freeze
      SHOULDNT_BE_PREFIX = /#{SHOULDNT_PREFIX} be\b/i.freeze
      ES_SUFFIX_PATTERN  = /(?:o|s|x|ch|sh|z)\z/i.freeze
      IES_SUFFIX_PATTERN = /[^aeou]y\z/i.freeze

      def initialize(text, ignore:, replace:)
        @text         = text
        @ignores      = ignore
        @replacements = replace
      end

      def rewrite
        case text
        when SHOULDNT_BE_PREFIX
          replace_prefix(SHOULDNT_BE_PREFIX, 'is not')
        when SHOULDNT_PREFIX
          replace_prefix(SHOULDNT_PREFIX, 'does not')
        else
          remove_should_and_pluralize
        end
      end

      private

      attr_reader :text, :ignores, :replacements

      def replace_prefix(pattern, replacement)
        text.sub(pattern) do |shouldnt|
          uppercase?(shouldnt) ? replacement.upcase : replacement
        end
      end

      def uppercase?(word)
        word.upcase.eql?(word)
      end

      def remove_should_and_pluralize
        _should, *words = text.split

        words.each_with_index do |word, index|
          next if ignored_word?(word)

          words[index] = substitute(word)

          break
        end

        words.join(' ')
      end

      def ignored_word?(word)
        ignores.any? { |ignore| ignore.casecmp(word).zero? }
      end

      def substitute(word)
        # NOTE: Custom replacements are case sensitive.
        return replacements.fetch(word) if replacements.key?(word)

        case word
        when ES_SUFFIX_PATTERN  then append_suffix(word, 'es')
        when IES_SUFFIX_PATTERN then append_suffix(word[0..-2], 'ies')
        else append_suffix(word, 's')
        end
      end

      def append_suffix(word, suffix)
        suffix = suffix.upcase if uppercase?(word)

        "#{word}#{suffix}"
      end

      private_constant(*constants(false))
    end
  end
end

Version data entries

42 entries across 40 versions & 2 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.12.1/lib/rubocop/rspec/wording.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.9.0/lib/rubocop/rspec/wording.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.12.1/lib/rubocop/rspec/wording.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.9.0/lib/rubocop/rspec/wording.rb
rubocop-rspec-2.12.1 lib/rubocop/rspec/wording.rb
rubocop-rspec-2.12.0 lib/rubocop/rspec/wording.rb
rubocop-rspec-2.11.1 lib/rubocop/rspec/wording.rb
rubocop-rspec-2.11.0 lib/rubocop/rspec/wording.rb
rubocop-rspec-2.10.0 lib/rubocop/rspec/wording.rb
rubocop-rspec-2.9.0 lib/rubocop/rspec/wording.rb
rubocop-rspec-2.8.0 lib/rubocop/rspec/wording.rb
rubocop-rspec-2.7.0 lib/rubocop/rspec/wording.rb
rubocop-rspec-2.6.0 lib/rubocop/rspec/wording.rb
rubocop-rspec-2.5.0 lib/rubocop/rspec/wording.rb
rubocop-rspec-2.4.0 lib/rubocop/rspec/wording.rb
rubocop-rspec-2.3.0 lib/rubocop/rspec/wording.rb
rubocop-rspec-2.2.0 lib/rubocop/rspec/wording.rb
rubocop-rspec-2.1.0 lib/rubocop/rspec/wording.rb
rubocop-rspec-2.0.1 lib/rubocop/rspec/wording.rb
rubocop-rspec-2.0.0 lib/rubocop/rspec/wording.rb