Skip to content

Commit

Permalink
[Fix rubocop#13099] Make Style/RedundantRegexpArgument respect the …
Browse files Browse the repository at this point in the history
…`EnforcedStyle` of `Style/StringLiterals`

Fixes rubocop#13099.

This PR makes `Style/RedundantRegexpArgument` respect the `EnforcedStyle` of `Style/StringLiterals`.
Also, combine the way to get the `EnforcedStyle` from `Style/StringLiterals` into `StringLiteralHelp`.
  • Loading branch information
koic authored and bbatsov committed Aug 23, 2024
1 parent 105cabe commit 04fddb0
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#13099](https://github.com/rubocop/rubocop/issues/13099): Make `Style/RedundantRegexpArgument` respect the `EnforcedStyle` of `Style/StringLiterals`. ([@koic][])
12 changes: 12 additions & 0 deletions lib/rubocop/cop/mixin/string_literals_help.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ def wrong_quotes?(src_or_node)
!/" | \\[^'\\] | \#[@{$]/x.match?(src)
end
end

def preferred_string_literal
enforce_double_quotes? ? '""' : "''"
end

def enforce_double_quotes?
string_literals_config['EnforcedStyle'] == 'double_quotes'
end

def string_literals_config
config.for_cop('Style/StringLiterals')
end
end
end
end
15 changes: 1 addition & 14 deletions lib/rubocop/cop/style/empty_heredoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module Style
class EmptyHeredoc < Base
include Heredoc
include RangeHelp
include StringLiteralsHelp
extend AutoCorrector

MSG = 'Use an empty string literal instead of heredoc.'
Expand All @@ -53,20 +54,6 @@ def on_heredoc(node)
corrector.remove(range_by_whole_lines(heredoc_end, include_final_newline: true))
end
end

private

def preferred_string_literal
enforce_double_quotes? ? '""' : "''"
end

def enforce_double_quotes?
string_literals_config['EnforcedStyle'] == 'double_quotes'
end

def string_literals_config
config.for_cop('Style/StringLiterals')
end
end
end
end
Expand Down
13 changes: 1 addition & 12 deletions lib/rubocop/cop/style/empty_literal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module Style
class EmptyLiteral < Base
include FrozenStringLiteral
include RangeHelp
include StringLiteralsHelp
extend AutoCorrector

ARR_MSG = 'Use array literal `[]` instead of `Array.new`.'
Expand Down Expand Up @@ -67,18 +68,6 @@ def offense_message(node)
end
end

def preferred_string_literal
enforce_double_quotes? ? '""' : "''"
end

def enforce_double_quotes?
string_literals_config['EnforcedStyle'] == 'double_quotes'
end

def string_literals_config
config.for_cop('Style/StringLiterals')
end

def first_argument_unparenthesized?(node)
parent = node.parent
return false unless parent && %i[send super zsuper].include?(parent.type)
Expand Down
2 changes: 0 additions & 2 deletions lib/rubocop/cop/style/quoted_symbols.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ def correct_quotes(str)

def style
return super unless super == :same_as_string_literals

string_literals_config = config.for_cop('Style/StringLiterals')
return :single_quotes unless string_literals_config['Enabled']

string_literals_config['EnforcedStyle'].to_sym
Expand Down
5 changes: 4 additions & 1 deletion lib/rubocop/cop/style/redundant_regexp_argument.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module Style
# 'foo'.sub('f', 'x')
# 'foo'.sub!('f', 'x')
class RedundantRegexpArgument < Base
include StringLiteralsHelp
extend AutoCorrector

MSG = 'Use string `%<prefer>s` as argument instead of regexp `%<current>s`.'
Expand Down Expand Up @@ -71,8 +72,10 @@ def preferred_argument(regexp_node)
if new_argument.include?('"')
new_argument.gsub!("'", "\\\\'")
quote = "'"
else
elsif new_argument.include?('\\')
quote = '"'
else
quote = enforce_double_quotes? ? '"' : "'"
end

"#{quote}#{new_argument}#{quote}"
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/html_formatter/expected.html
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ <h1 class="title">RuboCop Inspection Report</h1>
<div class="meta">
<span class="location">Line #4</span>
<span class="severity convention">convention:</span>
<span class="message">Style/RedundantRegexpArgument: Use string <code>&quot;&amp;amp;&amp;lt;&quot;</code> as argument instead of regexp <code>/&amp;amp;&amp;lt;/</code>.</span>
<span class="message">Style/RedundantRegexpArgument: Use string <code>&#39;&amp;amp;&amp;lt;&#39;</code> as argument instead of regexp <code>/&amp;amp;&amp;lt;/</code>.</span>
</div>

<pre><code> qux(quux.scan(<span class="highlight convention">/&amp;amp;&amp;lt;/</span>))</code></pre>
Expand Down
57 changes: 49 additions & 8 deletions spec/rubocop/cop/style/redundant_regexp_argument_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
it "registers an offense and corrects when the method is `#{method}`" do
expect_offense(<<~RUBY, method: method)
'foo'.#{method}(/f/)
_{method} ^^^ Use string `"f"` as argument instead of regexp `/f/`.
_{method} ^^^ Use string `'f'` as argument instead of regexp `/f/`.
RUBY

expect_correction(<<~RUBY)
'foo'.#{method}("f")
'foo'.#{method}('f')
RUBY
end

it "registers an offense and corrects when the method with safe navigation operator is `#{method}`" do
expect_offense(<<~RUBY, method: method)
'foo'&.#{method}(/f/)
_{method} ^^^ Use string `"f"` as argument instead of regexp `/f/`.
_{method} ^^^ Use string `'f'` as argument instead of regexp `/f/`.
RUBY

expect_correction(<<~RUBY)
'foo'&.#{method}("f")
'foo'&.#{method}('f')
RUBY
end
end
Expand Down Expand Up @@ -61,11 +61,11 @@
it 'registers an offense and corrects when using escaping characters' do
expect_offense(<<~'RUBY')
'a,b,c'.split(/\./)
^^^^ Use string `"."` as argument instead of regexp `/\./`.
^^^^ Use string `'.'` as argument instead of regexp `/\./`.
RUBY

expect_correction(<<~RUBY)
'a,b,c'.split(".")
'a,b,c'.split('.')
RUBY
end

Expand Down Expand Up @@ -127,11 +127,11 @@
it 'registers an offense and corrects when using two or more spaces regexp' do
expect_offense(<<~RUBY)
'foo bar'.split(/ /)
^^^^ Use string `" "` as argument instead of regexp `/ /`.
^^^^ Use string `' '` as argument instead of regexp `/ /`.
RUBY

expect_correction(<<~RUBY)
'foo bar'.split(" ")
'foo bar'.split(' ')
RUBY
end

Expand Down Expand Up @@ -168,4 +168,45 @@
'foo bar'.split(/ /)
RUBY
end

context 'when `Style/StringLiterals` is configured with `single_quotes`' do
let(:other_cops) { { 'Style/StringLiterals' => { 'EnforcedStyle' => 'single_quotes' } } }

it 'registers an offense and corrects to single quoted string when using non-backquoted regexp' do
expect_offense(<<~RUBY)
'foo'.split(/f/)
^^^ Use string `'f'` as argument instead of regexp `/f/`.
RUBY

expect_correction(<<~RUBY)
'foo'.split('f')
RUBY
end

it 'registers an offense and corrects to double quoted string when using backquoted regexp' do
expect_offense(<<~'RUBY')
'foo'.split(/\n/)
^^^^ Use string `"\n"` as argument instead of regexp `/\n/`.
RUBY

expect_correction(<<~'RUBY')
'foo'.split("\n")
RUBY
end
end

context 'when `Style/StringLiterals` is configured with `double_quotes`' do
let(:other_cops) { { 'Style/StringLiterals' => { 'EnforcedStyle' => 'double_quotes' } } }

it 'registers an offense and corrects to double quoted string when using non-backquoted regexp' do
expect_offense(<<~RUBY)
'foo'.split(/f/)
^^^ Use string `"f"` as argument instead of regexp `/f/`.
RUBY

expect_correction(<<~RUBY)
'foo'.split("f")
RUBY
end
end
end

0 comments on commit 04fddb0

Please sign in to comment.