forked from rubocop/rubocop
/
hash_transform_method.rb
188 lines (153 loc) · 5.96 KB
/
hash_transform_method.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
# frozen_string_literal: true
module RuboCop
module Cop
# Common functionality for Style/HashTransformKeys and
# Style/HashTransformValues
module HashTransformMethod
extend NodePattern::Macros
RESTRICT_ON_SEND = %i[[] to_h].freeze
# @!method array_receiver?(node)
def_node_matcher :array_receiver?, <<~PATTERN
{(array ...) (send _ :each_with_index) (send _ :with_index _ ?) (send _ :zip ...)}
PATTERN
def on_block(node)
on_bad_each_with_object(node) do |*match|
handle_possible_offense(node, match, 'each_with_object')
end
return if target_ruby_version < 2.6
on_bad_to_h(node) { |*match| handle_possible_offense(node, match, 'to_h {...}') }
end
def on_send(node)
on_bad_hash_brackets_map(node) do |*match|
handle_possible_offense(node, match, 'Hash[_.map {...}]')
end
on_bad_map_to_h(node) { |*match| handle_possible_offense(node, match, 'map {...}.to_h') }
end
def on_csend(node)
on_bad_map_to_h(node) { |*match| handle_possible_offense(node, match, 'map {...}.to_h') }
end
private
# @abstract Implemented with `def_node_matcher`
def on_bad_each_with_object(_node)
raise NotImplementedError
end
# @abstract Implemented with `def_node_matcher`
def on_bad_hash_brackets_map(_node)
raise NotImplementedError
end
# @abstract Implemented with `def_node_matcher`
def on_bad_map_to_h(_node)
raise NotImplementedError
end
# @abstract Implemented with `def_node_matcher`
def on_bad_to_h(_node)
raise NotImplementedError
end
def handle_possible_offense(node, match, match_desc)
captures = extract_captures(match)
# If key didn't actually change either, this is most likely a false
# positive (receiver isn't a hash).
return if captures.noop_transformation?
# Can't `transform_keys` if key transformation uses value, or
# `transform_values` if value transformation uses key.
return if captures.transformation_uses_both_args?
message = "Prefer `#{new_method_name}` over `#{match_desc}`."
add_offense(node, message: message) do |corrector|
correction = prepare_correction(node)
execute_correction(corrector, node, correction)
end
end
# @abstract
#
# @return [Captures]
def extract_captures(_match)
raise NotImplementedError
end
# @abstract
#
# @return [String]
def new_method_name
raise NotImplementedError
end
def prepare_correction(node)
if (match = on_bad_each_with_object(node))
Autocorrection.from_each_with_object(node, match)
elsif (match = on_bad_hash_brackets_map(node))
Autocorrection.from_hash_brackets_map(node, match)
elsif (match = on_bad_map_to_h(node))
Autocorrection.from_map_to_h(node, match)
elsif (match = on_bad_to_h(node))
Autocorrection.from_to_h(node, match)
else
raise 'unreachable'
end
end
def execute_correction(corrector, node, correction)
correction.strip_prefix_and_suffix(node, corrector)
correction.set_new_method_name(new_method_name, corrector)
captures = extract_captures(correction.match)
correction.set_new_arg_name(captures.transformed_argname, corrector)
correction.set_new_body_expression(captures.transforming_body_expr, corrector)
end
# Internal helper class to hold match data
Captures = Struct.new(
:transformed_argname,
:transforming_body_expr,
:unchanged_body_expr
) do
def noop_transformation?
transforming_body_expr.lvar_type? &&
transforming_body_expr.children == [transformed_argname]
end
def transformation_uses_both_args?
transforming_body_expr.descendants.include?(unchanged_body_expr)
end
end
# Internal helper class to hold autocorrect data
Autocorrection = Struct.new(:match, :block_node, :leading, :trailing) do
def self.from_each_with_object(node, match)
new(match, node, 0, 0)
end
def self.from_hash_brackets_map(node, match)
new(match, node.children.last, 'Hash['.length, ']'.length)
end
def self.from_map_to_h(node, match)
strip_trailing_chars = 0
unless node.parent&.block_type?
map_range = node.children.first.source_range
node_range = node.source_range
strip_trailing_chars = node_range.end_pos - map_range.end_pos
end
new(match, node.children.first, 0, strip_trailing_chars)
end
def self.from_to_h(node, match)
new(match, node, 0, 0)
end
def strip_prefix_and_suffix(node, corrector)
expression = node.loc.expression
corrector.remove_leading(expression, leading)
corrector.remove_trailing(expression, trailing)
end
def set_new_method_name(new_method_name, corrector)
range = block_node.send_node.loc.selector
if (send_end = block_node.send_node.loc.end)
# If there are arguments (only true in the `each_with_object`
# case)
range = range.begin.join(send_end)
end
corrector.replace(range, new_method_name)
end
def set_new_arg_name(transformed_argname, corrector)
corrector.replace(block_node.arguments.loc.expression, "|#{transformed_argname}|")
end
def set_new_body_expression(transforming_body_expr, corrector)
body_source = transforming_body_expr.loc.expression.source
if transforming_body_expr.hash_type? && !transforming_body_expr.braces?
body_source = "{ #{body_source} }"
end
corrector.replace(block_node.body, body_source)
end
end
end
end
end