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

Inherits:
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 STRING_REGEXP. (Upper case character, at least one space, and sentence punctuation at the end)

Examples:


# bad

"Result is bad."

# good

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

Constant Summary collapse

STRING_REGEXP =
/^\s*[[:upper:]][[:alpha:]]*[[:blank:]]+.*[.!?]$/.freeze
SUPPORTED_DECORATORS =
%w[
  t
  t!
  translate
  translate!
].freeze

Instance Method Summary collapse

Methods inherited from Cop

#highlights, #messages

Instance Method Details

#on_dstr(node) ⇒ Object



33
34
35
# File 'lib/rubocop/cop/i18n/rails_i18n/decorate_string.rb', line 33

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

#on_str(node) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/rubocop/cop/i18n/rails_i18n/decorate_string.rb', line 37

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