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
|
|
|
|
|
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(
|
2014-05-24 12:42:15 +00:00
|
|
|
reverse_map: [:map, :each],
|
|
|
|
reverse_each: [:each],
|
|
|
|
map: [:each],
|
|
|
|
send: [:public_send],
|
|
|
|
gsub: [:sub],
|
|
|
|
eql?: [:equal?],
|
|
|
|
:== => [:eql?, :equal?]
|
2014-03-30 14:56:51 +00:00
|
|
|
)
|
2013-08-02 01:01:42 +02:00
|
|
|
|
2014-06-15 12:30:36 +00:00
|
|
|
INDEX_REFERENCE = :[]
|
|
|
|
INDEX_ASSIGN = :[]=
|
|
|
|
VARIABLE_ASSIGN = :'='
|
|
|
|
ASSIGNMENT_OPERATORS = [INDEX_ASSIGN, VARIABLE_ASSIGN].to_set.freeze
|
|
|
|
ATTRIBUTE_ASSIGNMENT = /\A[a-z\d_]+=\z/.freeze
|
2013-06-22 17:53:12 +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
|
2013-06-22 17:53:12 +02:00
|
|
|
case selector
|
|
|
|
when INDEX_REFERENCE
|
|
|
|
run(Index::Reference)
|
|
|
|
when INDEX_ASSIGN
|
|
|
|
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
|
|
|
|
when binary_operator?
|
2013-06-14 19:53:55 +02:00
|
|
|
run(Binary)
|
2014-04-22 17:01:46 +00:00
|
|
|
when attribute_assignment?
|
|
|
|
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-14 19:47:23 +02:00
|
|
|
mutate_receiver
|
2013-06-15 17:16:34 +02:00
|
|
|
emit_argument_propagation
|
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 12:33:34 +00:00
|
|
|
emit(receiver) if receiver && !(OP_ASSIGN.include?(parent_type) && NOT_ASSIGNABLE.include?(receiver.type))
|
2013-07-14 21:37:22 +02:00
|
|
|
end
|
|
|
|
|
2013-06-14 19:53:55 +02: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
|
|
|
|
|
2014-01-12 00:43:45 +01:00
|
|
|
# Test for attribute assignment
|
|
|
|
#
|
2014-06-15 12:41:51 +00:00
|
|
|
# @return [Boolean]
|
2014-01-12 00:43:45 +01:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def attribute_assignment?
|
2014-06-15 12:41:51 +00:00
|
|
|
arguments.one? && ATTRIBUTE_ASSIGNMENT =~ selector
|
2014-01-12 00:43:45 +01: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
|
|
|
|
return unless arguments.one?
|
2013-06-22 00:32:42 +02:00
|
|
|
node = arguments.first
|
2014-06-15 12:16:55 +00:00
|
|
|
emit(node) unless 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
|
2013-06-21 15:51:30 +02:00
|
|
|
emit_receiver_mutations
|
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-08 13:01:26 +00:00
|
|
|
emit_receiver(nil) if allow_implicit_self?
|
|
|
|
end
|
|
|
|
|
|
|
|
# Test if implicit self is allowed
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def allow_implicit_self?
|
2014-06-15 12:43:08 +00:00
|
|
|
n_self?(receiver) && !(
|
|
|
|
KEYWORDS.include?(selector) ||
|
|
|
|
OP_ASSIGN.include?(parent_type) ||
|
|
|
|
attribute_assignment?
|
|
|
|
)
|
2013-06-14 19:47:23 +02:00
|
|
|
end
|
|
|
|
|
2014-01-12 00:43:45 +01:00
|
|
|
# Test for assignment
|
|
|
|
#
|
2014-06-15 12:20:00 +00:00
|
|
|
# @return [Boolean]
|
2014-01-12 00:43:45 +01:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def assignment?
|
2014-06-15 12:41:51 +00:00
|
|
|
arguments.one? && (ASSIGNMENT_OPERATORS.include?(selector) || attribute_assignment?)
|
2014-01-12 00:43:45 +01:00
|
|
|
end
|
|
|
|
|
2014-06-15 12:41:51 +00:00
|
|
|
# Test if node is part of an mlhs
|
2014-01-12 00:43:45 +01:00
|
|
|
#
|
2014-06-15 12:20:00 +00:00
|
|
|
# @return [Boolean]
|
2014-01-12 00:43:45 +01:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def mlhs?
|
|
|
|
assignment? && !arguments?
|
|
|
|
end
|
|
|
|
|
|
|
|
# Test for empty arguments
|
|
|
|
#
|
2014-06-15 12:20:00 +00:00
|
|
|
# @return [Boolean]
|
2014-01-12 00:43:45 +01:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def arguments?
|
|
|
|
arguments.any?
|
|
|
|
end
|
|
|
|
|
2013-06-04 10:25:13 +02:00
|
|
|
end # Send
|
|
|
|
end # Node
|
|
|
|
end # Mutator
|
|
|
|
end # Mutant
|