Class: RuboCop::Cop::I18n::RailsI18n::DecorateString

Inherits:
RuboCop::Cop
  • Object
show all
Defined in:
lib/rubocop/cop/i18n/rails_i18n/decorate_string.rb

Overview

This cop is looks for strings that appear to be sentences but are not decorated. Sentences are determined by the SENTENCE_REGEXP. (Upper case character, at least one space, and sentence punctuation at the end)

There are several options for configuration.

Examples:


# bad

"Result is bad."

# good

t("result_is_good")
I18n.t("result_is_good")

IgnoreExceptions: true

# OK

raise "Some string sentence"

EnforcedSentenceType: sentence

# bad

"Result is bad."

# good

t("result_is_good")
I18n.t("result_is_good")

EnforcedSentenceType: fragmented_sentence

# bad

"Result is bad"   # Contains a capital to start
"result is bad."  # Ends in punctuation

# good

t("result_is_good")
I18n.t("result_is_good")

EnforcedSentenceType: fragment

# bad

"result is bad"   # A series of words

# good

t("result_is_good")
I18n.t("result_is_good")

Regexp: ^only-this-text$


# bad

"only-this-text"

# good

"Any other string is fine now"
t("only_this_text")

Constant Summary collapse

SENTENCE_REGEXP =
/^\s*[[:upper:]][[:alpha:]]*[[:blank:]]+.*[.!?]$/.freeze
FRAGMENTED_SENTENCE_REGEXP =
/^\s*([[:upper:]][[:alpha:]]*[[:blank:]]+.*)|([[:alpha:]]*[[:blank:]]+.*[.!?])$/.freeze
FRAGMENT_REGEXP =
/^\s*[[:alpha:]]*[[:blank:]]+.*$/.freeze
SUPPORTED_DECORATORS =
%w[
  t
  t!
  translate
  translate!
].freeze

Instance Method Summary collapse

Instance Method Details

#on_dstr(node) ⇒ Object



84
85
86
# File 'lib/rubocop/cop/i18n/rails_i18n/decorate_string.rb', line 84

def on_dstr(node)
  check_for_parent_decorator(node) if dstr_contains_sentence?(node)
end

#on_str(node) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/rubocop/cop/i18n/rails_i18n/decorate_string.rb', line 88

def on_str(node)
  return unless sentence?(node)

  parent = node.parent
  if parent.respond_to?(:type)
    return if parent.regexp_type? || parent.dstr_type?
  end

  check_for_parent_decorator(node)
end