Skip to content

Commit

Permalink
[Fix rubocop#6985] Fix incorrect autocorrect
Browse files Browse the repository at this point in the history
  • Loading branch information
yakout committed Apr 28, 2019
1 parent 326da99 commit 57f30cd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -26,6 +26,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][])
* [#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 Expand Up @@ -3969,3 +3970,4 @@
[@jmanian]: https://github.com/jmanian
[@vfonic]: https://github.com/vfonic
[@andreaseger]: https://github.com/andreaseger
[@yakout]: https://github.com/yakout
9 changes: 9 additions & 0 deletions lib/rubocop/cop/lint/literal_in_interpolation.rb
Expand Up @@ -18,6 +18,7 @@ module Lint
# "result is 10"
class LiteralInInterpolation < Cop
include RangeHelp
include PercentLiteral

MSG = 'Literal interpolation detected.'.freeze
COMPOSITE = %i[array hash pair irange erange].freeze
Expand Down Expand Up @@ -58,6 +59,8 @@ def autocorrected_value(node)
node.children.last
when :sym
autocorrected_value_for_symbol(node)
when :array
autocorrected_value_for_array(node)
else
node.source.gsub('"', '\"')
end
Expand All @@ -70,6 +73,12 @@ def autocorrected_value_for_symbol(node)
range_between(node.loc.begin.end_pos, end_pos).source
end

def autocorrected_value_for_array(node)
return node.source.gsub('"', '\"') unless node.percent_literal?

contents_range(node).source.split(' ').to_s.gsub('"', '\"')
end

# Does node print its own source when converted to a string?
def prints_as_self?(node)
node.basic_literal? ||
Expand Down
7 changes: 7 additions & 0 deletions spec/rubocop/cop/lint/literal_in_interpolation_spec.rb
Expand Up @@ -90,6 +90,13 @@
it_behaves_like('literal interpolation', ':"symbol"', 'symbol')
it_behaves_like('literal interpolation', 1..2)
it_behaves_like('literal interpolation', 1...2)
it_behaves_like('literal interpolation', '%w[]', '[]')
it_behaves_like('literal interpolation', '%w[v1]', '[\"v1\"]')
it_behaves_like('literal interpolation', '%w[v1 v2]', '[\"v1\", \"v2\"]')
it_behaves_like('literal interpolation', '%i[s1 s2]', '[\"s1\", \"s2\"]')
it_behaves_like('literal interpolation', '%I[s1 s2]', '[\"s1\", \"s2\"]')
it_behaves_like('literal interpolation', '%i[s1 s2]', '[\"s1\", \"s2\"]')
it_behaves_like('literal interpolation', '%i[ s1 s2 ]', '[\"s1\", \"s2\"]')

it 'handles nested interpolations when auto-correction' do
corrected = autocorrect_source(%("this is \#{"\#{1}"} silly"))
Expand Down

0 comments on commit 57f30cd

Please sign in to comment.