mutant icon indicating copy to clipboard operation
mutant copied to clipboard

Do not mutate method parameter names when `super` (zsuper) is used

Open dgollahon opened this issue 4 years ago • 4 comments

When I have code like this:

def initialize(a, b = 5)
  super(a, b)
end

mutant will correctly point out that I could write:

def initialize(a, b = 5)
  super
end

Unfortunately, it will then also mutate the names to

def initialize(_a, b = 5)
  super
end

and

def initialize(a, _b = 5)
  super
end

I find this weird/confusing because the variables are actually not unused (which is the general convention for _variable_name). I'd rather keep my non-underscored, descriptive names in this case and I think mutant should not emit the rename mutations when local variables can be statically determined as referenced like in this case with the implicit reference via zsuper.

dgollahon avatar Dec 08 '20 06:12 dgollahon

Detecting the presence of zsuper in the body is easy. I support this change.

mbj avatar Dec 08 '20 13:12 mbj

@mbj I would be happy to implement this but I am not sure what the best way to check for the presence zsuper would be since the Argument mutator does not have access to the method body. Is there a recommended pattern for making that kind of context available? Should the Define mutator try to reach into the arguments node and handle the _ mutation since it is able to inspect its body?

dgollahon avatar Dec 13 '20 07:12 dgollahon

@dgollahon You can use the parent_node method to peek up and into a sibling. But this is discouraged and a method of last resort.

For now this may be the best solution, till I push the new mutation generation engine here that is more flexible, and is more declarative.

mbj avatar Dec 14 '20 02:12 mbj

Aha! I didn't realize there was a parent_node method. I think that makes the most sense for this case until we have something more expressive.

dgollahon avatar Dec 14 '20 02:12 dgollahon