free_mutant/lib/mutant/mutator/node/send.rb
Markus Schirp 13cd04d9be Introduce AST::Meta to externalize semantic analysis
* Add Mutant::AST namespace to hold all AST related data / helpers.
* Mutant::AST will be externalized into an ast-meta gem that can be
  shared with unparser for deduplication.
* Over the time the mutators itself will not need to deal with semantic
  analysis of the AST anymore by themselves.
* Move AST analysis for send nodes to AST::Meta
* Fix #209
2014-06-29 21:26:58 +00:00

159 lines
3.6 KiB
Ruby

module Mutant
class Mutator
class Node
# Namespace for send mutators
class Send < self
include AST::Types
handle(:send)
children :receiver, :selector
SELECTOR_REPLACEMENTS = IceNine.deep_freeze(
reverse_map: [:map, :each],
reverse_each: [:each],
map: [:each],
send: [:public_send],
gsub: [:sub],
eql?: [:equal?],
:== => [:eql?, :equal?]
)
private
# Perform dispatch
#
# @return [undefined]
#
# @api private
#
def dispatch
emit_singletons
if meta.index_assignment?
run(Index::Assign)
else
non_index_dispatch
end
end
# Perform non index dispatch
#
# @return [undefined]
#
# @api private
#
def non_index_dispatch
case
when meta.binary_method_operator?
run(Binary)
when meta.attribute_assignment?
run(AttributeAssignment)
else
normal_dispatch
end
end
# Return arguments
#
# @return [Enumerable<Parser::AST::Node>]
#
# @api private
#
alias_method :arguments, :remaining_children
# Perform normal, non special case dispatch
#
# @return [undefined]
#
# @api private
#
def normal_dispatch
emit_naked_receiver
emit_selector_replacement
emit_argument_propagation
mutate_receiver
mutate_arguments
end
# Emit selector replacement
#
# @return [undefined]
#
# @api private
#
def emit_selector_replacement
SELECTOR_REPLACEMENTS.fetch(selector, EMPTY_ARRAY).each do |replacement|
emit_selector(replacement)
end
end
# Emit naked receiver mutation
#
# @return [undefined]
#
# @api private
#
def emit_naked_receiver
emit(receiver) if receiver && !NOT_ASSIGNABLE.include?(receiver.type)
end
# Mutate arguments
#
# @return [undefined]
#
# @api private
#
def mutate_arguments
emit_type(receiver, selector)
remaining_children_with_index.each do |_node, index|
mutate_child(index)
delete_child(index)
end
end
# Emit argument propagation
#
# @return [undefined]
#
# @api private
#
def emit_argument_propagation
node = arguments.first
emit(node) if arguments.one? && !NOT_STANDALONE.include?(node.type)
end
# Emit receiver mutations
#
# @return [undefined]
#
# @api private
#
def mutate_receiver
return unless receiver
emit_implicit_self
emit_receiver_mutations do |node|
!n_nil?(node)
end
end
# Emit implicit self mutation
#
# @return [undefined]
#
# @api private
#
def emit_implicit_self
emit_receiver(nil) if n_self?(receiver) && !(
KEYWORDS.include?(selector) ||
METHOD_OPERATORS.include?(selector) ||
OP_ASSIGN.include?(parent_type) ||
meta.attribute_assignment?
)
end
end # Send
end # Node
end # Mutator
end # Mutant