This repository has been archived by the owner on Feb 19, 2024. It is now read-only.
forked from rubocop/rubocop
/
useless_access_modifier.rb
284 lines (253 loc) · 8.5 KB
/
useless_access_modifier.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# frozen_string_literal: true
module RuboCop
module Cop
module Lint
# This cop checks for redundant access modifiers, including those with no
# code, those which are repeated, and leading `public` modifiers in a
# class or module body. Conditionally-defined methods are considered as
# always being defined, and thus access modifiers guarding such methods
# are not redundant.
#
# This cop has `ContextCreatingMethods` option. The default setting value
# is an empty array that means no method is specified.
# This setting is an array of methods which, when called, are known to
# create its own context in the module's current access context.
#
# It also has `MethodCreatingMethods` option. The default setting value
# is an empty array that means no method is specified.
# This setting is an array of methods which, when called, are known to
# create other methods in the module's current access context.
#
# @example
# # bad
# class Foo
# public # this is redundant (default access is public)
#
# def method
# end
# end
#
# # bad
# class Foo
# # The following is redundant (methods defined on the class'
# # singleton class are not affected by the public modifier)
# public
#
# def self.method3
# end
# end
#
# # bad
# class Foo
# protected
#
# define_method(:method2) do
# end
#
# protected # this is redundant (repeated from previous modifier)
#
# [1,2,3].each do |i|
# define_method("foo#{i}") do
# end
# end
# end
#
# # bad
# class Foo
# private # this is redundant (no following methods are defined)
# end
#
# # good
# class Foo
# private # this is not redundant (a method is defined)
#
# def method2
# end
# end
#
# # good
# class Foo
# # The following is not redundant (conditionally defined methods are
# # considered as always defining a method)
# private
#
# if condition?
# def method
# end
# end
# end
#
# # good
# class Foo
# protected # this is not redundant (a method is defined)
#
# define_method(:method2) do
# end
# end
#
# @example ContextCreatingMethods: concerning
# # Lint/UselessAccessModifier:
# # ContextCreatingMethods:
# # - concerning
#
# # good
# require 'active_support/concern'
# class Foo
# concerning :Bar do
# def some_public_method
# end
#
# private
#
# def some_private_method
# end
# end
#
# # this is not redundant because `concerning` created its own context
# private
#
# def some_other_private_method
# end
# end
#
# @example MethodCreatingMethods: delegate
# # Lint/UselessAccessModifier:
# # MethodCreatingMethods:
# # - delegate
#
# # good
# require 'active_support/core_ext/module/delegation'
# class Foo
# # this is not redundant because `delegate` creates methods
# private
#
# delegate :method_a, to: :method_b
# end
class UselessAccessModifier < Cop
include RangeHelp
MSG = 'Useless `%<current>s` access modifier.'
def on_class(node)
check_node(node.children[2]) # class body
end
def on_module(node)
check_node(node.children[1]) # module body
end
def on_block(node)
return unless eval_call?(node)
check_node(node.body)
end
def on_sclass(node)
check_node(node.children[1]) # singleton class body
end
def autocorrect(node)
lambda do |corrector|
range = range_by_whole_lines(
node.source_range, include_final_newline: true
)
corrector.remove(range)
end
end
private
def_node_matcher :static_method_definition?, <<~PATTERN
{def (send nil? {:attr :attr_reader :attr_writer :attr_accessor} ...)}
PATTERN
def_node_matcher :dynamic_method_definition?, <<~PATTERN
{(send nil? :define_method ...) (block (send nil? :define_method ...) ...)}
PATTERN
def_node_matcher :class_or_instance_eval?, <<~PATTERN
(block (send _ {:class_eval :instance_eval}) ...)
PATTERN
def_node_matcher :class_or_module_or_struct_new_call?, <<~PATTERN
(block (send (const {nil? cbase} {:Class :Module :Struct}) :new ...) ...)
PATTERN
def check_node(node)
return if node.nil?
if node.begin_type?
check_scope(node)
elsif node.send_type? && node.bare_access_modifier?
add_offense(node, message: format(MSG, current: node.method_name))
end
end
def access_modifier?(node)
node.bare_access_modifier? ||
node.method?(:private_class_method)
end
def check_scope(node)
cur_vis, unused = check_child_nodes(node, nil, :public)
add_offense(unused, message: format(MSG, current: cur_vis)) if unused
end
def check_child_nodes(node, unused, cur_vis)
node.child_nodes.each do |child|
if child.send_type? && access_modifier?(child)
cur_vis, unused = check_send_node(child, cur_vis, unused)
elsif method_definition?(child)
unused = nil
elsif start_of_new_scope?(child)
check_scope(child)
elsif !child.defs_type?
cur_vis, unused = check_child_nodes(child, unused, cur_vis)
end
end
[cur_vis, unused]
end
def check_send_node(node, cur_vis, unused)
if node.bare_access_modifier?
check_new_visibility(node, unused, node.method_name, cur_vis)
elsif node.method?(:private_class_method) && !node.arguments?
add_offense(node, message: format(MSG, current: node.method_name))
[cur_vis, unused]
end
end
def check_new_visibility(node, unused, new_vis, cur_vis)
# does this modifier just repeat the existing visibility?
if new_vis == cur_vis
add_offense(node, message: format(MSG, current: cur_vis))
else
# was the previous modifier never applied to any defs?
add_offense(unused, message: format(MSG, current: cur_vis)) if unused
# once we have already warned about a certain modifier, don't
# warn again even if it is never applied to any method defs
unused = node
end
[new_vis, unused]
end
def method_definition?(child)
static_method_definition?(child) ||
dynamic_method_definition?(child) ||
any_method_definition?(child)
end
def any_method_definition?(child)
cop_config.fetch('MethodCreatingMethods', []).any? do |m|
matcher_name = "#{m}_method?".to_sym
unless respond_to?(matcher_name)
self.class.def_node_matcher matcher_name, <<~PATTERN
{def (send nil? :#{m} ...)}
PATTERN
end
send(matcher_name, child)
end
end
def start_of_new_scope?(child)
child.module_type? || child.class_type? ||
child.sclass_type? || eval_call?(child)
end
def eval_call?(child)
class_or_instance_eval?(child) ||
class_or_module_or_struct_new_call?(child) ||
any_context_creating_methods?(child)
end
def any_context_creating_methods?(child)
cop_config.fetch('ContextCreatingMethods', []).any? do |m|
matcher_name = "#{m}_block?".to_sym
unless respond_to?(matcher_name)
self.class.def_node_matcher matcher_name, <<~PATTERN
(block (send {nil? const} {:#{m}} ...) ...)
PATTERN
end
send(matcher_name, child)
end
end
end
end
end
end