Class: Mutineer::Mutators::ReturnNil
- Defined in:
- lib/mutineer/mutators/return_nil.rb
Overview
Return-nil mutator.
Replaces explicit return values and final expressions with nil.
Instance Method Summary collapse
-
#emit(loc) ⇒ void
private
Emits a return-nil mutation.
-
#final_expression_nil(body) ⇒ void
private
Mutates a method's final expression to nil when eligible.
-
#mutations_for(subject, source) ⇒ Array<Mutineer::Mutation>
Collects return-nil mutations for a subject.
-
#visit_return_node(node) ⇒ void
Visits return nodes.
Methods inherited from Base
#dangling_heredoc?, #decline, #declined, #drop_dangling_heredocs, #heredoc?, #visit_def_node, #visit_singleton_class_node
Instance Method Details
#emit(loc) ⇒ void (private)
This method returns an undefined value.
Emits a return-nil mutation.
80 81 82 83 84 85 86 87 |
# File 'lib/mutineer/mutators/return_nil.rb', line 80 def emit(loc) @mutations << Mutation.new( start_offset: loc.start_offset, end_offset: loc.end_offset, replacement: "nil", operator: :return_nil ) end |
#final_expression_nil(body) ⇒ void (private)
This method returns an undefined value.
Mutates a method's final expression to nil when eligible.
A def-level rescue, else or ensure makes the body a BeginNode
(#418). The method then returns the else value, or the main body's
value when there is no else, or the value of the rescue clause that
ran. Each of those final expressions becomes nil. The ensure value is
discarded, so it is not a final expression. A final explicit
begin ... end statement is handled the same way, so RuboCop's
Style/RedundantBegin autocorrect does not change the mutants (#470).
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/mutineer/mutators/return_nil.rb', line 58 def final_expression_nil(body) case body when Prism::StatementsNode last = body.body.last return if last.nil? || last.is_a?(Prism::ReturnNode) || last.is_a?(Prism::NilNode) return final_expression_nil(last) if last.is_a?(Prism::BeginNode) emit(last.location) when Prism::BeginNode final_expression_nil(body.else_clause ? body.else_clause.statements : body.statements) clause = body.rescue_clause while clause final_expression_nil(clause.statements) clause = clause.subsequent # Prism 2 removed the `consequent` alias. end end end |
#mutations_for(subject, source) ⇒ Array<Mutineer::Mutation>
Collects return-nil mutations for a subject.
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/mutineer/mutators/return_nil.rb', line 16 def mutations_for(subject, source) @source = source @mutations = [] @declined = 0 @body = subject.def_node.body if @body @body.accept(self) # rule 1 (explicit return nodes in this body) final_expression_nil(@body) # rule 2 (method's final expression) end drop_dangling_heredocs @mutations end |
#visit_return_node(node) ⇒ void
This method returns an undefined value.
Visits return nodes.
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/mutineer/mutators/return_nil.rb', line 33 def visit_return_node(node) args = node.arguments if args values = args.arguments unless values.size == 1 && values.first.is_a?(Prism::NilNode) emit(args.location) end end super end |