diff --git a/Changelog.md b/Changelog.md index 8806779d5..48c8688e0 100644 --- a/Changelog.md +++ b/Changelog.md @@ -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: `/(?bar)/` -> `/(?:bar)`. + * [#1198](https://github.com/mbj/mutant/pull/1198) * Fix configured match expression loading to properly display error diff --git a/lib/mutant.rb b/lib/mutant.rb index 525b3ec48..6d7feed5b 100644 --- a/lib/mutant.rb +++ b/lib/mutant.rb @@ -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' diff --git a/lib/mutant/mutator/node/regexp/named_group.rb b/lib/mutant/mutator/node/regexp/named_group.rb new file mode 100644 index 000000000..a06044683 --- /dev/null +++ b/lib/mutant/mutator/node/regexp/named_group.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +module Mutant + class Mutator + class Node + module Regexp + # Mutator for regexp named capture groups, such as `/(?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 diff --git a/meta/regexp/regexp_named_group.rb b/meta/regexp/regexp_named_group.rb new file mode 100644 index 000000000..1abe1bbb4 --- /dev/null +++ b/meta/regexp/regexp_named_group.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +Mutant::Meta::Example.add :regexp_named_group do + source '/(?)/' + + singleton_mutations + regexp_mutations +end + +Mutant::Meta::Example.add :regexp_named_group do + source '/(?\w)/' + + singleton_mutations + regexp_mutations + + mutation '/(?:\w)/' + mutation '/(?\W)/' +end