Class: Mutineer::Mutators::Base

Inherits:
Prism::Visitor
  • Object
show all
Defined in:
lib/mutineer/mutators/base.rb

Overview

Base Prism visitor for operators.

Subclasses override visit_* methods to push Mutation objects onto @mutations. Visiting only def_node.body is the body-only enforcement: the def signature line is never touched.

Instance Method Summary collapse

Instance Method Details

#dangling_heredoc?(mutation, node = @body) ⇒ Boolean (private)

Returns whether the mutation replaces a heredoc opener and keeps the opener without its body, or the body without its opener.

Parameters:

  • mutation (Mutineer::Mutation) —

    candidate edit.

  • node (Prism::Node, nil) (defaults to: @body) —

    subtree to search. Defaults to the body.

Returns:

  • (Boolean) —

    true when the edit would split a heredoc from its body.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/mutineer/mutators/base.rb', line 98

def dangling_heredoc?(mutation, node = @body)
  return false if node.nil? || !node.is_a?(Prism::Node)

  if node.respond_to?(:heredoc?) && node.heredoc? && node.respond_to?(:opening_loc)
    opening = node.opening_loc
    if opening && opening.start_offset >= mutation.start_offset && opening.start_offset < mutation.end_offset
      closing = node.respond_to?(:closing_loc) ? node.closing_loc : nil
      hangs = closing.nil? || closing.end_offset > mutation.end_offset
      opener = @source.byteslice(opening.start_offset, opening.end_offset - opening.start_offset)
      kept = !opener.empty? && mutation.replacement.include?(opener)
      if hangs
        return true unless kept
      elsif kept
        whole = @source.byteslice(opening.start_offset, closing.end_offset - opening.start_offset)
        return true unless mutation.replacement.include?(whole)
      end
    end
  end

  node.compact_child_nodes.any? { |child| dangling_heredoc?(mutation, child) }
end

#decline ⇒ nil (private)

Counts one declined candidate.

Returns:

  • (nil) —

    so a skip line reads return decline if ....



69
70
71
72
# File 'lib/mutineer/mutators/base.rb', line 69

def decline
  @declined += 1
  nil
end

#declined ⇒ Integer

Returns how many candidates the last #mutations_for call declined: an edit that would split a heredoc from its body, or an equivalent skip listed in docs/equivalent-skips.md. A declined candidate is never a mutation, so it is not in the run's total.

Returns:

  • (Integer) —

    declined candidates; 0 before the first call.



60
61
62
# File 'lib/mutineer/mutators/base.rb', line 60

def declined
  @declined || 0
end

#drop_dangling_heredocs ⇒ void (private)

This method returns an undefined value.

Drops a mutation that splits a heredoc opener from its body.

The body sits past the opener's node. Replacing the opener with text that does not contain it turns the body into Ruby, which parses and then raises. A replacement that still contains the opener keeps the body attached, so that mutation stays. The reverse also breaks: when the replaced range holds the body but the replacement keeps only the opener, the opener has no body and the source does not parse. A replacement that copies the opener and the body together stays. Each dropped mutation counts as declined.



86
87
88
89
90
# File 'lib/mutineer/mutators/base.rb', line 86

def drop_dangling_heredocs
  before = @mutations.size
  @mutations.reject! { |mutation| dangling_heredoc?(mutation) }
  @declined += before - @mutations.size
end

#heredoc?(node) ⇒ Boolean (private)

Returns whether a node is, or contains, a heredoc.

A heredoc's body lies outside its node's byte range. A mutation that deletes the node leaves the body behind as code, so the mutant always raises.

Parameters:

  • node (Prism::Node) —

    node to inspect.

Returns:

  • (Boolean) —

    true when a heredoc is inside the node.



128
129
130
131
# File 'lib/mutineer/mutators/base.rb', line 128

def heredoc?(node)
  (node.respond_to?(:heredoc?) && node.heredoc?) ||
    node.compact_child_nodes.any? { |child| heredoc?(child) }
end

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

Walks the subject body and collects mutations.

Parameters:

  • subject (Mutineer::Subject) —

    subject whose body is visited.

  • source (String) —

    full source text for byte-based slicing.

Returns:



20
21
22
23
24
25
26
27
28
# File 'lib/mutineer/mutators/base.rb', line 20

def mutations_for(subject, source)
  @source = source
  @mutations = []
  @declined = 0
  @body = subject.def_node.body
  @body&.accept(self)
  drop_dangling_heredocs
  @mutations
end

#visit_def_node(node) ⇒ void

This method returns an undefined value.

Skips a nested method definition. The project finds it as a subject of its own, so a visit here counts its mutations twice. Inside class << obj, the project finds no subject, so the visit continues.

Parameters:

  • node (Prism::DefNode) —

    nested definition node.



36
37
38
# File 'lib/mutineer/mutators/base.rb', line 36

def visit_def_node(node)
  super if @in_undiscovered
end

#visit_singleton_class_node(node) ⇒ void

This method returns an undefined value.

Tracks a class << obj whose obj is not self. The project does not recurse into it, so its defs are no subjects (see Project::SubjectVisitor#visit_singleton_class_node).

Parameters:

  • node (Prism::SingletonClassNode) —

    singleton-class node.



46
47
48
49
50
51
52
# File 'lib/mutineer/mutators/base.rb', line 46

def visit_singleton_class_node(node)
  return super if node.expression.is_a?(Prism::SelfNode)

  outer, @in_undiscovered = @in_undiscovered, true
  super
  @in_undiscovered = outer
end