Skip to content

Commit

Permalink
[Fix #6822] Fix Lint/LiteralInInterpolation autocorrection for single…
Browse files Browse the repository at this point in the history
… quotes (#6979)

## Current behavior:

  Doing `$ rubocop --only Lint/LiteralInInterpolation`;

  source:

  ```ruby
  x = "ABC".gsub(/(A)(B)(C)/, "D#{'\2'}F")
  "foo#{'\n'}bar"
  "this is #{'"'} silly"
  ```

  corrected to:

  ```ruby
  x = "ABC".gsub(/(A)(B)(C)/, "D\2F")
  "foo\nbar"
  "this is " silly"
  ```

  ## New behavior:

  corrected to:

  ```ruby
  x = "ABC".gsub(/(A)(B)(C)/, "D\\2F")
  "foo\\nbar"
  "this is \" silly"
  ```
  • Loading branch information
hoshinotsuyoshi authored and bbatsov committed Apr 29, 2019
1 parent 6b68976 commit 557ad20
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,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][])
* [#6985](https://github.com/rubocop-hq/rubocop/issues/6985): Fix an incorrect auto-correct for `Lint/LiteralInInterpolation` if contains array percent literal. ([@yakout][])

### Changes
Expand Down
10 changes: 9 additions & 1 deletion lib/rubocop/cop/lint/literal_in_interpolation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,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)
when :array
Expand All @@ -66,6 +66,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
Expand Down
38 changes: 38 additions & 0 deletions spec/rubocop/cop/lint/literal_in_interpolation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,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

0 comments on commit 557ad20

Please sign in to comment.