Module: RuboCop::Cop::HashTransformMethod
Relationships & Source Files | |
Namespace Children | |
Classes:
| |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
Macros
|
|
Defined in: | lib/rubocop/cop/mixin/hash_transform_method.rb |
Overview
Common functionality for Style/HashTransformKeys and Style/HashTransformValues
Constant Summary
-
RESTRICT_ON_SEND =
# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 10%i[[] to_h].freeze
Instance Method Summary
- #array_receiver?(node)
- #on_block(node)
- #on_csend(node)
- #on_send(node)
- #execute_correction(corrector, node, correction) private
- #extract_captures(_match) ⇒ Captures private abstract
- #handle_possible_offense(node, match, match_desc) private
- #new_method_name ⇒ String private abstract
- #on_bad_each_with_object(_node) private abstract
- #on_bad_hash_brackets_map(_node) private abstract
- #on_bad_map_to_h(_node) private abstract
- #on_bad_to_h(_node) private abstract
- #prepare_correction(node) private
Instance Method Details
#array_receiver?(node)
[ GitHub ]# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 13
def_node_matcher :array_receiver?, <<~PATTERN {(array ...) (send _ :each_with_index) (send _ :with_index _ ?) (send _ :zip ...)} PATTERN
#execute_correction(corrector, node, correction) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 108
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
#extract_captures(_match) ⇒ Captures (private)
This method is abstract.
# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 83
def extract_captures(_match) raise NotImplementedError end
#handle_possible_offense(node, match, match_desc) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 60
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? return unless captures.use_transformed_argname? = "Prefer `#{new_method_name}` over `#{match_desc}`." add_offense(node, message: ) do |corrector| correction = prepare_correction(node) execute_correction(corrector, node, correction) end end
#new_method_name ⇒ String (private)
This method is abstract.
# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 90
def new_method_name raise NotImplementedError end
#on_bad_each_with_object(_node) (private)
This method is abstract.
Implemented with def_node_matcher
# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 41
def on_bad_each_with_object(_node) raise NotImplementedError end
#on_bad_hash_brackets_map(_node) (private)
This method is abstract.
Implemented with def_node_matcher
# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 46
def on_bad_hash_brackets_map(_node) raise NotImplementedError end
#on_bad_map_to_h(_node) (private)
This method is abstract.
Implemented with def_node_matcher
# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 51
def on_bad_map_to_h(_node) raise NotImplementedError end
#on_bad_to_h(_node) (private)
This method is abstract.
Implemented with def_node_matcher
# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 56
def on_bad_to_h(_node) raise NotImplementedError end
#on_block(node)
[ GitHub ]# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 17
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler 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
#on_csend(node)
[ GitHub ]# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 34
def on_csend(node) on_bad_map_to_h(node) { |*match| handle_possible_offense(node, match, 'map {...}.to_h') } end
#on_send(node)
[ GitHub ]# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 27
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
#prepare_correction(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 94
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