Skip to content

Commit

Permalink
Add unused group name mutation
Browse files Browse the repository at this point in the history
- Mutates capture group names: `/(?<foo>bar)/` -> `/(?<_foo>bar)/`.
- Closes #573. NOTE: I have deviated from the issue slightly and chosen a `_` prefix so that, like with local variables, names can be left in unused if they are explicitly marked as such. I have decided to extend the behavior to not mutating to a non-capture group as well in this case because the `_` prefix seems like a clear choice that the user would rather keep the label even if the capturing functionality is unused.
  • Loading branch information
dgollahon committed Jan 3, 2021
1 parent 8e9d51c commit 2e09662
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Unreleased

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

# v0.10.25 2021-01-02-03

* [#1194](https://github.com/mbj/mutant/pull/1194)
Expand Down
16 changes: 15 additions & 1 deletion lib/mutant/mutator/node/regexp/named_group.rb
Original file line number Diff line number Diff line change
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 unless 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
Original file line number Diff line number Diff line change
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

0 comments on commit 2e09662

Please sign in to comment.