From 57f30cddc7649034f45ed063d5c68a2b9745af35 Mon Sep 17 00:00:00 2001 From: Ahmed Yakout Date: Sun, 28 Apr 2019 05:33:47 +0200 Subject: [PATCH] [Fix #6985] Fix incorrect autocorrect --- CHANGELOG.md | 2 ++ lib/rubocop/cop/lint/literal_in_interpolation.rb | 9 +++++++++ spec/rubocop/cop/lint/literal_in_interpolation_spec.rb | 7 +++++++ 3 files changed, 18 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b909038ed4..22de84fe093 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -3969,3 +3970,4 @@ [@jmanian]: https://github.com/jmanian [@vfonic]: https://github.com/vfonic [@andreaseger]: https://github.com/andreaseger +[@yakout]: https://github.com/yakout diff --git a/lib/rubocop/cop/lint/literal_in_interpolation.rb b/lib/rubocop/cop/lint/literal_in_interpolation.rb index e4b2f412e74..7ae79e04fe7 100644 --- a/lib/rubocop/cop/lint/literal_in_interpolation.rb +++ b/lib/rubocop/cop/lint/literal_in_interpolation.rb @@ -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 @@ -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 @@ -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? || diff --git a/spec/rubocop/cop/lint/literal_in_interpolation_spec.rb b/spec/rubocop/cop/lint/literal_in_interpolation_spec.rb index 9549bfd2231..5f163c4cf89 100644 --- a/spec/rubocop/cop/lint/literal_in_interpolation_spec.rb +++ b/spec/rubocop/cop/lint/literal_in_interpolation_spec.rb @@ -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"))