Class: Evilution::Mutator::Operator::CollectionReturn

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

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

#collection_replacement(node) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/evilution/mutator/operator/collection_return.rb', line 25

def collection_replacement(node)
  case node
  when Prism::ArrayNode
    "[]" if node.elements.any?
  when Prism::HashNode
    "{}" if node.elements.any?
  end
end

#visit_def_node(node) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/evilution/mutator/operator/collection_return.rb', line 6

def visit_def_node(node)
  body = node.body
  if body.is_a?(Prism::StatementsNode) && body.body.length > 1
    return_node = body.body.last
    replacement = collection_replacement(return_node)

    if replacement
      add_mutation(
        offset: body.location.start_offset,
        length: body.location.length,
        replacement: replacement,
        node: node
      )
    end
  end

  super
end