Class: Mutineer::Mutators::LiteralMutation

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

Overview

Literal mutation mutator.

Mutates integers and strings with the Tier-2 literal rules.

Instance Method Summary collapse

Methods inherited from Base

#heredoc?, #mutations_for

Instance Method Details

#emit(loc, replacement) ⇒ void (private)

This method returns an undefined value.

Emits a literal mutation.

Parameters:

  • loc (Prism::Location) —

    source location.

  • replacement (String) —

    replacement literal.



41
42
43
44
45
46
47
48
# File 'lib/mutineer/mutators/literal_mutation.rb', line 41

def emit(loc, replacement)
  @mutations << Mutation.new(
    start_offset: loc.start_offset,
    end_offset: loc.end_offset,
    replacement: replacement,
    operator: :literal_mutation
  )
end

#visit_integer_node(node) ⇒ void

This method returns an undefined value.

Visits integer literals.

Parameters:

  • node (Prism::IntegerNode) —

    node to inspect.



15
16
17
18
19
20
21
# File 'lib/mutineer/mutators/literal_mutation.rb', line 15

def visit_integer_node(node)
  n = node.value
  emit(node.location, "0") unless n.zero?
  emit(node.location, "1") unless n == 1
  emit(node.location, (n + 1).to_s)
  super
end

#visit_string_node(node) ⇒ void

This method returns an undefined value.

Visits string literals.

Parameters:

  • node (Prism::StringNode) —

    node to inspect.



27
28
29
30
31
32
# File 'lib/mutineer/mutators/literal_mutation.rb', line 27

def visit_string_node(node)
  loc = node.location
  token = @source.byteslice(loc.start_offset...loc.end_offset)
  emit(loc, '""') unless token == '""' || token == "''" || node.unescaped.empty?
  super
end