Skip to content

Commit

Permalink
[Fix #6985] Fix Lint/LiteralInInterpolation's autocorrect (#6986)
Browse files Browse the repository at this point in the history
This fixes an incorrect auto-correct for Lint/LiteralInInterpolation if contains array percent literals.
  • Loading branch information
yakout authored and bbatsov committed Apr 29, 2019
1 parent 666587c commit 17cdf5a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 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][])
* [#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 @@ -3971,3 +3972,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 17cdf5a

Please sign in to comment.