class RuboCop::Cop::Highlands::PhraseBundleKeys

Prefer matching Phrase Bundle and t call keys inside of PhraseBundleClasses.

@example

# bad
def phrases
  {
    "shortened_key" => t(
      "my_real_translation_key",
      default: 'Does not matter',
    ),
  }
end

# good
def phrases
  {
    "my_real_translation_key" => t(
      "my_real_translation_key",
      default: 'Does not matter',
    ),
  }
end

Constants

MESSAGE

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/highlands/phrase_bundle_keys.rb, line 31
def on_send(node)
  parent = node.parent
  if t_call?(node) && in_phrase_bundle_class?(node) && parent.pair_type?
    hash_key = parent.children[0]
    unless hash_key.children[0] == node.children[2].children[0]
      add_offense(hash_key, message: MESSAGE)
    end
  end
end

Private Instance Methods

const_phrase_bundle_children(node) click to toggle source
# File lib/rubocop/cop/highlands/phrase_bundle_keys.rb, line 53
def const_phrase_bundle_children(node)
  node.children.select do |e|
    e.is_a?(Parser::AST::Node) &&
      e.const_type? &&
      e.children[1] == :PhraseBundle
  end
end
in_phrase_bundle_class?(node) click to toggle source
# File lib/rubocop/cop/highlands/phrase_bundle_keys.rb, line 43
def in_phrase_bundle_class?(node)
  if node.class_type? && !const_phrase_bundle_children(node).empty?
    true
  elsif node.parent
    in_phrase_bundle_class?(node.parent)
  else
    false
  end
end
t_call?(node) click to toggle source
# File lib/rubocop/cop/highlands/phrase_bundle_keys.rb, line 61
def t_call?(node)
  node.children[1] == :t
end