Skip to content

Commit

Permalink
Add named capture to non-capturing group mutation
Browse files Browse the repository at this point in the history
- Mutate named capture groups to passive groups: `/(?<foo>bar)/` -> `/(?:bar)/`.
  • Loading branch information
dgollahon committed Jan 2, 2021
1 parent 5207ce2 commit 037673a
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Unreleased

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

* Add mutation from named capturing group to non-capturing group: `/(?<foo>bar)/` -> `/(?:bar)`.

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

* Add mutation from `=~` -> `#match?`
Expand Down
1 change: 1 addition & 0 deletions lib/mutant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ module Mutant
require 'mutant/mutator/node/regexp'
require 'mutant/mutator/node/regexp/alternation_meta'
require 'mutant/mutator/node/regexp/capture_group'
require 'mutant/mutator/node/regexp/named_group'
require 'mutant/mutator/node/regexp/character_type'
require 'mutant/mutator/node/regexp/end_of_line_anchor'
require 'mutant/mutator/node/regexp/end_of_string_or_before_end_of_line_anchor'
Expand Down
25 changes: 25 additions & 0 deletions lib/mutant/mutator/node/regexp/named_group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module Mutant
class Mutator
class Node
module Regexp
# Mutator for regexp named capture groups, such as `/(?<foo>bar)/`
class NamedGroup < Node
handle(:regexp_named_group)

children :name, :group

private

def dispatch
return unless group

emit(s(:regexp_passive_group, group))
emit_group_mutations
end
end # EndOfLineAnchor
end # Regexp
end # Node
end # Mutator
end # Mutant
1 change: 1 addition & 0 deletions meta/regexp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
singleton_mutations
mutation '//'
mutation '/nomatch\A/'
mutation '/(?:.)/'
end

Pathname
Expand Down
18 changes: 18 additions & 0 deletions meta/regexp/regexp_named_group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

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

singleton_mutations
regexp_mutations
end

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

singleton_mutations
regexp_mutations

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

0 comments on commit 037673a

Please sign in to comment.