Class: RuboCop::Cop::I18n::GetText::DecorateString

Inherits:
RuboCop::Cop
  • Object
show all
Defined in:
lib/rubocop/cop/i18n/gettext/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

_("Result is good.")

Constant Summary collapse

STRING_REGEXP =
/^\s*[[:upper:]][[:alpha:]]*[[:blank:]]+.*[.!?]$/.freeze

Instance Method Summary collapse

Instance Method Details

#autocorrect(node) ⇒ Object



40
41
42
# File 'lib/rubocop/cop/i18n/gettext/decorate_string.rb', line 40

def autocorrect(node)
  single_string_correct(node) if node.str_type?
end

#on_dstr(node) ⇒ Object



25
26
27
# File 'lib/rubocop/cop/i18n/gettext/decorate_string.rb', line 25

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

#on_str(node) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/rubocop/cop/i18n/gettext/decorate_string.rb', line 29

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