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
|
2015-10-08 21:58:59 -07:00
|
|
|
# rubocop:disable ClassLength
|
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],
|
2016-02-06 01:59:49 -08:00
|
|
|
method: %i[public_method],
|
2015-05-31 20:44:09 +00:00
|
|
|
gsub: %i[sub],
|
|
|
|
eql?: %i[equal?],
|
|
|
|
to_s: %i[to_str],
|
|
|
|
to_i: %i[to_int],
|
2016-02-05 22:36:01 -08:00
|
|
|
to_a: %i[to_ary to_set],
|
2015-08-09 20:36:44 -07:00
|
|
|
to_h: %i[to_hash],
|
2015-12-31 18:03:39 -05:00
|
|
|
at: %i[fetch key?],
|
2015-12-25 15:43:13 -05:00
|
|
|
fetch: %i[key?],
|
2015-12-25 13:56:57 -05:00
|
|
|
values_at: %i[fetch_values],
|
2015-12-25 15:39:50 -05:00
|
|
|
:[] => %i[at fetch key?],
|
2015-05-31 20:44:09 +00:00
|
|
|
:== => %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
|
|
|
|
2015-10-08 21:58:59 -07:00
|
|
|
RECEIVER_SELECTOR_REPLACEMENTS = IceNine.deep_freeze(
|
|
|
|
Date: {
|
|
|
|
parse: %i[jd civil strptime iso8601 rfc3339 xmlschema rfc2822 rfc822 httpdate jisx0301]
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
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]
|
|
|
|
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]
|
|
|
|
def non_index_dispatch
|
2015-06-13 14:20:36 +00:00
|
|
|
if meta.binary_method_operator?
|
2013-06-14 19:53:55 +02:00
|
|
|
run(Binary)
|
2015-06-13 14:20:36 +00:00
|
|
|
elsif 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
|
|
|
|
|
2015-07-03 15:21:39 +00:00
|
|
|
# AST metadata for node
|
2015-06-13 14:20:36 +00:00
|
|
|
#
|
|
|
|
# @return [AST::Meta::Send]
|
|
|
|
def meta
|
|
|
|
AST::Meta::Send.new(node)
|
|
|
|
end
|
|
|
|
memoize :meta
|
|
|
|
|
2015-07-03 15:21:39 +00:00
|
|
|
# Arguments being send
|
2013-06-21 15:58:35 +02:00
|
|
|
#
|
|
|
|
# @return [Enumerable<Parser::AST::Node>]
|
|
|
|
alias_method :arguments, :remaining_children
|
2016-03-06 21:19:32 +00:00
|
|
|
private :arguments
|
2013-06-21 15:58:35 +02:00
|
|
|
|
2013-06-21 15:51:30 +02:00
|
|
|
# Perform normal, non special case dispatch
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
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
|
2015-10-18 22:34:00 -07:00
|
|
|
emit_selector_specific_mutations
|
2013-06-15 17:16:34 +02:00
|
|
|
emit_argument_propagation
|
2015-10-08 21:58:59 -07:00
|
|
|
emit_receiver_selector_mutations
|
2014-06-15 12:47:04 +00:00
|
|
|
mutate_receiver
|
2013-06-14 19:47:23 +02:00
|
|
|
mutate_arguments
|
|
|
|
end
|
|
|
|
|
2015-10-18 22:34:00 -07:00
|
|
|
# Emit mutations which only correspond to one selector
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
def emit_selector_specific_mutations
|
|
|
|
emit_const_get_mutation
|
|
|
|
emit_integer_mutation
|
2016-02-07 12:35:18 -08:00
|
|
|
emit_dig_mutation
|
2016-04-03 16:38:23 -07:00
|
|
|
emit_drop_mutation
|
2015-10-18 22:34:00 -07:00
|
|
|
end
|
|
|
|
|
2015-10-08 21:58:59 -07:00
|
|
|
# Emit selector mutations specific to top level constants
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
def emit_receiver_selector_mutations
|
|
|
|
return unless meta.receiver_possible_top_level_const?
|
|
|
|
|
|
|
|
RECEIVER_SELECTOR_REPLACEMENTS
|
|
|
|
.fetch(receiver.children.last, EMPTY_HASH)
|
|
|
|
.fetch(selector, EMPTY_ARRAY)
|
|
|
|
.each(&method(:emit_selector))
|
|
|
|
end
|
|
|
|
|
2016-02-07 12:35:18 -08:00
|
|
|
# Emit mutation for `#dig`
|
|
|
|
#
|
|
|
|
# - Mutates `foo.dig(a, b)` to `foo.fetch(a).dig(b)`
|
|
|
|
# - Mutates `foo.dig(a)` to `foo.fetch(a)`
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
def emit_dig_mutation
|
|
|
|
return if !selector.equal?(:dig) || arguments.none?
|
|
|
|
|
|
|
|
head, *tail = arguments
|
|
|
|
|
|
|
|
fetch_mutation = s(:send, receiver, :fetch, head)
|
|
|
|
|
|
|
|
return emit(fetch_mutation) if tail.empty?
|
|
|
|
|
|
|
|
emit(s(:send, fetch_mutation, :dig, *tail))
|
|
|
|
end
|
|
|
|
|
2016-04-03 16:38:23 -07:00
|
|
|
# Emit mutation `foo[n..-1]` -> `foo.drop(n)`
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
def emit_drop_mutation
|
|
|
|
return if !selector.equal?(:[]) || !arguments.one? || !n_irange?(arguments.first)
|
|
|
|
|
|
|
|
start, ending = *arguments.first
|
|
|
|
|
|
|
|
return unless ending.eql?(s(:int, -1))
|
|
|
|
|
|
|
|
emit(s(:send, receiver, :drop, start))
|
|
|
|
end
|
|
|
|
|
2015-10-18 22:34:00 -07:00
|
|
|
# Emit mutation from `to_i` to `Integer(...)`
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
def emit_integer_mutation
|
|
|
|
return unless selector.equal?(:to_i)
|
|
|
|
|
|
|
|
emit(s(:send, nil, :Integer, receiver))
|
|
|
|
end
|
|
|
|
|
2015-08-19 19:30:35 -04:00
|
|
|
# Emit mutation from `const_get` to const literal
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
def emit_const_get_mutation
|
|
|
|
return unless selector.equal?(:const_get) && n_sym?(arguments.first)
|
|
|
|
|
|
|
|
emit(s(:const, receiver, AST::Meta::Symbol.new(arguments.first).name))
|
|
|
|
end
|
|
|
|
|
2013-09-02 21:29:44 +02:00
|
|
|
# Emit selector replacement
|
2013-08-02 00:46:50 +02:00
|
|
|
#
|
|
|
|
# @return [undefined]
|
2013-09-02 21:29:44 +02:00
|
|
|
def emit_selector_replacement
|
2015-07-19 09:22:07 +00:00
|
|
|
SELECTOR_REPLACEMENTS.fetch(selector, EMPTY_ARRAY).each(&method(:emit_selector))
|
2013-08-02 00:46:50 +02:00
|
|
|
end
|
|
|
|
|
2013-07-14 21:37:22 +02:00
|
|
|
# Emit naked receiver mutation
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
def emit_naked_receiver
|
2016-03-06 21:19:32 +00:00
|
|
|
emit(receiver) if receiver
|
2013-07-14 21:37:22 +02:00
|
|
|
end
|
|
|
|
|
2013-06-14 19:47:23 +02:00
|
|
|
# Mutate arguments
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
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
|
|
|
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]
|
|
|
|
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]
|
|
|
|
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) ||
|
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
|