Skip to content

Commit bb5d1a3

Browse files
committed
[Fix #14605] Fix false positive for Lint/EmptyInterpolation when interpolation is inside a %W literal
1 parent 783a4bf commit bb5d1a3

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#14605](https://github.com/rubocop/rubocop/issues/14605): Fix false positive for `Lint/EmptyInterpolation` when interpolation is inside a `%W` literal. ([@dvandersluis][])

lib/rubocop/cop/lint/empty_interpolation.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,23 @@ class EmptyInterpolation < Base
1919
MSG = 'Empty interpolation detected.'
2020

2121
def on_interpolation(begin_node)
22+
return if in_percent_literal_array?(begin_node)
23+
2224
node_children = begin_node.children.dup
2325
node_children.delete_if { |e| e.nil_type? || (e.basic_literal? && e.str_content&.empty?) }
2426
return unless node_children.empty?
2527

2628
add_offense(begin_node) { |corrector| corrector.remove(begin_node) }
2729
end
30+
31+
private
32+
33+
def in_percent_literal_array?(begin_node)
34+
array_node = begin_node.each_ancestor(:array).first
35+
return false unless array_node
36+
37+
array_node.percent_literal?
38+
end
2839
end
2940
end
3041
end

spec/rubocop/cop/lint/empty_interpolation_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,27 @@
8282
"this is the #{true}"
8383
RUBY
8484
end
85+
86+
it 'does not register an offense for an empty string interpolation inside a `%W` literal' do
87+
expect_no_offenses(<<~'RUBY')
88+
%W[#{''} one two]
89+
RUBY
90+
end
91+
92+
it 'does not register an offense for an empty string interpolation inside a `%I` literal' do
93+
expect_no_offenses(<<~'RUBY')
94+
%I[#{''} one two]
95+
RUBY
96+
end
97+
98+
it 'registers an offense for an empty string interpolation inside an array' do
99+
expect_offense(<<~'RUBY')
100+
["#{''}", one, two]
101+
^^^^^ Empty interpolation detected.
102+
RUBY
103+
104+
expect_correction(<<~RUBY)
105+
["", one, two]
106+
RUBY
107+
end
85108
end

0 commit comments

Comments
 (0)