Skip to content

Commit

Permalink
Merge pull request #1453 from Earlopain/transaction-exit-numblock
Browse files Browse the repository at this point in the history
Make `Rails/TransactionExitStatement` aware of numblocks
  • Loading branch information
koic authored Feb 27, 2025
2 parents f150386 + 41c32f1 commit 7731291
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/fix_transaction_exit_numblocks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1453](https://github.com/rubocop/rubocop-rails/pull/1453): Make `Rails/TransactionExitStatement` aware of numblocks. ([@earlopain][])
4 changes: 2 additions & 2 deletions lib/rubocop/cop/rails/transaction_exit_statement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def in_transaction_block?(node)
return false unless transaction_method_name?(node.method_name)
return false unless (parent = node.parent)

parent.block_type? && parent.body
parent.any_block_type? && parent.body
end

def statement(statement_node)
Expand All @@ -113,7 +113,7 @@ def statement(statement_node)
end

def nested_block?(statement_node)
name = statement_node.ancestors.find(&:block_type?).children.first.method_name
name = statement_node.ancestors.find(&:any_block_type?).children.first.method_name
!transaction_method_name?(name)
end

Expand Down
31 changes: 31 additions & 0 deletions spec/rubocop/cop/rails/transaction_exit_statement_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@
RUBY
end

it 'registers an offense when `return` is used in transaction with numblock' do
expect_offense(<<~RUBY, method: method)
ApplicationRecord.%{method} do
_1.after_commit { }
return if user.active?
^^^^^^ Exit statement `return` is not allowed. Use `raise` (rollback) or `next` (commit).
end
RUBY
end

it 'registers an offense when `break` is used in transactions' do
expect_offense(<<~RUBY, method: method)
ApplicationRecord.%{method} do
Expand Down Expand Up @@ -56,6 +66,17 @@
RUBY
end

it 'registers an offense when `return` is used in `each` with numblock in transactions' do
expect_offense(<<~RUBY, method: method)
ApplicationRecord.%{method} do
foo.each do
return if _1
^^^^^^ Exit statement `return` is not allowed. Use `raise` (rollback) or `next` (commit).
end
end
RUBY
end

it 'registers an offense when `throw` is used in `loop` in transactions' do
expect_offense(<<~RUBY, method: method)
ApplicationRecord.%{method} do
Expand All @@ -77,6 +98,16 @@
RUBY
end

it 'does not register an offense when `break` is used in `each` with numblock in transactions' do
expect_no_offenses(<<~RUBY)
ApplicationRecord.#{method} do
foo.each do
break if _1
end
end
RUBY
end

it 'registers an offense when `return` is used in `rescue`' do
expect_offense(<<~RUBY, method: method)
ApplicationRecord.%{method} do
Expand Down

0 comments on commit 7731291

Please sign in to comment.