-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
trivial_accessors.rb
254 lines (220 loc) · 6.41 KB
/
trivial_accessors.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# frozen_string_literal: true
module RuboCop
module Cop
module Style
# Looks for trivial reader/writer methods, that could
# have been created with the attr_* family of functions automatically.
# `to_ary`, `to_a`, `to_c`, `to_enum`, `to_h`, `to_hash`, `to_i`, `to_int`, `to_io`,
# `to_open`, `to_path`, `to_proc`, `to_r`, `to_regexp`, `to_str`, `to_s`, and `to_sym` methods
# are allowed by default. These are customizable with `AllowedMethods` option.
#
# @example
# # bad
# def foo
# @foo
# end
#
# def bar=(val)
# @bar = val
# end
#
# def self.baz
# @baz
# end
#
# # good
# attr_reader :foo
# attr_writer :bar
#
# class << self
# attr_reader :baz
# end
#
# @example ExactNameMatch: true (default)
# # good
# def name
# @other_name
# end
#
# @example ExactNameMatch: false
# # bad
# def name
# @other_name
# end
#
# @example AllowPredicates: true (default)
# # good
# def foo?
# @foo
# end
#
# @example AllowPredicates: false
# # bad
# def foo?
# @foo
# end
#
# # good
# attr_reader :foo
#
# @example AllowDSLWriters: true (default)
# # good
# def on_exception(action)
# @on_exception=action
# end
#
# @example AllowDSLWriters: false
# # bad
# def on_exception(action)
# @on_exception=action
# end
#
# # good
# attr_writer :on_exception
#
# @example IgnoreClassMethods: false (default)
# # bad
# def self.foo
# @foo
# end
#
# # good
# class << self
# attr_reader :foo
# end
#
# @example IgnoreClassMethods: true
# # good
# def self.foo
# @foo
# end
#
# @example AllowedMethods: ['allowed_method']
# # good
# def allowed_method
# @foo
# end
class TrivialAccessors < Base
include AllowedMethods
extend AutoCorrector
MSG = 'Use `attr_%<kind>s` to define trivial %<kind>s methods.'
def on_def(node)
return if top_level_node?(node)
return if in_module_or_instance_eval?(node)
return if ignore_class_methods? && node.defs_type?
on_method_def(node)
end
alias on_defs on_def
private
def in_module_or_instance_eval?(node)
node.each_ancestor(:block, :class, :sclass, :module).each do |pnode|
case pnode.type
when :class, :sclass
return false
when :module
return true
else
return true if pnode.method?(:instance_eval)
end
end
false
end
def on_method_def(node)
kind = if trivial_reader?(node)
'reader'
elsif trivial_writer?(node)
'writer'
end
return unless kind
add_offense(node.loc.keyword, message: format(MSG, kind: kind)) do |corrector|
autocorrect(corrector, node)
end
end
def autocorrect(corrector, node)
parent = node.parent
return if parent&.send_type?
if node.def_type?
autocorrect_instance(corrector, node)
elsif node.defs_type? && node.children.first.self_type?
autocorrect_class(corrector, node)
end
end
def exact_name_match?
cop_config['ExactNameMatch']
end
def allow_predicates?
cop_config['AllowPredicates']
end
def allow_dsl_writers?
cop_config['AllowDSLWriters']
end
def ignore_class_methods?
cop_config['IgnoreClassMethods']
end
def allowed_method_names
allowed_methods.map(&:to_sym) + [:initialize]
end
def dsl_writer?(node)
!node.assignment_method?
end
def trivial_reader?(node)
looks_like_trivial_reader?(node) && !allowed_method_name?(node) && !allowed_reader?(node)
end
def looks_like_trivial_reader?(node)
!node.arguments? && node.body && node.body.ivar_type?
end
def trivial_writer?(node)
looks_like_trivial_writer?(node) && !allowed_method_name?(node) && !allowed_writer?(node)
end
# @!method looks_like_trivial_writer?(node)
def_node_matcher :looks_like_trivial_writer?, <<~PATTERN
{(def _ (args (arg ...)) (ivasgn _ (lvar _)))
(defs _ _ (args (arg ...)) (ivasgn _ (lvar _)))}
PATTERN
def allowed_method_name?(node)
allowed_method_names.include?(node.method_name) ||
(exact_name_match? && !names_match?(node))
end
def allowed_writer?(node)
allow_dsl_writers? && dsl_writer?(node)
end
def allowed_reader?(node)
allow_predicates? && node.predicate_method?
end
def names_match?(node)
ivar_name, = *node.body
node.method_name.to_s.sub(/[=?]$/, '') == ivar_name[1..]
end
def trivial_accessor_kind(node)
if trivial_writer?(node) && !dsl_writer?(node)
'writer'
elsif trivial_reader?(node)
'reader'
end
end
def accessor(kind, method_name)
"attr_#{kind} :#{method_name.to_s.chomp('=')}"
end
def autocorrect_instance(corrector, node)
kind = trivial_accessor_kind(node)
return unless names_match?(node) && !node.predicate_method? && kind
corrector.replace(node, accessor(kind, node.method_name))
end
def autocorrect_class(corrector, node)
kind = trivial_accessor_kind(node)
return unless names_match?(node) && kind
indent = ' ' * node.loc.column
corrector.replace(
node,
['class << self',
"#{indent} #{accessor(kind, node.method_name)}",
"#{indent}end"].join("\n")
)
end
def top_level_node?(node)
node.parent.nil?
end
end
end
end
end