forked from puppetlabs/rubocop-i18n
/
decorate_string.rb
176 lines (157 loc) · 5.09 KB
/
decorate_string.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# frozen_string_literal: true
module RuboCop
module Cop
module I18n
module RailsI18n
# This cop is looks for strings that appear to be sentences but are not decorated.
# Sentences are determined by the SENTENCE_REGEXP. (Upper case character, at least one space,
# and sentence punctuation at the end)
#
# @example
#
# # bad
#
# "Result is bad."
#
# @example
#
# # good
#
# t("result_is_good")
# I18n.t("result_is_good")
#
# There are several options for configuration.
#
# @example IgnoreExceptions: true
# # OK
#
# raise "Some string sentence"
#
# @example EnforcedSentenceType: sentence
# # bad
#
# "Result is bad."
#
# # good
#
# t("result_is_good")
# I18n.t("result_is_good")
#
# @example EnforcedSentenceType: fragmented_sentence
# # bad
#
# "Result is bad" # Contains a capital to start
# "result is bad." # Ends in punctuation
#
# # good
#
# t("result_is_good")
# I18n.t("result_is_good")
#
# @example EnforcedSentenceType: fragment
# # bad
#
# "result is bad" # A series of words
#
# # good
#
# t("result_is_good")
# I18n.t("result_is_good")
#
# @example Regexp: ^only-this-text$
#
# # bad
#
# "only-this-text"
#
# # good
#
# "Any other string is fine now"
# t("only_this_text")
#
class DecorateString < Base
SENTENCE_REGEXP = /^\s*[[:upper:]][[:alpha:]]*[[:blank:]]+.*[.!?]$/.freeze
FRAGMENTED_SENTENCE_REGEXP = /^\s*([[:upper:]][[:alpha:]]*[[:blank:]]+.*)|([[:alpha:]]*[[:blank:]]+.*[.!?])$/.freeze
FRAGMENT_REGEXP = /^\s*[[:alpha:]]*[[:blank:]]+.*$/.freeze
SUPPORTED_DECORATORS = %w[
t
t!
translate
translate!
].freeze
def on_dstr(node)
check_for_parent_decorator(node) if dstr_contains_sentence?(node)
end
def on_str(node)
return unless sentence?(node)
parent = node.parent
if parent.respond_to?(:type)
return if parent.regexp_type? || parent.dstr_type?
end
check_for_parent_decorator(node)
end
private
def sentence?(node)
child = node.children[0]
if child.is_a?(String)
if child.valid_encoding?
child.encode(Encoding::UTF_8).chomp =~ regexp
else
false
end
elsif child.respond_to?(:type) && child.str_type?
sentence?(child)
else
false
end
end
def regexp
@regexp ||= regexp_from_config || regexp_from_string_type
end
def regexp_from_string_type
case cop_config['EnforcedSentenceType'].to_s.downcase
when 'sentence' then SENTENCE_REGEXP
when 'fragmented_sentence' then FRAGMENTED_SENTENCE_REGEXP
when 'fragment' then FRAGMENT_REGEXP
else # rubocop:disable Lint/DuplicateBranch
SENTENCE_REGEXP
end
end
def regexp_from_config
Regexp.new(cop_config['Regexp']) if cop_config['Regexp']
end
def dstr_contains_sentence?(node)
node.children.any? { |child| sentence?(child) }
end
def check_for_parent_decorator(node)
return if parent_is_translator?(node.parent)
return if parent_is_indexer?(node.parent)
return if ignoring_raised_parent?(node.parent)
add_offense(node, message: 'decorator is missing around sentence')
end
def ignoring_raised_parent?(parent)
return false unless cop_config['IgnoreExceptions']
return true if parent.respond_to?(:method_name) && %i[raise fail].include?(parent.method_name)
# Commonly exceptions are initialized manually.
return ignoring_raised_parent?(parent.parent) if parent.respond_to?(:method_name) && parent.method?(:new)
false
end
def parent_is_indexer?(parent)
parent.respond_to?(:method_name) && parent.method?(:[])
end
def parent_is_translator?(parent)
if parent.respond_to?(:type) && parent.send_type?
method_name = parent.loc.selector.source
if RailsI18n.supported_decorator?(method_name)
# Implicit receiver is assumed correct.
return true if parent.receiver.nil?
return true if parent.receiver.children == [nil, :I18n]
end
end
false
end
end
end
end
end
end