Skip to content

Commit

Permalink
Merge pull request #9880 from koic/fix_a_false_positive_for_style_reg…
Browse files Browse the repository at this point in the history
…exp_literal

Fix a false positive for `Style/RegexpLiteral`
  • Loading branch information
koic committed Jun 17, 2021
2 parents eba9ab8 + 0400bba commit f2edbfa
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/fix_a_false_positive_for_style_regexp_literal.md
@@ -0,0 +1 @@
* [#9880](https://github.com/rubocop/rubocop/pull/9880): Fix a false positive for `Style/RegexpLiteral` when using a regexp starts with a blank as a method argument. ([@koic][])
5 changes: 3 additions & 2 deletions lib/rubocop/cop/style/regexp_literal.rb
Expand Up @@ -117,7 +117,7 @@ def allowed_mixed_slash?(node)
def allowed_percent_r_literal?(node)
style == :slashes && contains_disallowed_slash?(node) ||
style == :percent_r ||
allowed_mixed_percent_r?(node) || omit_parentheses_style?(node)
allowed_mixed_percent_r?(node) || allowed_omit_parentheses_with_percent_r_literal?(node)
end

def allowed_mixed_percent_r?(node)
Expand Down Expand Up @@ -149,8 +149,9 @@ def preferred_delimiters
config.for_cop('Style/PercentLiteralDelimiters') ['PreferredDelimiters']['%r'].chars
end

def omit_parentheses_style?(node)
def allowed_omit_parentheses_with_percent_r_literal?(node)
return false unless node.parent&.call_type?
return true if node.content.start_with?(' ')

enforced_style = config.for_cop('Style/MethodCallWithArgsParentheses')['EnforcedStyle']

Expand Down
38 changes: 38 additions & 0 deletions spec/rubocop/cop/style/regexp_literal_spec.rb
Expand Up @@ -518,6 +518,25 @@
^^^^^^^^^^ Use `//` around regular expression.
RUBY
end

it 'does not register an offense when using a regexp starts with a blank as a method argument' do
expect_no_offenses(<<~RUBY)
do_something %r/ regexp/
RUBY
end

it 'does not register an offense when using a regexp starts with a blank as a safe navigation method argument' do
expect_no_offenses(<<~RUBY)
foo&.do_something %r/ regexp/
RUBY
end

it 'registers an offense when using a regexp starts with a blank' do
expect_offense(<<~RUBY)
%r/ regexp/
^^^^^^^^^^^ Use `//` around regular expression.
RUBY
end
end

context 'when using `%r` regexp with `EnforcedStyle: mixed`' do
Expand All @@ -543,6 +562,25 @@
^^^^^^^^^^ Use `//` around regular expression.
RUBY
end

it 'does not register an offense when using a regexp starts with a blank as a method argument' do
expect_no_offenses(<<~RUBY)
do_something %r/ regexp/
RUBY
end

it 'does not register an offense when using a regexp starts with a blank as a safe navigation method argument' do
expect_no_offenses(<<~RUBY)
foo&.do_something %r/ regexp/
RUBY
end

it 'registers an offense when using a regexp starts with a blank' do
expect_offense(<<~RUBY)
%r/ regexp/
^^^^^^^^^^^ Use `//` around regular expression.
RUBY
end
end
end

Expand Down

0 comments on commit f2edbfa

Please sign in to comment.