Skip to content

Commit

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

  ## 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 committed Apr 27, 2019
1 parent 95d2110 commit 5849678
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
10 changes: 9 additions & 1 deletion lib/rubocop/cop/lint/literal_in_interpolation.rb
Expand Up @@ -55,14 +55,22 @@ 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
node.source.gsub('"', '\"')
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
Expand Up @@ -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

0 comments on commit 5849678

Please sign in to comment.