@@ -5,7 +5,13 @@ module Cop
55 module Rails
66 # Checks for places where I18n "lazy" lookup can be used.
77 #
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)
915 # # en.yml
1016 # # en:
1117 # # books:
@@ -28,11 +34,29 @@ module Rails
2834 # end
2935 # end
3036 #
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+ #
3154 class I18nLazyLookup < Base
55+ include ConfigurableEnforcedStyle
3256 include VisibilityHelp
3357 extend AutoCorrector
3458
35- MSG = 'Use "lazy" lookup for the text used in controllers.'
59+ MSG = 'Use %<style>s lookup for the text used in controllers.'
3660
3761 RESTRICT_ON_SEND = %i[ translate t ] . freeze
3862
@@ -42,23 +66,45 @@ class I18nLazyLookup < Base
4266
4367 def on_send ( node )
4468 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
4779
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? ( '.' )
5083
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
5386
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 } '" )
5893 end
5994 end
6095
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
62108
63109 def controller_and_action ( node )
64110 action_node = node . each_ancestor ( :def ) . first
@@ -90,6 +136,10 @@ def controller_path(controller)
90136
91137 path . delete_suffix ( 'Controller' ) . underscore
92138 end
139+
140+ def message ( _range )
141+ format ( MSG , style : style )
142+ end
93143 end
94144 end
95145 end
0 commit comments