Class: Mutineer::Mutators::RangeLiteral

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

Overview

Range mutator (Tier-2).

Swaps .. with ... and ... with .., one mutation per range. The .. -> ... mutant survives when no test checks the last element. Flip-flops are FlipFlopNode, not RangeNode, so this mutator skips them.

An endless range (1.., 1..nil) is skipped only when it is a direct index argument of a call in SLICE_CALLS or of an index write ("ab"[1..] += x) and the receiver is an array or string literal. There (1..) and (1...) select the same elements, so the mutant is equivalent, and each skipped range counts as declined. A variable, a call, or a constant is swapped. The syntax does not record that receiver's class, and a Hash, a Proc, or a custom [] can tell the two ranges apart. Every other endless range is swapped, because the two ranges differ on ==, hash, exclude_end? and to_s, so a test can kill it. This includes the value of h[:k] = 1.. and a call on a class, such as Array[1..], which builds a value and does not slice.

Constant Summary collapse

SWAPS =

Maps each range operator to its opposite.

{ ".." => "...", "..." => ".." }.freeze
SLICE_CALLS =

Calls whose direct endless-range index arguments are not mutated when the receiver is an array or string literal. Those calls select the same elements for (1..) and (1...). The assigned value of []= is not an index, so it is mutated.

%i[[] []= slice slice! byteslice bytesplice values_at].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

#begin_is_sequence?(node) ⇒ Boolean (private)

Whether every value a begin can return is an array or string literal. The value is the else, or the body when there is no else. Each rescue can replace it. ensure runs but does not supply the value.

Parameters:

  • node (Prism::BeginNode) —

    begin expression used as a receiver.

Returns:

  • (Boolean)


155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/mutineer/mutators/range_literal.rb', line 155

def begin_is_sequence?(node)
  success = if node.else_clause
              statement_value(node.else_clause.statements)
            else
              statement_value(node.statements)
            end
  return false unless sequence_receiver?(success)

  rescue_clause = node.rescue_clause
  while rescue_clause
    return false unless sequence_receiver?(statement_value(rescue_clause.statements))

    rescue_clause = rescue_clause.subsequent
  end
  true
end

#emit(node) ⇒ void (private)

This method returns an undefined value.

Emits the mutation that swaps the range operator.

Parameters:

  • node (Prism::RangeNode) —

    range node to mutate.



193
194
195
196
197
198
199
200
201
# File 'lib/mutineer/mutators/range_literal.rb', line 193

def emit(node)
  loc = node.operator_loc
  @mutations << Mutation.new(
    start_offset: loc.start_offset,
    end_offset: loc.end_offset,
    replacement: SWAPS.fetch(loc.slice),
    operator: :range
  )
end

#endless?(node) ⇒ Boolean (private)

Whether the range has no end: 1.. or 1..nil.

Parameters:

  • node (Prism::RangeNode) —

    range node to inspect.

Returns:

  • (Boolean)


185
186
187
# File 'lib/mutineer/mutators/range_literal.rb', line 185

def endless?(node)
  node.right.nil? || node.right.is_a?(Prism::NilNode)
end

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

Resets the per-subject record of endless ranges that are slice arguments.

Parameters:

  • subject (Mutineer::Subject) —

    subject whose body is visited.

  • source (String) —

    full source text for byte-based slicing.

Returns:



39
40
41
42
# File 'lib/mutineer/mutators/range_literal.rb', line 39

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

#record_slice_args(node) ⇒ void (private)

This method returns an undefined value.

Records each range that is a direct argument of the node, when the receiver is an array or string literal. For a []= call the last argument is the assigned value, not an index, so it is left out. A variable, call, or constant is not recorded: its class is not fixed by the syntax, so the range is swapped.

Parameters:

  • node (Prism::Node) —

    call or index node that responds to arguments.



117
118
119
120
121
122
123
# File 'lib/mutineer/mutators/range_literal.rb', line 117

def record_slice_args(node)
  return unless sequence_receiver?(node.receiver)

  args = node.arguments&.arguments || []
  args = args[0...-1] if node.is_a?(Prism::CallNode) && node.name == :[]=
  args.each { |arg| @slice_args[arg] = true if arg.is_a?(Prism::RangeNode) }
end

#sequence_receiver?(node) ⇒ Boolean (private)

Whether the receiver's syntax is an Array or a String. A quoted string or an array literal is. Parentheses are unwrapped to the value they return, so ("ab")[1..] is still a string. A begin counts only when every branch that can supply its value is one of those literals: a rescue or an else can return a Hash instead. Backticks are a call to `, which can be overridden, so they are not literals. A variable, call, or constant is not either: it can hold a Hash, a Proc, or a custom object, and an earlier assignment is not followed.

Parameters:

  • node (Prism::Node, nil) —

    receiver of a slice call or index write.

Returns:

  • (Boolean)


136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/mutineer/mutators/range_literal.rb', line 136

def sequence_receiver?(node)
  case node
  when Prism::ArrayNode, Prism::StringNode, Prism::InterpolatedStringNode
    true
  when Prism::ParenthesesNode
    sequence_receiver?(statement_value(node.body))
  when Prism::BeginNode
    begin_is_sequence?(node)
  else
    false
  end
end

#statement_value(node) ⇒ Prism::Node? (private)

The value of a statement list, which is its last statement. Any other node is itself.

Parameters:

  • node (Prism::Node, nil) —

    statements, or a single expression.

Returns:

  • (Prism::Node, nil)


177
178
179
# File 'lib/mutineer/mutators/range_literal.rb', line 177

def statement_value(node)
  node.is_a?(Prism::StatementsNode) ? node.body.last : node
end

#visit_call_node(node) ⇒ void

This method returns an undefined value.

Visits a call. Records the range arguments of a call in SLICE_CALLS when the receiver is an array or string literal.

Parameters:

  • node (Prism::CallNode) —

    call node to inspect.



49
50
51
52
# File 'lib/mutineer/mutators/range_literal.rb', line 49

def visit_call_node(node)
  record_slice_args(node) if SLICE_CALLS.include?(node.name)
  super
end

#visit_index_and_write_node(node) ⇒ void

This method returns an undefined value.

Visits a[1..] &&= x. Records its index arguments when the receiver is an array or string literal.

Parameters:

  • node (Prism::IndexAndWriteNode) —

    node to inspect.



79
80
81
82
# File 'lib/mutineer/mutators/range_literal.rb', line 79

def visit_index_and_write_node(node)
  record_slice_args(node)
  super
end

#visit_index_operator_write_node(node) ⇒ void

This method returns an undefined value.

Visits a[1..] += x. Records its index arguments when the receiver is an array or string literal.

Parameters:

  • node (Prism::IndexOperatorWriteNode) —

    node to inspect.



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

def visit_index_operator_write_node(node)
  record_slice_args(node)
  super
end

#visit_index_or_write_node(node) ⇒ void

This method returns an undefined value.

Visits a[1..] ||= x. Records its index arguments when the receiver is an array or string literal.

Parameters:

  • node (Prism::IndexOrWriteNode) —

    node to inspect.



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

def visit_index_or_write_node(node)
  record_slice_args(node)
  super
end

#visit_index_target_node(node) ⇒ void

This method returns an undefined value.

Visits an index used as an assignment target, as in a[1..], b = x, y. Records its index arguments when the receiver is an array or string literal.

Parameters:

  • node (Prism::IndexTargetNode) —

    node to inspect.



89
90
91
92
# File 'lib/mutineer/mutators/range_literal.rb', line 89

def visit_index_target_node(node)
  record_slice_args(node)
  super
end

#visit_range_node(node) ⇒ void

This method returns an undefined value.

Visits range nodes and emits range mutations.

Parameters:

  • node (Prism::RangeNode) —

    range node to inspect.



98
99
100
101
102
103
104
105
# File 'lib/mutineer/mutators/range_literal.rb', line 98

def visit_range_node(node)
  if endless?(node) && @slice_args.key?(node)
    decline
  else
    emit(node)
  end
  super # nested ranges ((1..2)...(3..4)) each get their own mutation
end