Class: Evilution::Mutator::Operator::StringLiteral

Inherits:
Base
  • Object
show all
Defined in:
lib/evilution/mutator/operator/string_literal.rb

Instance Attribute Summary

Attributes inherited from Base

#mutations

Instance Method Summary collapse

Methods inherited from Base

#call, clear_parse_cache!, operator_name, parsed_tree_for

Constructor Details

#initialize(skip_heredoc_literals: false, **rest) ⇒ StringLiteral

Returns a new instance of StringLiteral.



6
7
8
9
# File 'lib/evilution/mutator/operator/string_literal.rb', line 6

def initialize(skip_heredoc_literals: false, **rest)
  super(**rest)
  @skip_heredoc_literals = skip_heredoc_literals
end

Instance Method Details

#visit_interpolated_regular_expression_node(node) ⇒ Object



57
58
59
# File 'lib/evilution/mutator/operator/string_literal.rb', line 57

def visit_interpolated_regular_expression_node(node)
  visit_non_string_parts(node)
end

#visit_interpolated_string_node(node) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/evilution/mutator/operator/string_literal.rb', line 11

def visit_interpolated_string_node(node)
  if node.heredoc?
    return if @skip_heredoc_literals

    node.parts.each do |part|
      next if part.is_a?(Prism::StringNode)

      visit(part)
    end
    return
  end

  # Adjacent-string concatenation — both `"foo" "bar"` and the line-continued
  # form `"foo" \\\n "bar"` — lands in an InterpolatedStringNode whose parts
  # are all StringNodes. Mutating chunks individually splices the wrong span:
  # for the continued form Ruby treats `nil \\\n "rest"` as a confusing
  # parse rather than a clean nil; for both forms the result is one StringNode
  # plus an orphaned adjacent literal, not a meaningful mutation of the whole
  # expression. Replace the entire concatenation in one shot instead.
  if adjacent_string_concat?(node)
    emit_string_mutations(node)
    return
  end

  super
end

#visit_interpolated_symbol_node(node) ⇒ Object

Inner StringNode chunks of an interpolated symbol (‘:“visit_#type”`), interpolated regular expression (`/^#needle/`), or interpolated x-string (“ `echo #cmd` “) are not free string literals — they are fragments of a different literal kind. Mutating them splices empty-string bytes into the middle of a `:“…”` / `/…/` / “ `…` “ token, producing unparseable code. Visit only the interpolation parts (which may contain mutatable expressions); skip the raw StringNode chunks.



53
54
55
# File 'lib/evilution/mutator/operator/string_literal.rb', line 53

def visit_interpolated_symbol_node(node)
  visit_non_string_parts(node)
end

#visit_interpolated_x_string_node(node) ⇒ Object



61
62
63
# File 'lib/evilution/mutator/operator/string_literal.rb', line 61

def visit_interpolated_x_string_node(node)
  visit_non_string_parts(node)
end

#visit_string_node(node) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/evilution/mutator/operator/string_literal.rb', line 38

def visit_string_node(node)
  return super if node.heredoc?

  emit_string_mutations(node)

  super
end