Skip to content

Commit

Permalink
[Fix rubocop#12985] Fix an error for `Style/RedundantRegexpCharacterC…
Browse files Browse the repository at this point in the history
…lass`

Fixes rubocop#12985.

This PR fixes an error for `Style/RedundantRegexpCharacterClass`
when using regexp_parser gem 2.3.1 or older.

This issue has already been resolved in regexp_parser gem 2.4.

In the past, there was an issue with compatibility with regexp_parser 1.x due to the following:
rubocop#9102 (comment)

Currently, many dependent gems including Capybara have permitted upgrading to regexp_parser 2.x:
https://rubygems.org/gems/regexp_parser/reverse_dependencies

Therefore, it would be a good time to specify `spec.runtime_dependency` as regexp_parser 2.4+.
  • Loading branch information
koic committed Jun 13, 2024
1 parent 967fe88 commit 511f3dc
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12985](https://github.com/rubocop/rubocop/issues/12985): Fix an error for `Style/RedundantRegexpCharacterClass` when using regexp_parser gem 2.3.1 or older. ([@koic][])
25 changes: 4 additions & 21 deletions lib/rubocop/ext/regexp_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,9 @@ module Expression
module Base
attr_accessor :origin

if Gem::Version.new(Regexp::Parser::VERSION) >= Gem::Version.new('2.0')
# Shortcut to `loc.expression`
def expression
@expression ||= origin.adjust(begin_pos: ts, end_pos: ts + full_length)
end
# Please remove this `else` branch when support for regexp_parser 1.8 will be dropped.
# It's for compatibility with regexp_parser 1.8 and will never be maintained.
else
attr_accessor :source

def start_index
# ts is a byte index; convert it to a character index
@start_index ||= source.byteslice(0, ts).length
end

# Shortcut to `loc.expression`
def expression
end_pos = start_index + full_length
@expression ||= origin.adjust(begin_pos: start_index, end_pos: end_pos)
end
# Shortcut to `loc.expression`
def expression
@expression ||= origin.adjust(begin_pos: ts, end_pos: ts + full_length)
end

# @returns a location map like `parser` does, with:
Expand Down Expand Up @@ -69,8 +52,8 @@ def build_location

body = expression.adjust(end_pos: -q.text.length)
q.origin = origin
q.source = source if q.respond_to?(:source=) # for regexp_parser 1.8
q_loc = q.expression

{ body: body, quantifier: q_loc }
end
end
Expand Down
2 changes: 1 addition & 1 deletion rubocop.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency('parallel', '~> 1.10')
s.add_runtime_dependency('parser', '>= 3.3.0.2')
s.add_runtime_dependency('rainbow', '>= 2.2.2', '< 4.0')
s.add_runtime_dependency('regexp_parser', '>= 1.8', '< 3.0')
s.add_runtime_dependency('regexp_parser', '>= 2.4', '< 3.0')
s.add_runtime_dependency('rexml', '>= 3.2.5', '< 4.0')
s.add_runtime_dependency('rubocop-ast', '>= 1.31.1', '< 2.0')
s.add_runtime_dependency('ruby-progressbar', '~> 1.7')
Expand Down

0 comments on commit 511f3dc

Please sign in to comment.