Skip to content

Commit 949b7a3

Browse files
authored
Merge pull request #753 from r7kamura/feature/compact-blank
Fix `Rails/CompactBlank` bug when offense is found in block
2 parents a4fa778 + 0d883b5 commit 949b7a3

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#753](https://github.com/rubocop/rubocop-rails/pull/753): Fix `Rails/CompactBlank` bug when offense is found in block. ([@r7kamura][])

lib/rubocop/cop/rails/compact_blank.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@ def use_hash_value_block_argument?(arguments, receiver_in_block)
9393
end
9494

9595
def offense_range(node)
96-
end_pos = node.parent&.block_type? ? node.parent.loc.expression.end_pos : node.loc.expression.end_pos
96+
end_pos = if node.parent&.block_type? && node.parent&.send_node == node
97+
node.parent.loc.expression.end_pos
98+
else
99+
node.loc.expression.end_pos
100+
end
97101

98102
range_between(node.loc.selector.begin_pos, end_pos)
99103
end

spec/rubocop/cop/rails/compact_blank_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@
6868
RUBY
6969
end
7070

71+
it 'registers and corrects an offense when using `reject(&:blank?)` in block' do
72+
expect_offense(<<~RUBY)
73+
hash.transform_values { |value| value.reject(&:blank?) }
74+
^^^^^^^^^^^^^^^^ Use `compact_blank` instead.
75+
RUBY
76+
77+
expect_correction(<<~RUBY)
78+
hash.transform_values { |value| value.compact_blank }
79+
RUBY
80+
end
81+
7182
it 'does not register an offense when using `compact_blank`' do
7283
expect_no_offenses(<<~RUBY)
7384
collection.compact_blank

0 commit comments

Comments
 (0)