class RuboCop::Cop::I18n::GetText::DecorateString
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)
@example
# bad "Result is bad."
@example
# good _("Result is good.")
Constants
- STRING_REGEXP
Public Instance Methods
autocorrect(node)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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
Private Instance Methods
check_for_parent_decorator(node)
click to toggle source
# File lib/rubocop/cop/i18n/gettext/decorate_string.rb, line 65 def check_for_parent_decorator(node) parent = node.parent if parent.respond_to?(:type) && parent.send_type? method_name = parent.loc.selector.source return if GetText.supported_decorator?(method_name) elsif parent.respond_to?(:method_name) && parent.method?(:[]) return end add_offense(node, message: 'decorator is missing around sentence') end
dstr_contains_sentence?(node)
click to toggle source
# File lib/rubocop/cop/i18n/gettext/decorate_string.rb, line 61 def dstr_contains_sentence?(node) node.children.any? { |child| sentence?(child) } end
sentence?(node)
click to toggle source
# File lib/rubocop/cop/i18n/gettext/decorate_string.rb, line 46 def sentence?(node) child = node.children[0] if child.is_a?(String) if child.valid_encoding? child.encode(Encoding::UTF_8).chomp =~ STRING_REGEXP else false end elsif child.respond_to?(:type) && child.str_type? sentence?(child) else false end end
single_string_correct(node)
click to toggle source
# File lib/rubocop/cop/i18n/gettext/decorate_string.rb, line 76 def single_string_correct(node) lambda { |corrector| corrector.insert_before(node.source_range, '_(') corrector.insert_after(node.source_range, ')') } end