diff --git a/CHANGELOG.md b/CHANGELOG.md index c4ae30bf1d7..aa7e04ae831 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ * [#6935](https://github.com/rubocop-hq/rubocop/issues/6935): `Layout/AccessModifierIndentation` should ignore access modifiers that apply to specific methods. ([@deivid-rodriguez][]) * [#6956](https://github.com/rubocop-hq/rubocop/issues/6956): Prevent auto-correct confliction of `Lint/Lambda` and `Lint/UnusedBlockArgument`. ([@koic][]) * [#6915](https://github.com/rubocop-hq/rubocop/issues/6915): Fix false positive in `Style/SafeNavigation` when a modifier if is safe guarding a method call being passed to `break`, `fail`, `next`, `raise`, `return`, `throw`, and `yield`. ([@rrosenblum][]) +* [#6822](https://github.com/rubocop-hq/rubocop/issues/6822): Fix Lint/LiteralInInterpolation autocorrection for single quotes. ([@hoshinotsuyoshi][]) ### Changes diff --git a/lib/rubocop/cop/lint/literal_in_interpolation.rb b/lib/rubocop/cop/lint/literal_in_interpolation.rb index e4b2f412e74..f31ad4039c5 100644 --- a/lib/rubocop/cop/lint/literal_in_interpolation.rb +++ b/lib/rubocop/cop/lint/literal_in_interpolation.rb @@ -55,7 +55,7 @@ def autocorrected_value(node) when :float node.children.last.to_f.to_s when :str - node.children.last + autocorrected_value_for_string(node) when :sym autocorrected_value_for_symbol(node) else @@ -63,6 +63,14 @@ def autocorrected_value(node) end end + def autocorrected_value_for_string(node) + if node.source.start_with?("'", '%q') + node.children.last.inspect[1..-2] + else + node.children.last + end + end + def autocorrected_value_for_symbol(node) end_pos = node.loc.end ? node.loc.end.begin_pos : node.loc.expression.end_pos diff --git a/spec/rubocop/cop/lint/literal_in_interpolation_spec.rb b/spec/rubocop/cop/lint/literal_in_interpolation_spec.rb index 9549bfd2231..caafde0cf70 100644 --- a/spec/rubocop/cop/lint/literal_in_interpolation_spec.rb +++ b/spec/rubocop/cop/lint/literal_in_interpolation_spec.rb @@ -145,4 +145,42 @@ it_behaves_like('non-special string literal interpolation', %('foo')) it_behaves_like('non-special string literal interpolation', %("foo")) + + it 'handles double quotes in single quotes when auto-correction' do + corrected = autocorrect_source(<<-'RUBY'.strip_indent) + "this is #{'"'} silly" + RUBY + expect(corrected).to eq(<<-'RUBY'.strip_indent) + "this is \" silly" + RUBY + end + + it 'handles backslach in single quotes when auto-correction' do + corrected = autocorrect_source(<<-'RUBY'.strip_indent) + x = "ABC".gsub(/(A)(B)(C)/, "D#{'\2'}F") + "this is #{'\n'} silly" + "this is #{%q(\n)} silly" + RUBY + expect(corrected).to eq(<<-'RUBY'.strip_indent) + x = "ABC".gsub(/(A)(B)(C)/, "D\\2F") + "this is \\n silly" + "this is \\n silly" + RUBY + end + + it 'handles backslach in double quotes when auto-correction' do + corrected = autocorrect_source(<<-'RUBY'.strip_indent) + "this is #{"\n"} silly" + "this is #{%(\n)} silly" + "this is #{%Q(\n)} silly" + RUBY + expect(corrected).to eq(<<-'RUBY'.strip_indent) + "this is + silly" + "this is + silly" + "this is + silly" + RUBY + end end