Class: Evilution::Mutator::Operator::CollectionReplacement

Inherits:
Base
  • Object
show all
Defined in:
lib/evilution/mutator/operator/collection_replacement.rb

Constant Summary collapse

REPLACEMENTS =
{
  map: [:each],
  each: [:map],
  select: [:reject],
  reject: [:select],
  flat_map: [:map],
  collect: [:each],
  sort: [:sort_by],
  sort_by: [:sort],
  find: [:detect],
  detect: [:find],
  any?: [:all?],
  all?: [:any?],
  count: [:length],
  length: [:count],
  pop: [:shift],
  shift: [:pop],
  push: [:unshift],
  unshift: [:push],
  each_key: [:each_value],
  each_value: [:each_key],
  assoc: [:rassoc],
  rassoc: [:assoc],
  grep: [:grep_v],
  grep_v: [:grep],
  take: [:drop],
  drop: [:take],
  min: [:max],
  max: [:min],
  min_by: [:max_by],
  max_by: [:min_by],
  compact: [:flatten],
  flatten: [:compact],
  zip: [:product],
  product: [:zip],
  first: [:last],
  last: [:first],
  keys: [:values],
  values: [:keys]
}.freeze

Instance Attribute Summary

Attributes inherited from Base

#mutations

Instance Method Summary collapse

Methods inherited from Base

#call, clear_parse_cache!, #initialize, operator_name, parsed_tree_for

Constructor Details

This class inherits a constructor from Evilution::Mutator::Base

Instance Method Details

#visit_call_node(node) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/evilution/mutator/operator/collection_replacement.rb', line 47

def visit_call_node(node)
  replacements = REPLACEMENTS[node.name]
  return super unless replacements

  loc = node.message_loc
  return super unless loc

  replacements.each do |replacement|
    add_mutation(
      offset: loc.start_offset,
      length: loc.length,
      replacement: replacement.to_s,
      node: node
    )
  end

  super
end