Class: Mutineer::Mutators::LiteralMutation
- 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
-
#emit(loc, replacement) ⇒ void
private
Emits a literal mutation.
-
#mutate_string(node, quoted:) ⇒ void
private
Empties a non-empty string.
-
#visit_integer_node(node) ⇒ void
Visits integer literals.
-
#visit_interpolated_string_node(node) ⇒ void
Visits an interpolated string.
-
#visit_string_node(node) ⇒ void
Visits string literals.
Methods inherited from Base
#dangling_heredoc?, #decline, #declined, #drop_dangling_heredocs, #heredoc?, #mutations_for, #visit_def_node, #visit_singleton_class_node
Instance Method Details
#emit(loc, replacement) ⇒ void (private)
This method returns an undefined value.
Emits a literal mutation.
71 72 73 74 75 76 77 78 |
# File 'lib/mutineer/mutators/literal_mutation.rb', line 71 def emit(loc, replacement) @mutations << Mutation.new( start_offset: loc.start_offset, end_offset: loc.end_offset, replacement: replacement, operator: :literal_mutation ) end |
#mutate_string(node, quoted:) ⇒ void (private)
This method returns an undefined value.
Empties a non-empty string. A string with no quotes of its own (a
heredoc line, a %(...) or %Q(...) part, a %w[] element, a regex,
backtick or interpolated-symbol part) cannot be emptied with "": the edit inserts text
and can break the parse (#469). Such a string is declined.
57 58 59 60 61 62 63 64 |
# File 'lib/mutineer/mutators/literal_mutation.rb', line 57 def mutate_string(node, quoted:) return if node.unescaped.empty? return decline unless quoted loc = node.location token = @source.byteslice(loc.start_offset...loc.end_offset) emit(loc, '""') unless token == '""' || token == "''" end |
#visit_integer_node(node) ⇒ void
This method returns an undefined value.
Visits integer literals.
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_interpolated_string_node(node) ⇒ void
This method returns an undefined value.
Visits an interpolated string. In a "..." string, "" over a text
part closes and reopens the string, so it empties the part. Any other
text part is handled as a string with no quotes of its own.
29 30 31 32 33 34 35 36 |
# File 'lib/mutineer/mutators/literal_mutation.rb', line 29 def visit_interpolated_string_node(node) double_quoted = node.opening_loc&.slice == '"' node.parts.each do |part| next part.accept(self) unless part.is_a?(Prism::StringNode) mutate_string(part, quoted: double_quoted || part.opening_loc) end end |
#visit_string_node(node) ⇒ void
This method returns an undefined value.
Visits string literals.
42 43 44 45 |
# File 'lib/mutineer/mutators/literal_mutation.rb', line 42 def visit_string_node(node) mutate_string(node, quoted: node.opening_loc) super end |