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 named capture to non-capturing group mutation #1194

Merged
merged 1 commit into from
Jan 3, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# v0.10.25 2021-01-02-03

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

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

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

* Fix configured match expression loading to properly display error
Expand Down
1 change: 1 addition & 0 deletions lib/mutant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ module Mutant
require 'mutant/mutator/node/regexp/alternation_meta'
require 'mutant/mutator/node/regexp/beginning_of_line_anchor'
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
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