Skip to content

Commit

Permalink
Fix #8830 bad autocorrect of Style/StringConcatenation when string …
Browse files Browse the repository at this point in the history
…includes double quotes.
  • Loading branch information
tleish committed Oct 2, 2020
1 parent f3af4e2 commit 749a495
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -14,6 +14,7 @@
* [#8514](https://github.com/rubocop-hq/rubocop/issues/8514): Correct multiple `Style/MethodDefParentheses` per file. ([@rdunlop][])
* [#8825](https://github.com/rubocop-hq/rubocop/issues/8825): Fix crash in `Style/ExplicitBlockArgument` when code is called outside of a method. ([@ghiculescu][])
* [#8354](https://github.com/rubocop-hq/rubocop/issues/8354): Detect regexp named captures in `Style/CaseLikeIf` cop. ([@dsavochkin][])
* [#8830](https://github.com/rubocop-hq/rubocop/issues/8830): Fix bad autocorrect of `Style/StringConcatenation` when string includes double quotes. ([@tleish][])

### Changes

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/string_concatenation.rb
Expand Up @@ -87,7 +87,7 @@ def replacement(parts)
if single_quoted?(part)
part.value.gsub('\\') { '\\\\' }
else
escape_string(part.value)
part.value.inspect[1..-2]
end
else
"\#{#{part.source}}"
Expand Down
24 changes: 24 additions & 0 deletions spec/rubocop/cop/style/string_concatenation_spec.rb
Expand Up @@ -122,4 +122,28 @@
expect_no_corrections
end
end

context 'double quotes inside string' do
it 'registers an offense and corrects with double quotes' do
expect_offense(<<-RUBY)
email_with_name = "He said " + "\\\"Arrest that man!\\\"."
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer string interpolation to string concatenation.
RUBY

expect_correction(<<-RUBY)
email_with_name = "He said \\\"Arrest that man!\\\"."
RUBY
end

it 'registers an offense and corrects with percentage quotes' do
expect_offense(<<-RUBY)
email_with_name = %(He said ) + %("Arrest that man!".)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer string interpolation to string concatenation.
RUBY

expect_correction(<<-RUBY)
email_with_name = "He said \\\"Arrest that man!\\\"."
RUBY
end
end
end

0 comments on commit 749a495

Please sign in to comment.