@@ -5,7 +5,13 @@ module Cop
5
5
module Rails
6
6
# Checks for places where I18n "lazy" lookup can be used.
7
7
#
8
- # @example
8
+ # This cop has two different enforcement modes. When the EnforcedStyle
9
+ # is `lazy` (the default), explicit lookups are added as offenses.
10
+ #
11
+ # When the EnforcedStyle is `explicit` then lazy lookups are added as
12
+ # offenses.
13
+ #
14
+ # @example EnforcedStyle: lazy (default)
9
15
# # en.yml
10
16
# # en:
11
17
# # books:
@@ -28,11 +34,29 @@ module Rails
28
34
# end
29
35
# end
30
36
#
37
+ # @example EnforcedStyle: explicit
38
+ # # bad
39
+ # class BooksController < ApplicationController
40
+ # def create
41
+ # # ...
42
+ # redirect_to books_url, notice: t('.success')
43
+ # end
44
+ # end
45
+ #
46
+ # # good
47
+ # class BooksController < ApplicationController
48
+ # def create
49
+ # # ...
50
+ # redirect_to books_url, notice: t('books.create.success')
51
+ # end
52
+ # end
53
+ #
31
54
class I18nLazyLookup < Base
55
+ include ConfigurableEnforcedStyle
32
56
include VisibilityHelp
33
57
extend AutoCorrector
34
58
35
- MSG = 'Use "lazy" lookup for the text used in controllers.'
59
+ MSG = 'Use %<style>s lookup for the text used in controllers.'
36
60
37
61
RESTRICT_ON_SEND = %i[ translate t ] . freeze
38
62
@@ -42,23 +66,45 @@ class I18nLazyLookup < Base
42
66
43
67
def on_send ( node )
44
68
translate_call? ( node ) do |key_node |
45
- key = key_node . value
46
- return if key . to_s . start_with? ( '.' )
69
+ case style
70
+ when :lazy
71
+ handle_lazy_style ( node , key_node )
72
+ when :explicit
73
+ handle_explicit_style ( node , key_node )
74
+ end
75
+ end
76
+ end
77
+
78
+ private
47
79
48
- controller , action = controller_and_action ( node )
49
- return unless controller && action
80
+ def handle_lazy_style ( node , key_node )
81
+ key = key_node . value
82
+ return if key . to_s . start_with? ( '.' )
50
83
51
- scoped_key = get_scoped_key ( key_node , controller , action )
52
- return unless key == scoped_key
84
+ controller , action = controller_and_action ( node )
85
+ return unless controller && action
53
86
54
- add_offense ( key_node ) do |corrector |
55
- unscoped_key = key_node . value . to_s . split ( '.' ) . last
56
- corrector . replace ( key_node , "'.#{ unscoped_key } '" )
57
- end
87
+ scoped_key = get_scoped_key ( key_node , controller , action )
88
+ return unless key == scoped_key
89
+
90
+ add_offense ( key_node ) do |corrector |
91
+ unscoped_key = key_node . value . to_s . split ( '.' ) . last
92
+ corrector . replace ( key_node , "'.#{ unscoped_key } '" )
58
93
end
59
94
end
60
95
61
- private
96
+ def handle_explicit_style ( node , key_node )
97
+ key = key_node . value
98
+ return unless key . to_s . start_with? ( '.' )
99
+
100
+ controller , action = controller_and_action ( node )
101
+ return unless controller && action
102
+
103
+ scoped_key = get_scoped_key ( key_node , controller , action )
104
+ add_offense ( key_node ) do |corrector |
105
+ corrector . replace ( key_node , "'#{ scoped_key } '" )
106
+ end
107
+ end
62
108
63
109
def controller_and_action ( node )
64
110
action_node = node . each_ancestor ( :def ) . first
@@ -90,6 +136,10 @@ def controller_path(controller)
90
136
91
137
path . delete_suffix ( 'Controller' ) . underscore
92
138
end
139
+
140
+ def message ( _range )
141
+ format ( MSG , style : style )
142
+ end
93
143
end
94
144
end
95
145
end
0 commit comments