Skip to content

Commit

Permalink
Merge pull request #10945 from koic/fix_an_incorrect_autocorrect_for_…
Browse files Browse the repository at this point in the history
…lint_literal_in_interpolation

[Fix #10944] Fix an incorrect autocorrect for `Lint/LiteralInInterpolation`
  • Loading branch information
koic committed Aug 22, 2022
2 parents 0048f70 + b69ca43 commit e99bc6c
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
@@ -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

0 comments on commit e99bc6c

Please sign in to comment.