Class: RuboCop::Cop::IndexMethod::Autocorrection
- Inherits:
-
Struct
- Object
- Struct
- RuboCop::Cop::IndexMethod::Autocorrection
- Defined in:
- lib/rubocop/cop/mixin/index_method.rb
Overview
Internal helper class to hold autocorrect data
Class Method Summary collapse
- .from_each_with_object(node, match) ⇒ Object
- .from_hash_brackets_map(node, match) ⇒ Object
- .from_map_to_h(node, match) ⇒ Object
- .from_to_h(node, match) ⇒ Object
Instance Method Summary collapse
- #set_new_arg_name(transformed_argname, corrector) ⇒ Object
- #set_new_body_expression(transforming_body_expr, corrector) ⇒ Object
- #set_new_method_name(new_method_name, corrector) ⇒ Object
- #strip_prefix_and_suffix(node, corrector) ⇒ Object
Class Method Details
.from_each_with_object(node, match) ⇒ Object
118 119 120 |
# File 'lib/rubocop/cop/mixin/index_method.rb', line 118 def self.from_each_with_object(node, match) new(match, node, 0, 0) end |
.from_hash_brackets_map(node, match) ⇒ Object
138 139 140 |
# File 'lib/rubocop/cop/mixin/index_method.rb', line 138 def self.from_hash_brackets_map(node, match) new(match, node.children.last, "#{node.receiver.source}[".length, ']'.length) end |
.from_map_to_h(node, match) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/rubocop/cop/mixin/index_method.rb', line 126 def self.from_map_to_h(node, match) if node.block_literal? strip_trailing_chars = 0 else 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 |
.from_to_h(node, match) ⇒ Object
122 123 124 |
# File 'lib/rubocop/cop/mixin/index_method.rb', line 122 def self.from_to_h(node, match) new(match, node, 0, 0) end |
Instance Method Details
#set_new_arg_name(transformed_argname, corrector) ⇒ Object
157 158 159 160 161 |
# File 'lib/rubocop/cop/mixin/index_method.rb', line 157 def set_new_arg_name(transformed_argname, corrector) return if block_node.numblock_type? corrector.replace(block_node.arguments, "|#{transformed_argname}|") end |
#set_new_body_expression(transforming_body_expr, corrector) ⇒ Object
163 164 165 166 167 168 |
# File 'lib/rubocop/cop/mixin/index_method.rb', line 163 def set_new_body_expression(transforming_body_expr, corrector) body_source = transforming_body_expr.source body_source = "{ #{body_source} }" if transforming_body_expr.hash_type? && !transforming_body_expr.braces? corrector.replace(block_node.body, body_source) end |
#set_new_method_name(new_method_name, corrector) ⇒ Object
148 149 150 151 152 153 154 155 |
# File 'lib/rubocop/cop/mixin/index_method.rb', line 148 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 |
#strip_prefix_and_suffix(node, corrector) ⇒ Object
142 143 144 145 146 |
# File 'lib/rubocop/cop/mixin/index_method.rb', line 142 def strip_prefix_and_suffix(node, corrector) expression = node.source_range corrector.remove_leading(expression, leading) corrector.remove_trailing(expression, trailing) end |