/
phrase_bundle_keys.rb
64 lines (60 loc) · 1.63 KB
/
phrase_bundle_keys.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
module RuboCop
module Cop
module Airbnb
# 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
class PhraseBundleKeys < Base
MESSAGE =
'Phrase bundle keys should match their translation keys.'.freeze
RESTRICT_ON_SEND = %i(t).freeze
def on_send(node)
parent = node.parent
if 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
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
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
end
end
end
end