Skip to content

Commit 7f709e2

Browse files
authored
Merge pull request #666 from koic/fix_an_error_for_rails_transaction_exit_statement
Fix an error for `Rails/TransactionExitStatement`
2 parents b909f24 + 36d4524 commit 7f709e2

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#666](https://github.com/rubocop/rubocop-rails/pull/666): Fix an error for `Rails/TransactionExitStatement` when transaction block is empty. ([@koic][])

lib/rubocop/cop/rails/transaction_exit_statement.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@ class TransactionExitStatement < Base
5454
PATTERN
5555

5656
def on_send(node)
57-
parent = node.parent
58-
59-
return unless parent&.block_type?
57+
return unless (parent = node.parent)
58+
return unless parent.block_type? && parent.body
6059

6160
exit_statements(parent.body).each do |statement_node|
6261
next unless statement_node.ancestors.find(&:block_type?).method?(:transaction)

spec/rubocop/cop/rails/transaction_exit_statement_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,11 @@
5353
end
5454
RUBY
5555
end
56+
57+
it 'does not register an offense when transaction block is empty' do
58+
expect_no_offenses(<<~RUBY)
59+
ApplicationRecord.transaction do
60+
end
61+
RUBY
62+
end
5663
end

0 commit comments

Comments
 (0)