Skip to content

Commit

Permalink
[Fix rubocop#8684] Fix an error for Lint/InterpolationCheck cop
Browse files Browse the repository at this point in the history
  • Loading branch information
tejasbubane committed Dec 1, 2020
1 parent 9a8777f commit 7622498
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/fix_fix_an_error_for_lintinterpolationcheck.md
@@ -0,0 +1 @@
* [#8684](https://github.com/rubocop-hq/rubocop/issues/8684): Fix an error for `Lint/InterpolationCheck` cop. ([@tejasbubane][])
9 changes: 7 additions & 2 deletions lib/rubocop/cop/lint/interpolation_check.rb
Expand Up @@ -23,10 +23,11 @@ class InterpolationCheck < Base
'Use double quoted strings if you need interpolation.'

def on_str(node)
parent = node.parent
return if parent && (parent.dstr_type? || parent.regexp_type?)
return unless node
return if string_or_regex?(node.parent)
return unless /(?<!\\)#\{.*\}/.match?(node.source)
return if heredoc?(node)
return unless node.loc.begin && node.loc.end

add_offense(node) do |corrector|
autocorrect(corrector, node)
Expand All @@ -35,6 +36,10 @@ def on_str(node)

private

def string_or_regex?(node)
node&.dstr_type? || node&.regexp_type?
end

def autocorrect(corrector, node)
starting_token, ending_token = if node.source.include?('"')
['%{', '}']
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/lint/interpolation_check_spec.rb
Expand Up @@ -60,4 +60,10 @@
foo = "alpha #{variable} beta \#{gamma}\" delta"
RUBY
end

it 'does not register offense for strings in %w()' do
expect_no_offenses(<<~'RUBY')
%w("#{a}-foo")
RUBY
end
end

0 comments on commit 7622498

Please sign in to comment.