Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Rails/CompactBlank bug when offense is found in block #753

Merged
merged 1 commit into from Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/fix_fix_railscompactblank_bug_when_offense.md
@@ -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
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
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