Skip to content

Commit

Permalink
[Fix rubocop#8083] Fix an error for Lint/MixedRegexpCaptureTypes
Browse files Browse the repository at this point in the history
Fixes rubocop#8083.

This PR fixes an error for `Lint/MixedRegexpCaptureTypes` cop
when using a regular expression that cannot be processed by regexp_parser gem.
https://github.com/ammar/regexp_parser

Probably false negatives will be made, however false negatives will be
reduced when a processing pattern of regexp_parser increases.

So this PR relies on regexp_parser for error resolution.
  • Loading branch information
koic committed Jun 3, 2020
1 parent af67c19 commit 373423e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,10 @@

## 0.85.0 (2020-06-01)

### Bug fixes

* [#8083](https://github.com/rubocop-hq/rubocop/issues/8083): Fix an error for `Lint/MixedRegexpCaptureTypes` cop when using a regular expression that cannot be processed by regexp_parser gem. ([@koic][])

### New features

* [#6289](https://github.com/rubocop-hq/rubocop/issues/6289): Add new `CheckDefinitionPathHierarchy` option for `Naming/FileName`. ([@jschneid][])
Expand Down
9 changes: 8 additions & 1 deletion lib/rubocop/cop/lint/mixed_regexp_capture_types.rb
Expand Up @@ -27,7 +27,14 @@ class MixedRegexpCaptureTypes < Cop
def on_regexp(node)
return if contain_non_literal?(node)

tree = Regexp::Parser.parse(node.content)
begin
tree = Regexp::Parser.parse(node.content)
# Returns if a regular expression that cannot be processed by regexp_parser gem.
# https://github.com/rubocop-hq/rubocop/issues/8083
rescue Regexp::Scanner::ScannerError
return
end

return unless named_capture?(tree)
return unless numbered_capture?(tree)

Expand Down
7 changes: 7 additions & 0 deletions spec/rubocop/cop/lint/mixed_regexp_capture_types_spec.rb
Expand Up @@ -31,6 +31,13 @@
RUBY
end

# See https://github.com/rubocop-hq/rubocop/issues/8083
it 'does not register offense when using a Regexp cannot be processed by regexp_parser gem' do
expect_no_offenses(<<~'RUBY')
/data = ({"words":.+}}}[^}]*})/m
RUBY
end

# RuboCop does not know a value of variables that it will contain in the regexp literal.
# For example, `/(?<foo>#{var}*)` is interpreted as `/(?<foo>*)`.
# So it does not offense when variables are used in regexp literals.
Expand Down

0 comments on commit 373423e

Please sign in to comment.