Skip to content

Commit

Permalink
[Fix rubocop#8985] Fix Style/StringConcatenation autocorrect genera…
Browse files Browse the repository at this point in the history
…ting invalid ruby

When single quoted strings

Closes rubocop#8985
  • Loading branch information
tejasbubane committed Dec 2, 2020
1 parent e9e4c55 commit ccb7102
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_fix_stylestringconcatenation_autocorrect.md
@@ -0,0 +1 @@
* [#8985](https://github.com/rubocop-hq/rubocop/issues/8985): Fix `Style/StringConcatenation` autocorrect generating invalid ruby. ([@tejasbubane][])
8 changes: 7 additions & 1 deletion lib/rubocop/cop/style/string_concatenation.rb
Expand Up @@ -106,7 +106,13 @@ def replacement(parts)
end
end

"\"#{interpolated_parts.join}\""
"\"#{handle_quotes(interpolated_parts).join}\""
end

def handle_quotes(parts)
parts.map do |part|
part == '"' ? '\"' : part
end
end

def single_quoted?(str_node)
Expand Down
22 changes: 22 additions & 0 deletions spec/rubocop/cop/style/string_concatenation_spec.rb
Expand Up @@ -158,4 +158,26 @@
RUBY
end
end

context 'empty quotes' do
it 'registers offense and corrects' do
expect_offense(<<-RUBY)
'"' + "foo" + '"'
^^^^^^^^^^^^^^^^^ Prefer string interpolation to string concatenation.
'"' + "foo" + "'"
^^^^^^^^^^^^^^^^^ Prefer string interpolation to string concatenation.
"'" + "foo" + '"'
^^^^^^^^^^^^^^^^^ Prefer string interpolation to string concatenation.
"'" + "foo" + '"' + "bar"
^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer string interpolation to string concatenation.
RUBY

expect_correction(<<-RUBY)
"\\\"foo\\\""
"\\\"foo'"
"'foo\\\""
"'foo\\\"bar"
RUBY
end
end
end

0 comments on commit ccb7102

Please sign in to comment.