forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dot_separated_keys.rb
71 lines (59 loc) · 2.18 KB
/
dot_separated_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
65
66
67
68
69
70
71
# frozen_string_literal: true
module RuboCop
module Cop
module Rails
# Enforces the use of dot-separated locale keys instead of specifying the `:scope` option
# with an array or a single symbol in `I18n` translation methods.
# Dot-separated notation is easier to read and trace the hierarchy.
#
# @example
# # bad
# I18n.t :record_invalid, scope: [:activerecord, :errors, :messages]
# I18n.t :title, scope: :invitation
#
# # good
# I18n.t 'activerecord.errors.messages.record_invalid'
# I18n.t :record_invalid, scope: 'activerecord.errors.messages'
#
class DotSeparatedKeys < Base
include RangeHelp
extend AutoCorrector
MSG = 'Use the dot-separated keys instead of specifying the `:scope` option.'
TRANSLATE_METHODS = %i[translate t].freeze
def_node_matcher :translate_with_scope?, <<~PATTERN
(send {nil? (const {nil? cbase} :I18n)} {:translate :t} ${sym_type? str_type?}
(hash <$(pair (sym :scope) ${array_type? sym_type?}) ...>)
)
PATTERN
def on_send(node)
return unless TRANSLATE_METHODS.include?(node.method_name)
translate_with_scope?(node) do |key_node, scope_node|
return unless should_convert_scope?(scope_node)
add_offense(scope_node) do |corrector|
# Eat the comma on the left.
range = range_with_surrounding_space(scope_node.source_range, side: :left)
range = range_with_surrounding_comma(range, :left)
corrector.remove(range)
corrector.replace(key_node, new_key(key_node, scope_node))
end
end
end
private
def should_convert_scope?(scope_node)
scopes(scope_node).all?(&:basic_literal?)
end
def new_key(key_node, scope_node)
"'#{scopes(scope_node).map(&:value).join('.')}.#{key_node.value}'".squeeze('.')
end
def scopes(scope_node)
value = scope_node.value
if value.array_type?
value.values
else
[value]
end
end
end
end
end
end