Skip to content

Commit

Permalink
[Fix rubocop#12542] Fix false positive for `Lint/MixedRegexpCaptureTy…
Browse files Browse the repository at this point in the history
…pes`
  • Loading branch information
marocchino committed Dec 21, 2023
1 parent 7de40e0 commit 6cc71c4
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 behind ([@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 exp_conditions(exp, event, named)
end

self
end

private

def exp_conditions(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 6cc71c4

Please sign in to comment.