forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi18n_locale_texts.rb
114 lines (106 loc) · 3.01 KB
/
i18n_locale_texts.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# frozen_string_literal: true
module RuboCop
module Cop
module Rails
# Enforces use of I18n and locale files instead of locale specific strings.
#
# @example
# # bad
# class User < ApplicationRecord
# validates :email, presence: { message: "must be present" }
# end
#
# # good
# # config/locales/en.yml
# # en:
# # activerecord:
# # errors:
# # models:
# # user:
# # blank: "must be present"
#
# class User < ApplicationRecord
# validates :email, presence: true
# end
#
# # bad
# class PostsController < ApplicationController
# def create
# # ...
# redirect_to root_path, notice: "Post created!"
# end
# end
#
# # good
# # config/locales/en.yml
# # en:
# # posts:
# # create:
# # success: "Post created!"
#
# class PostsController < ApplicationController
# def create
# # ...
# redirect_to root_path, notice: t(".success")
# end
# end
#
# # bad
# class UserMailer < ApplicationMailer
# def welcome(user)
# mail(to: user.email, subject: "Welcome to My Awesome Site")
# end
# end
#
# # good
# # config/locales/en.yml
# # en:
# # user_mailer:
# # welcome:
# # subject: "Welcome to My Awesome Site"
#
# class UserMailer < ApplicationMailer
# def welcome(user)
# mail(to: user.email)
# end
# end
#
class I18nLocaleTexts < Base
MSG = 'Move locale texts to the locale files in the `config/locales` directory.'
RESTRICT_ON_SEND = %i[validates redirect_to redirect_back []= mail].freeze
def_node_search :validation_message, <<~PATTERN
(pair (sym :message) $str)
PATTERN
def_node_search :redirect_to_flash, <<~PATTERN
(pair (sym {:notice :alert}) $str)
PATTERN
def_node_matcher :flash_assignment?, <<~PATTERN
(send
{
(send nil? :flash)
(send (send nil? :flash) :now)
} :[]= _ $str)
PATTERN
def_node_search :mail_subject, <<~PATTERN
(pair (sym :subject) $str)
PATTERN
def on_send(node)
case node.method_name
when :validates
validation_message(node) do |text_node|
add_offense(text_node)
end
return
when :redirect_to, :redirect_back
text_node = redirect_to_flash(node).to_a.last
when :[]=
text_node = flash_assignment?(node)
when :mail
text_node = mail_subject(node).to_a.last
end
add_offense(text_node) if text_node
end
end
end
end
end