2012-12-06 21:30:28 +01:00
|
|
|
module Mutant
|
|
|
|
class Mutator
|
|
|
|
class Node
|
2013-01-04 22:16:03 +01:00
|
|
|
|
|
|
|
# Namespace for send mutators
|
2012-12-06 21:30:28 +01:00
|
|
|
class Send < self
|
2014-06-29 21:25:17 +00:00
|
|
|
include AST::Types
|
2012-12-06 21:30:28 +01:00
|
|
|
|
2013-06-04 10:25:13 +02:00
|
|
|
handle(:send)
|
2012-12-06 21:30:28 +01:00
|
|
|
|
2013-06-21 15:51:30 +02:00
|
|
|
children :receiver, :selector
|
|
|
|
|
2014-03-30 14:56:51 +00:00
|
|
|
SELECTOR_REPLACEMENTS = IceNine.deep_freeze(
|
2015-05-31 20:44:09 +00:00
|
|
|
reverse_map: %i[map each],
|
|
|
|
kind_of?: %i[instance_of?],
|
|
|
|
is_a?: %i[instance_of?],
|
|
|
|
reverse_each: %i[each],
|
|
|
|
reverse_merge: %i[merge],
|
|
|
|
map: %i[each],
|
|
|
|
send: %i[public_send __send__],
|
|
|
|
__send__: %i[public_send],
|
|
|
|
gsub: %i[sub],
|
|
|
|
eql?: %i[equal?],
|
|
|
|
to_s: %i[to_str],
|
|
|
|
to_i: %i[to_int],
|
|
|
|
to_a: %i[to_ary],
|
|
|
|
:== => %i[eql? equal?],
|
|
|
|
:>= => %i[> == eql? equal?],
|
|
|
|
:<= => %i[< == eql? equal?],
|
|
|
|
:> => %i[== >= eql? equal?],
|
|
|
|
:< => %i[== <= eql? equal?]
|
2014-03-30 14:56:51 +00:00
|
|
|
)
|
2013-08-02 01:01:42 +02:00
|
|
|
|
2012-12-06 21:30:28 +01:00
|
|
|
private
|
|
|
|
|
2013-06-22 17:53:12 +02:00
|
|
|
# Perform dispatch
|
2012-12-10 17:11:08 +01:00
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def dispatch
|
2014-06-05 16:37:31 +00:00
|
|
|
emit_singletons
|
2014-06-29 21:25:17 +00:00
|
|
|
if meta.index_assignment?
|
2013-06-22 17:53:12 +02:00
|
|
|
run(Index::Assign)
|
|
|
|
else
|
|
|
|
non_index_dispatch
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Perform non index dispatch
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def non_index_dispatch
|
2014-01-12 00:43:45 +01:00
|
|
|
case
|
2014-06-29 21:25:17 +00:00
|
|
|
when meta.binary_method_operator?
|
2013-06-14 19:53:55 +02:00
|
|
|
run(Binary)
|
2014-06-29 21:25:17 +00:00
|
|
|
when meta.attribute_assignment?
|
2014-04-22 17:01:46 +00:00
|
|
|
run(AttributeAssignment)
|
2014-01-12 00:43:45 +01:00
|
|
|
else
|
|
|
|
normal_dispatch
|
2013-06-14 19:53:55 +02:00
|
|
|
end
|
2013-06-21 15:51:30 +02:00
|
|
|
end
|
|
|
|
|
2013-06-21 15:58:35 +02:00
|
|
|
# Return arguments
|
|
|
|
#
|
|
|
|
# @return [Enumerable<Parser::AST::Node>]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
alias_method :arguments, :remaining_children
|
|
|
|
|
2013-06-21 15:51:30 +02:00
|
|
|
# Perform normal, non special case dispatch
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def normal_dispatch
|
2013-07-14 21:37:22 +02:00
|
|
|
emit_naked_receiver
|
2013-09-02 21:29:44 +02:00
|
|
|
emit_selector_replacement
|
2013-06-15 17:16:34 +02:00
|
|
|
emit_argument_propagation
|
2014-06-15 12:47:04 +00:00
|
|
|
mutate_receiver
|
2013-06-14 19:47:23 +02:00
|
|
|
mutate_arguments
|
|
|
|
end
|
|
|
|
|
2013-09-02 21:29:44 +02:00
|
|
|
# Emit selector replacement
|
2013-08-02 00:46:50 +02:00
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2013-09-02 21:29:44 +02:00
|
|
|
def emit_selector_replacement
|
2014-03-30 14:56:51 +00:00
|
|
|
SELECTOR_REPLACEMENTS.fetch(selector, EMPTY_ARRAY).each do |replacement|
|
|
|
|
emit_selector(replacement)
|
|
|
|
end
|
2013-08-02 00:46:50 +02:00
|
|
|
end
|
|
|
|
|
2013-07-14 21:37:22 +02:00
|
|
|
# Emit naked receiver mutation
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def emit_naked_receiver
|
2014-06-15 13:02:24 +00:00
|
|
|
emit(receiver) if receiver && !NOT_ASSIGNABLE.include?(receiver.type)
|
2013-07-14 21:37:22 +02:00
|
|
|
end
|
|
|
|
|
2013-06-14 19:47:23 +02:00
|
|
|
# Mutate arguments
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def mutate_arguments
|
2014-06-03 03:51:22 +00:00
|
|
|
emit_type(receiver, selector)
|
2014-05-27 15:12:36 +00:00
|
|
|
remaining_children_with_index.each do |_node, index|
|
2013-06-14 19:47:23 +02:00
|
|
|
mutate_child(index)
|
|
|
|
delete_child(index)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-15 17:16:34 +02:00
|
|
|
# Emit argument propagation
|
|
|
|
#
|
2013-06-17 11:47:27 +02:00
|
|
|
# @return [undefined]
|
2013-06-15 17:16:34 +02:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def emit_argument_propagation
|
2013-06-22 00:32:42 +02:00
|
|
|
node = arguments.first
|
2014-06-15 12:45:31 +00:00
|
|
|
emit(node) if arguments.one? && !NOT_STANDALONE.include?(node.type)
|
2013-06-15 17:16:34 +02:00
|
|
|
end
|
|
|
|
|
2013-06-14 19:47:23 +02:00
|
|
|
# Emit receiver mutations
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def mutate_receiver
|
|
|
|
return unless receiver
|
|
|
|
emit_implicit_self
|
2014-06-15 13:12:11 +00:00
|
|
|
emit_receiver_mutations do |node|
|
|
|
|
!n_nil?(node)
|
|
|
|
end
|
2013-06-14 19:47:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Emit implicit self mutation
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
2013-06-15 16:37:43 +02:00
|
|
|
# @api private
|
2013-06-14 19:47:23 +02:00
|
|
|
#
|
|
|
|
def emit_implicit_self
|
2014-06-15 12:44:25 +00:00
|
|
|
emit_receiver(nil) if n_self?(receiver) && !(
|
2014-06-15 19:48:27 +00:00
|
|
|
KEYWORDS.include?(selector) ||
|
|
|
|
METHOD_OPERATORS.include?(selector) ||
|
|
|
|
OP_ASSIGN.include?(parent_type) ||
|
2014-06-29 21:25:17 +00:00
|
|
|
meta.attribute_assignment?
|
2014-06-15 12:43:08 +00:00
|
|
|
)
|
2013-06-14 19:47:23 +02:00
|
|
|
end
|
|
|
|
|
2013-06-04 10:25:13 +02:00
|
|
|
end # Send
|
|
|
|
end # Node
|
|
|
|
end # Mutator
|
2014-06-15 13:12:11 +00:00
|
|
|
|
2013-06-04 10:25:13 +02:00
|
|
|
end # Mutant
|