Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unused group name mutation #1200

Merged
merged 1 commit into from Jan 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions Changelog.md
@@ -1,5 +1,8 @@
# Unreleased

* [#1200](https://github.com/mbj/mutant/pull/1200)
* Add unused group name mutation: `/(?<foo>bar)/` -> `/(?<_foo>bar)/`.

* [#1205](https://github.com/mbj/mutant/pull/1205)

* Add `mutant environment test list` subcommand.
Expand Down
16 changes: 15 additions & 1 deletion lib/mutant/mutator/node/regexp/named_group.rb
Expand Up @@ -15,8 +15,22 @@ class NamedGroup < Node
def dispatch
return unless group

emit(s(:regexp_passive_group, group))
emit_group_mutations

# Allows unused captures to be kept and named if they are explicitly prefixed with an
# underscore, like we allow with unused local variables.
return if name_underscored?

emit(s(:regexp_passive_group, group))
emit_name_underscore_mutation
end

def emit_name_underscore_mutation
emit_type("_#{name}", group)
end

def name_underscored?
name.start_with?('_')
end
end # EndOfLineAnchor
end # Regexp
Expand Down
10 changes: 10 additions & 0 deletions meta/regexp/regexp_named_group.rb
Expand Up @@ -15,4 +15,14 @@

mutation '/(?:\w)/'
mutation '/(?<foo>\W)/'
mutation '/(?<_foo>\w)/'
end

Mutant::Meta::Example.add :regexp_named_group do
source '/(?<_foo>\w)/'

singleton_mutations
regexp_mutations

mutation '/(?<_foo>\W)/'
end