2013-07-28 19:03:06 -04:00
|
|
|
# encoding: utf-8
|
|
|
|
|
2012-12-06 15:30:28 -05:00
|
|
|
module Mutant
|
|
|
|
class Mutator
|
|
|
|
class Node
|
2013-01-04 16:16:03 -05:00
|
|
|
|
|
|
|
# Namespace for send mutators
|
2012-12-06 15:30:28 -05:00
|
|
|
class Send < self
|
|
|
|
|
2013-06-04 04:25:13 -04:00
|
|
|
handle(:send)
|
2012-12-06 15:30:28 -05:00
|
|
|
|
2013-06-21 09:51:30 -04:00
|
|
|
children :receiver, :selector
|
|
|
|
|
2013-08-01 19:01:42 -04:00
|
|
|
SELECTOR_REPLACEMENTS = {
|
2013-09-07 14:27:06 -04:00
|
|
|
send: :public_send,
|
|
|
|
gsub: :sub
|
2013-08-01 19:01:42 -04:00
|
|
|
}.freeze
|
|
|
|
|
2013-06-22 11:53:12 -04:00
|
|
|
INDEX_REFERENCE = :[]
|
|
|
|
INDEX_ASSIGN = :[]=
|
|
|
|
ASSIGN_SUFFIX = :'='
|
|
|
|
|
|
|
|
# Base mutator for index operations
|
|
|
|
class Index < self
|
|
|
|
|
|
|
|
# Mutator for index references
|
|
|
|
class Reference < self
|
|
|
|
|
|
|
|
# Perform dispatch
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def dispatch
|
|
|
|
emit(receiver)
|
|
|
|
end
|
|
|
|
|
|
|
|
end # Reference
|
|
|
|
|
|
|
|
# Mutator for index assignments
|
|
|
|
class Assign < self
|
|
|
|
|
|
|
|
# Perform dispatch
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def dispatch
|
|
|
|
emit(receiver)
|
|
|
|
end
|
|
|
|
|
|
|
|
end # Assign
|
|
|
|
end # Index
|
|
|
|
|
2012-12-06 15:30:28 -05:00
|
|
|
private
|
|
|
|
|
2013-06-22 11:53:12 -04:00
|
|
|
# Perform dispatch
|
2012-12-10 11:11:08 -05:00
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def dispatch
|
2013-06-22 11:53:12 -04:00
|
|
|
case selector
|
|
|
|
when INDEX_REFERENCE
|
|
|
|
run(Index::Reference)
|
|
|
|
when INDEX_ASSIGN
|
|
|
|
run(Index::Assign)
|
|
|
|
else
|
|
|
|
non_index_dispatch
|
|
|
|
end
|
2013-09-08 02:10:25 -04:00
|
|
|
emit_nil
|
2013-06-22 11:53:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Perform non index dispatch
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def non_index_dispatch
|
2013-06-14 13:53:55 -04:00
|
|
|
if binary_operator?
|
|
|
|
run(Binary)
|
|
|
|
return
|
|
|
|
end
|
2013-06-21 09:51:30 -04:00
|
|
|
normal_dispatch
|
|
|
|
end
|
|
|
|
|
2013-06-21 09:58:35 -04:00
|
|
|
# Return arguments
|
|
|
|
#
|
|
|
|
# @return [Enumerable<Parser::AST::Node>]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
alias_method :arguments, :remaining_children
|
|
|
|
|
2013-06-21 09:51:30 -04:00
|
|
|
# Perform normal, non special case dispatch
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def normal_dispatch
|
2013-07-14 15:37:22 -04:00
|
|
|
emit_naked_receiver
|
2013-09-02 15:29:44 -04:00
|
|
|
emit_selector_replacement
|
2013-06-14 13:47:23 -04:00
|
|
|
mutate_receiver
|
2013-06-15 11:16:34 -04:00
|
|
|
emit_argument_propagation
|
2013-06-14 13:47:23 -04:00
|
|
|
mutate_arguments
|
|
|
|
end
|
|
|
|
|
2013-09-02 15:29:44 -04:00
|
|
|
# Emit selector replacement
|
2013-08-01 18:46:50 -04:00
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2013-09-02 15:29:44 -04:00
|
|
|
def emit_selector_replacement
|
2013-08-01 19:01:42 -04:00
|
|
|
replacement = SELECTOR_REPLACEMENTS.fetch(selector) { return }
|
|
|
|
emit_selector(replacement)
|
2013-08-01 18:46:50 -04:00
|
|
|
end
|
|
|
|
|
2013-07-14 15:37:22 -04:00
|
|
|
# Emit naked receiver mutation
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def emit_naked_receiver
|
|
|
|
return unless receiver
|
|
|
|
op_assign = OP_ASSIGN.include?(parent_type)
|
|
|
|
not_assignable = NOT_ASSIGNABLE.include?(receiver.type)
|
|
|
|
return if op_assign and not_assignable
|
|
|
|
emit(receiver)
|
|
|
|
end
|
|
|
|
|
2013-06-14 13:53:55 -04:00
|
|
|
# Test for binary operator
|
|
|
|
#
|
|
|
|
# @return [true]
|
|
|
|
# if send is a binary operator
|
|
|
|
#
|
|
|
|
# @return [false]
|
|
|
|
# otherwise
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def binary_operator?
|
|
|
|
arguments.one? && BINARY_METHOD_OPERATORS.include?(selector)
|
|
|
|
end
|
|
|
|
|
2013-06-14 13:47:23 -04:00
|
|
|
# Mutate arguments
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def mutate_arguments
|
|
|
|
return if arguments.empty?
|
|
|
|
emit_self(receiver, selector)
|
2013-06-21 09:51:30 -04:00
|
|
|
remaining_children_with_index.each do |node, index|
|
2013-06-14 13:47:23 -04:00
|
|
|
mutate_child(index)
|
|
|
|
delete_child(index)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-15 11:16:34 -04:00
|
|
|
# Emit argument propagation
|
|
|
|
#
|
2013-06-17 05:47:27 -04:00
|
|
|
# @return [undefined]
|
2013-06-15 11:16:34 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def emit_argument_propagation
|
|
|
|
return unless arguments.one?
|
2013-06-21 18:32:42 -04:00
|
|
|
node = arguments.first
|
2013-07-14 15:37:22 -04:00
|
|
|
return if NOT_STANDALONE.include?(node.type)
|
2013-06-15 11:16:34 -04:00
|
|
|
emit(arguments.first)
|
|
|
|
end
|
|
|
|
|
2013-06-14 13:47:23 -04:00
|
|
|
# Emit receiver mutations
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def mutate_receiver
|
|
|
|
return unless receiver
|
|
|
|
emit_implicit_self
|
2013-06-21 09:51:30 -04:00
|
|
|
emit_receiver_mutations
|
2013-06-14 13:47:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Emit implicit self mutation
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
2013-06-15 10:37:43 -04:00
|
|
|
# @api private
|
2013-06-14 13:47:23 -04:00
|
|
|
#
|
|
|
|
def emit_implicit_self
|
|
|
|
if receiver.type == :self and !KEYWORDS.include?(selector)
|
2013-06-21 09:51:30 -04:00
|
|
|
emit_receiver(nil)
|
2013-06-14 13:47:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-04 04:25:13 -04:00
|
|
|
end # Send
|
|
|
|
end # Node
|
|
|
|
end # Mutator
|
|
|
|
end # Mutant
|