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 #10944] Fix an incorrect autocorrect for Lint/LiteralInInterpolation #10945

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
@@ -0,0 +1 @@
* [#10944](https://github.com/rubocop/rubocop/issues/10944): Fix an incorrect autocorrect for `Lint/LiteralInInterpolation` when using `"#{nil}"`. ([@koic][])
4 changes: 4 additions & 0 deletions lib/rubocop/cop/lint/literal_in_interpolation.rb
Expand Up @@ -58,6 +58,7 @@ def special_keyword?(node)
(node.str_type? && !node.loc.respond_to?(:begin)) || node.source_range.is?('__LINE__')
end

# rubocop:disable Metrics/MethodLength
def autocorrected_value(node)
case node.type
when :int
Expand All @@ -70,10 +71,13 @@ def autocorrected_value(node)
autocorrected_value_for_symbol(node)
when :array
autocorrected_value_for_array(node)
when :nil
''
else
node.source.gsub('"', '\"')
end
end
# rubocop:enable Metrics/MethodLength

def autocorrected_value_for_string(node)
if node.source.start_with?("'", '%q')
Expand Down
2 changes: 1 addition & 1 deletion spec/rubocop/cop/lint/literal_in_interpolation_spec.rb
Expand Up @@ -102,7 +102,7 @@
it_behaves_like('literal interpolation', '{"a" => "b"}', '{\"a\" => \"b\"}')
it_behaves_like('literal interpolation', true)
it_behaves_like('literal interpolation', false)
it_behaves_like('literal interpolation', 'nil')
it_behaves_like('literal interpolation', 'nil', '')
it_behaves_like('literal interpolation', ':symbol', 'symbol')
it_behaves_like('literal interpolation', ':"symbol"', 'symbol')
it_behaves_like('literal interpolation', 1..2)
Expand Down