Class: Mutineer::Mutators::CallToReceiver

Inherits:
Base
  • Object
show all
Defined in:
lib/mutineer/mutators/call_to_receiver.rb

Overview

Call-to-receiver mutator (Tier-2).

Replaces the last call of a chain with its receiver, with the arguments and the block of that call: user.full_name becomes user, and ids.uniq.sort becomes ids.uniq. A chain begins at a receiver that is not a dotted call, as in ChainLink. A call in an argument or a block starts a chain of its own.

The mutant survives when no test checks what the last call does: a strip, a uniq, a compact or a lookup the tests never see.

The operator does not overlap with ChainLink. chain_link drops a link in the middle of a chain, and this operator drops the last call. A call that is the receiver of a dotted call, or of a call write or a call target, is a link, so it gets no mutant here. The two operators give the same source only when the last call repeats the link before it, as in node.name.name. When both edits start on the same line, the job plan runs one copy, the chain_link one (#275).

Calls in SKIPPED are never replaced; each one counts as declined. The list is shorter than Mutineer::Mutators::ChainLink::SKIPPED: on the last call, a conversion such as to_s or to_i decides the type of the value that the expression returns, so m[1].to_i becomes m[1]. Foo.new becomes the class, and almost any use of the class raises.

A call is not replaced when its name is an operator (a.+(1)), or a setter (a.b = 1). A call with no dot (a + b, a[0]) is not replaced.

Constant Summary collapse

WORD_NAME =

Matches the name of a call written as a word, not as an operator.

/\A[[:alpha:]_]/
SKIPPED =

Method names whose last call is never replaced. A name is here when its mutant is most often equivalent, or when almost any test that runs the line kills it. freeze and itself return the receiver. dup and clone return a copy, so the mutant shows when code later changes the copy or the original, or compares the result with the original by identity. Dropping to_set most often leaves an Array, which answers include? and empty? as a Set does (it keeps duplicates, so size and each can differ). new leaves the class in place of an instance, which almost any test that runs the line kills.

%i[new dup clone freeze itself to_set].freeze

Instance Method Summary collapse

Methods inherited from Base

#dangling_heredoc?, #decline, #declined, #drop_dangling_heredocs, #heredoc?, #visit_def_node, #visit_singleton_class_node

Instance Method Details

#dotted?(node) ⇒ Boolean (private)

Returns whether a node is a call through ., &. or ::.

Parameters:

  • node (Prism::Node, nil) —

    node to inspect.

Returns:

  • (Boolean) —

    true for a call node with a call operator.



143
144
145
# File 'lib/mutineer/mutators/call_to_receiver.rb', line 143

def dotted?(node)
  node.is_a?(Prism::CallNode) && !node.call_operator_loc.nil?
end

#mark(node) ⇒ void (private)

This method returns an undefined value.

Records the receiver of a node as a link when the node is a dotted call or a call write or target. The receiver of a call with no dot is not a link. It is the last call of a chain of its own.

Parameters:

  • node (Prism::Node) —

    node that responds to receiver.



115
116
117
# File 'lib/mutineer/mutators/call_to_receiver.rb', line 115

def mark(node)
  @links[node.receiver] = true if dotted?(node) || !node.is_a?(Prism::CallNode)
end

#mutations_for(subject, source) ⇒ Array<Mutineer::Mutation>

Resets the per-subject record of calls that are links.

Parameters:

  • subject (Mutineer::Subject) —

    subject whose body is visited.

  • source (String) —

    full source text for byte-based slicing.

Returns:



54
55
56
57
# File 'lib/mutineer/mutators/call_to_receiver.rb', line 54

def mutations_for(subject, source)
  @links = {}.compare_by_identity
  super
end

#replace(node) ⇒ void (private)

This method returns an undefined value.

Emits the replacement of a call by its receiver, unless the name is an operator, a setter, or in SKIPPED.

Parameters:

  • node (Prism::CallNode) —

    the dotted call to replace.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/mutineer/mutators/call_to_receiver.rb', line 124

def replace(node)
  name = node.name.to_s
  return unless name.match?(WORD_NAME)
  return if name.end_with?("=")
  return decline if SKIPPED.include?(node.name)

  receiver = node.receiver.location
  @mutations << Mutation.new(
    start_offset: node.location.start_offset,
    end_offset: node.location.end_offset,
    replacement: @source.byteslice(receiver.start_offset, receiver.end_offset - receiver.start_offset),
    operator: :call_to_receiver
  )
end

#visit_call_and_write_node(node) ⇒ void

This method returns an undefined value.

Visits a.b.c &&= 1. Its receiver is a link.

Parameters:

  • node (Prism::CallAndWriteNode) —

    node to inspect.



92
93
94
95
# File 'lib/mutineer/mutators/call_to_receiver.rb', line 92

def visit_call_and_write_node(node)
  mark(node)
  super
end

#visit_call_node(node) ⇒ void

This method returns an undefined value.

Visits a call. A dotted call that is not a link is the last call of its chain.

Parameters:

  • node (Prism::CallNode) —

    call node to inspect.



64
65
66
67
68
# File 'lib/mutineer/mutators/call_to_receiver.rb', line 64

def visit_call_node(node)
  mark(node)
  replace(node) if dotted?(node) && !@links.key?(node)
  super
end

#visit_call_operator_write_node(node) ⇒ void

This method returns an undefined value.

Visits a.b.c += 1. Its receiver is a link.

Parameters:

  • node (Prism::CallOperatorWriteNode) —

    node to inspect.



74
75
76
77
# File 'lib/mutineer/mutators/call_to_receiver.rb', line 74

def visit_call_operator_write_node(node)
  mark(node)
  super
end

#visit_call_or_write_node(node) ⇒ void

This method returns an undefined value.

Visits a.b.c ||= 1. Its receiver is a link.

Parameters:

  • node (Prism::CallOrWriteNode) —

    node to inspect.



83
84
85
86
# File 'lib/mutineer/mutators/call_to_receiver.rb', line 83

def visit_call_or_write_node(node)
  mark(node)
  super
end

#visit_call_target_node(node) ⇒ void

This method returns an undefined value.

Visits a call used as an assignment target, as in a.b.c, d = 1, 2. Its receiver is a link.

Parameters:

  • node (Prism::CallTargetNode) —

    node to inspect.



102
103
104
105
# File 'lib/mutineer/mutators/call_to_receiver.rb', line 102

def visit_call_target_node(node)
  mark(node)
  super
end