forked from rubocop/rubocop
/
memoized_instance_variable_name.rb
171 lines (154 loc) · 5.07 KB
/
memoized_instance_variable_name.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
# frozen_string_literal: true
module RuboCop
module Cop
module Naming
# This cop checks for memoized methods whose instance variable name
# does not match the method name.
#
# This cop can be configured with the EnforcedStyleForLeadingUnderscores
# directive. It can be configured to allow for memoized instance variables
# prefixed with an underscore. Prefixing ivars with an underscore is a
# convention that is used to implicitly indicate that an ivar should not
# be set or referencd outside of the memoization method.
#
# @example EnforcedStyleForLeadingUnderscores: disallowed (default)
# # bad
# # Method foo is memoized using an instance variable that is
# # not `@foo`. This can cause confusion and bugs.
# def foo
# @something ||= calculate_expensive_thing
# end
#
# # good
# def _foo
# @foo ||= calculate_expensive_thing
# end
#
# # good
# def foo
# @foo ||= calculate_expensive_thing
# end
#
# # good
# def foo
# @foo ||= begin
# calculate_expensive_thing
# end
# end
#
# # good
# def foo
# helper_variable = something_we_need_to_calculate_foo
# @foo ||= calculate_expensive_thing(helper_variable)
# end
#
# @example EnforcedStyleForLeadingUnderscores: required
# # bad
# def foo
# @something ||= calculate_expensive_thing
# end
#
# # bad
# def foo
# @foo ||= calculate_expensive_thing
# end
#
# # good
# def foo
# @_foo ||= calculate_expensive_thing
# end
#
# # good
# def _foo
# @_foo ||= calculate_expensive_thing
# end
#
# @example EnforcedStyleForLeadingUnderscores :optional
# # bad
# def foo
# @something ||= calculate_expensive_thing
# end
#
# # good
# def foo
# @foo ||= calculate_expensive_thing
# end
#
# # good
# def foo
# @_foo ||= calculate_expensive_thing
# end
#
# # good
# def _foo
# @_foo ||= calculate_expensive_thing
# end
class MemoizedInstanceVariableName < Cop
include ConfigurableEnforcedStyle
MSG = 'Memoized variable `%<var>s` does not match ' \
'method name `%<method>s`. Use `@%<suggested_var>s` instead.'.freeze
UNDERSCORE_REQUIRED = 'Memoized variable `%<var>s` does not start ' \
'with `_`. Use `@%<suggested_var>s` instead.'.freeze
def self.node_pattern
memo_assign = '(or_asgn $(ivasgn _) _)'
memoized_at_end_of_method = "(begin ... #{memo_assign})"
instance_method =
"(def $_ _ {#{memo_assign} #{memoized_at_end_of_method}})"
class_method =
"(defs self $_ _ {#{memo_assign} #{memoized_at_end_of_method}})"
"{#{instance_method} #{class_method}}"
end
private_class_method :node_pattern
def_node_matcher :memoized?, node_pattern
def on_def(node)
(method_name, ivar_assign) = memoized?(node)
return if matches?(method_name, ivar_assign)
msg = format(
message(ivar_assign.children.first.to_s),
var: ivar_assign.children.first.to_s,
suggested_var: suggested_var(method_name),
method: method_name
)
add_offense(node, location: ivar_assign.source_range, message: msg)
end
alias on_defs on_def
private
def style_parameter_name
'EnforcedStyleForLeadingUnderscores'
end
def matches?(method_name, ivar_assign)
return true if ivar_assign.nil? || method_name == :initialize
method_name = method_name.to_s.delete('!?')
variable = ivar_assign.children.first
variable_name = variable.to_s.sub('@', '')
variable_name_candidates(method_name).include?(variable_name)
end
def message(variable)
variable_name = variable.to_s.sub('@', '')
return UNDERSCORE_REQUIRED if style == :required &&
!variable_name.start_with?('_')
MSG
end
def suggested_var(method_name)
suggestion = method_name.to_s.delete('!?')
style == :required ? "_#{suggestion}" : suggestion
end
def variable_name_candidates(method_name)
no_underscore = method_name.sub(/\A_/, '')
with_underscore = "_#{method_name}"
case style
when :required
[with_underscore,
method_name.start_with?('_') ? method_name : nil].compact
when :disallowed
[method_name, no_underscore]
when :optional
[method_name, with_underscore, no_underscore]
else
raise 'Unreachable'
end
end
end
end
end
end