Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #6985] Fix incorrect autocorrect #6986

Merged
merged 1 commit into from Apr 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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