Skip to content

Commit

Permalink
[Fix #12542] Fix false positive for Lint/MixedRegexpCaptureTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
marocchino authored and bbatsov committed Jan 12, 2024
1 parent b9d793a commit 075d4d5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12542](https://github.com/rubocop/rubocop/issues/12542): Fix false positive for `Lint/MixedRegexpCaptureTypes` when using look-ahead matcher. ([@marocchino][])
13 changes: 9 additions & 4 deletions lib/rubocop/ext/regexp_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,22 @@ def each_capture(named: ANY)
return enum_for(__method__, named: named) unless block_given?

parsed_tree&.traverse do |event, exp, _index|
yield(exp) if event == :enter &&
named == exp.respond_to?(:name) &&
exp.respond_to?(:capturing?) &&
exp.capturing?
yield(exp) if named_capturing?(exp, event, named)
end

self
end

private

def named_capturing?(exp, event, named)
event == :enter &&
named == exp.respond_to?(:name) &&
!exp.text.start_with?('(?<=') &&
exp.respond_to?(:capturing?) &&
exp.capturing?
end

def with_interpolations_blanked
# Ignore the trailing regopt node
children[0...-1].map do |child|
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/lint/mixed_regexp_capture_types_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
RUBY
end

it 'does not register offense to a regexp with look behind' do
expect_no_offenses(<<~RUBY)
/(?<=>)(<br>)(?=><)/
RUBY
end

it 'does not register offense to a regexp with numbered capture only' do
expect_no_offenses(<<~RUBY)
/(foo)(bar)/
Expand Down

0 comments on commit 075d4d5

Please sign in to comment.