Skip to content

Commit

Permalink
Merge pull request #753 from r7kamura/feature/compact-blank
Browse files Browse the repository at this point in the history
Fix `Rails/CompactBlank` bug when offense is found in block
  • Loading branch information
koic authored Aug 6, 2022
2 parents a4fa778 + 0d883b5 commit 949b7a3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_fix_railscompactblank_bug_when_offense.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#753](https://github.com/rubocop/rubocop-rails/pull/753): Fix `Rails/CompactBlank` bug when offense is found in block. ([@r7kamura][])
6 changes: 5 additions & 1 deletion lib/rubocop/cop/rails/compact_blank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ def use_hash_value_block_argument?(arguments, receiver_in_block)
end

def offense_range(node)
end_pos = node.parent&.block_type? ? node.parent.loc.expression.end_pos : node.loc.expression.end_pos
end_pos = if node.parent&.block_type? && node.parent&.send_node == node
node.parent.loc.expression.end_pos
else
node.loc.expression.end_pos
end

range_between(node.loc.selector.begin_pos, end_pos)
end
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/rails/compact_blank_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@
RUBY
end

it 'registers and corrects an offense when using `reject(&:blank?)` in block' do
expect_offense(<<~RUBY)
hash.transform_values { |value| value.reject(&:blank?) }
^^^^^^^^^^^^^^^^ Use `compact_blank` instead.
RUBY

expect_correction(<<~RUBY)
hash.transform_values { |value| value.compact_blank }
RUBY
end

it 'does not register an offense when using `compact_blank`' do
expect_no_offenses(<<~RUBY)
collection.compact_blank
Expand Down

0 comments on commit 949b7a3

Please sign in to comment.