Class: RuboCop::Cop::Airbnb::PhraseBundleKeys

Inherits:
RuboCop::Cop
  • Object
show all
Defined in:
lib/rubocop/cop/airbnb/phrase_bundle_keys.rb

Overview

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

Examples:

# 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

Constant Summary collapse

MESSAGE =
'Phrase bundle keys should match their translation keys.'.freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/rubocop/cop/airbnb/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