Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect autocorrect for Style/RedundantRegexpArgument when the regex contains a single quote. #13616

Merged
merged 1 commit into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#13616](https://github.com/rubocop/rubocop/pull/13616): Fix incorrect autocorrect for `Style/RedundantRegexpArgument` when the regex contains a single quote. ([@mrzasa][])
3 changes: 3 additions & 0 deletions lib/rubocop/cop/style/redundant_regexp_argument.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ def preferred_argument(regexp_node)
new_argument.gsub!("'", "\\\\'")
new_argument.gsub!('\"', '"')
quote = "'"
elsif new_argument.include?('\'')
new_argument.gsub!("'", "\\\\'")
quote = "'"
elsif new_argument.include?('\\')
quote = '"'
else
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/style/redundant_regexp_argument_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@
RUBY
end

it 'registers an offense and corrects when using single quote character' do
expect_offense(<<~'RUBY')
str.gsub(/'/)
^^^ Use string `'\''` as argument instead of regexp `/'/`.
RUBY

expect_correction(<<~'RUBY')
str.gsub('\'')
RUBY
end

it 'registers an offense and corrects when using escaped double quote character' do
expect_offense(<<~'RUBY')
str.gsub(/\"/)
Expand Down
Loading