lib/rubocop/rspec/wording.rb in rubocop-rspec-1.15.0 vs lib/rubocop/rspec/wording.rb in rubocop-rspec-1.15.1

- old
+ new

@@ -2,46 +2,80 @@ module RuboCop module RSpec # RSpec example wording rewriter class Wording + SHOULDNT_PREFIX = /\Ashould(?:n't| not)\b/i + SHOULDNT_BE_PREFIX = /#{SHOULDNT_PREFIX} be\b/i + ES_SUFFIX_PATTERN = /(?:o|s|x|ch|sh|z)\z/i + IES_SUFFIX_PATTERN = /[^aeou]y\z/i + def initialize(text, ignore:, replace:) @text = text @ignores = ignore @replacements = replace end def rewrite - text.split.tap do |words| - first_word = words.shift - words.unshift('not') if first_word.eql?("shouldn't") - - words.each_with_index do |value, key| - next if ignores.include?(value) - words[key] = simple_present(words.fetch(key)) - break - end - end.join(' ') + 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 simple_present(word) - return replacements.fetch(word) if replacements.key?(word) + def replace_prefix(pattern, replacement) + text.sub(pattern) do |shouldnt| + uppercase?(shouldnt) ? replacement.upcase : replacement + end + end - # ends with o s x ch sh or ss - if %w(o s x ch sh).any?(&word.public_method(:end_with?)) - return "#{word}es" + 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 - # ends with y - if word.end_with?('y') && !%w(a u o e).include?(word[-2]) - return "#{word[0..-2]}ies" + 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 - "#{word}s" + def append_suffix(word, suffix) + suffix = suffix.upcase if uppercase?(word) + + "#{word}#{suffix}" end + + private_constant(*constants(false)) end end end