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 an incorrect auto-correct for StringConcatenation when concat string includes double quote and interpolation. #9346

Merged
merged 1 commit into from Jan 11, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1 @@
* [#9346](https://github.com/rubocop-hq/rubocop/pull/9346): Fix an incorrect auto-correct for `Style/StringConcatenation` when concat string include double quotes and interpolation. ([@k-karen][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/string_concatenation.rb
Expand Up @@ -116,7 +116,7 @@ def replacement(parts)
parts.map do |part|
if part.str_type?
if single_quoted?(part)
part.value.gsub('\\') { '\\\\' }
part.value.gsub(/(\\|")/, '\\\\\&')
else
part.value.inspect[1..-2]
end
Expand Down
13 changes: 13 additions & 0 deletions spec/rubocop/cop/style/string_concatenation_spec.rb
Expand Up @@ -190,4 +190,17 @@
RUBY
end
end

context 'double quotes inside string surrounded single quotes' do
it 'registers an offense and corrects with double quotes' do
expect_offense(<<-RUBY)
'"bar"' + foo
^^^^^^^^^^^^^ Prefer string interpolation to string concatenation.
RUBY

expect_correction(<<-RUBY)
"\\\"bar\\\"\#{foo}"
RUBY
end
end
end