Skip to content

Commit

Permalink
Skip group options regexp_parser can't parse
Browse files Browse the repository at this point in the history
  • Loading branch information
dgollahon committed Dec 20, 2020
1 parent caf6b46 commit 147e608
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/mutant/ast/regexp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ def self.parse(regexp)
::Regexp::Parser.parse(regexp)
# `regexp_parser` is more strict than MRI
# See: https://github.com/ammar/regexp_parser/issues/75
rescue ::Regexp::Scanner::PrematureEndError
# Also: https://github.com/ammar/regexp_parser/issues/76
rescue ::Regexp::Scanner::PrematureEndError,
::Regexp::Scanner::InvalidGroupOption
end
# rubocop:enable Lint/SuppressedException

Expand Down
13 changes: 12 additions & 1 deletion meta/regexp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@
mutation '/(?(1)(foo)(?:bar))/'
end

# Case where MRI would accept an expression but regexp_parser not.
# MRI accepts this regex but `regexp_parser` does not.
# See: https://github.com/ammar/regexp_parser/issues/75
Mutant::Meta::Example.add :regexp do
source '/\xA/'

Expand All @@ -84,6 +85,16 @@
mutation '/nomatch\A/'
end

# MRI accepts this regex but `regexp_parser` does not.
# See: https://github.com/ammar/regexp_parser/issues/76
Mutant::Meta::Example.add :regexp do
source '/(?<æ>.)/'

singleton_mutations
mutation '//'
mutation '/nomatch\A/'
end

Pathname
.glob(Pathname.new(__dir__).join('regexp', '*.rb'))
.sort
Expand Down
8 changes: 7 additions & 1 deletion spec/unit/mutant/ast/regexp/parse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ def apply(input)
end
end

context 'on regexp regexp_parser does not accept' do
context 'when given a hex escape `regexp_parser` does not support' do
it 'returns nil' do
expect(apply(/\xA/)).to be(nil)
end
end

context 'when given a capture group `regexp_parser` does not support' do
it 'returns nil' do
expect(apply(/(?<æ>.)/)).to be(nil)
end
end
end

0 comments on commit 147e608

Please sign in to comment.