Skip to content

Commit

Permalink
[WIP] Attempt regexp_parser 2.x upgrade
Browse files Browse the repository at this point in the history
- 2.x fails because of this change: ammar/regexp_parser@f14a36b#diff-5bfe3942df5a59a97d7e5c7d3e918a1c704f964685e719c178af22401da46c23R25. Calling `#to_s` is no longer possible on a frozen instance of `Regexp::Expression::Group::Passive`.
- I have left a monkeypatch in as a demo of one way this problem could get resolved.
  • Loading branch information
dgollahon committed Dec 23, 2020
1 parent 460dcdc commit ef82da9
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 29 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ PATH
mprelude (~> 0.1.0)
parser (~> 2.7.1)
procto (~> 0.0.2)
regexp_parser (~> 1.8)
regexp_parser (~> 2.0, >= 2.0.1)
unparser (~> 0.5.4)
variable (~> 0.0.1)

Expand Down Expand Up @@ -52,7 +52,7 @@ GEM
ast (~> 2.4.1)
procto (0.0.3)
rainbow (3.0.0)
regexp_parser (1.8.2)
regexp_parser (2.0.1)
rexml (3.2.4)
rspec (3.10.0)
rspec-core (~> 3.10.0)
Expand Down
19 changes: 11 additions & 8 deletions lib/mutant/ast/regexp.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# frozen_string_literal: true

class Regexp::Expression::Group::Passive
def initialize(*)
@implicit = false
super
end

def implicit?
@implicit
end
end

module Mutant
module AST
# Regexp source mapper
Expand All @@ -9,17 +20,9 @@ module Regexp
# @param regexp [String]
#
# @return [Regexp::Expression, nil]
#
# rubocop:disable Lint/SuppressedException
def self.parse(regexp)
::Regexp::Parser.parse(regexp)
# `regexp_parser` is more strict than MRI
# See: https://github.com/ammar/regexp_parser/issues/75
# Also: https://github.com/ammar/regexp_parser/issues/76
rescue ::Regexp::Scanner::PrematureEndError,
::Regexp::Scanner::InvalidGroupOption
end
# rubocop:enable Lint/SuppressedException

# Convert expression into ast node
#
Expand Down
4 changes: 2 additions & 2 deletions lib/mutant/mutator/node/literal/regex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def dispatch
# body is composed of only strings. Regular expressions
# with interpolation are skipped
def mutate_body
return unless body.all?(&method(:n_str?)) && body_ast
return unless body.all?(&method(:n_str?))

Mutator.mutate(body_ast).each do |mutation|
source = AST::Regexp.to_expression(mutation).to_s
Expand All @@ -41,7 +41,7 @@ def mutate_body
end

def body_ast
body_expression and AST::Regexp.to_ast(body_expression)
AST::Regexp.to_ast(body_expression)
end

def body_expression
Expand Down
2 changes: 1 addition & 1 deletion mutant.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Gem::Specification.new do |gem|
gem.add_runtime_dependency('mprelude', '~> 0.1.0')
gem.add_runtime_dependency('parser', '~> 2.7.1')
gem.add_runtime_dependency('procto', '~> 0.0.2')
gem.add_runtime_dependency('regexp_parser', '~> 1.8')
gem.add_runtime_dependency('regexp_parser', '~> 2.0', '>= 2.0.1')
gem.add_runtime_dependency('unparser', '~> 0.5.4')
gem.add_runtime_dependency('variable', '~> 0.0.1')

Expand Down
12 changes: 0 additions & 12 deletions spec/unit/mutant/ast/regexp/parse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,4 @@ def apply(input)
expect(apply(/foo/).to_re).to eql(/foo/)
end
end

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
4 changes: 2 additions & 2 deletions test_app/Gemfile.minitest.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ PATH
mprelude (~> 0.1.0)
parser (~> 2.7.1)
procto (~> 0.0.2)
regexp_parser (~> 1.8)
regexp_parser (~> 2.0, >= 2.0.1)
unparser (~> 0.5.4)
variable (~> 0.0.1)
mutant-minitest (0.10.21)
Expand Down Expand Up @@ -54,7 +54,7 @@ GEM
parser (2.7.2.0)
ast (~> 2.4.1)
procto (0.0.3)
regexp_parser (1.8.2)
regexp_parser (2.0.1)
thread_safe (0.3.6)
unparser (0.5.4)
abstract_type (~> 0.0.7)
Expand Down
4 changes: 2 additions & 2 deletions test_app/Gemfile.rspec3.8.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ PATH
mprelude (~> 0.1.0)
parser (~> 2.7.1)
procto (~> 0.0.2)
regexp_parser (~> 1.8)
regexp_parser (~> 2.0, >= 2.0.1)
unparser (~> 0.5.4)
variable (~> 0.0.1)
mutant-rspec (0.10.21)
Expand Down Expand Up @@ -53,7 +53,7 @@ GEM
parser (2.7.2.0)
ast (~> 2.4.1)
procto (0.0.3)
regexp_parser (1.8.2)
regexp_parser (2.0.1)
rspec (3.8.0)
rspec-core (~> 3.8.0)
rspec-expectations (~> 3.8.0)
Expand Down

0 comments on commit ef82da9

Please sign in to comment.