Skip to content

Commit

Permalink
Add Configurable option for Rails/TransactionExitStatement
Browse files Browse the repository at this point in the history
  • Loading branch information
marocchino committed Aug 17, 2023
1 parent 3bc0e2d commit ad71894
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1072](https://github.com/rubocop/rubocop-rails/pull/1072): Add Configurable option for `Rails/TransactionExitStatement`. ([@marocchino][])
3 changes: 3 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,9 @@ Rails/TransactionExitStatement:
Description: 'Avoid the usage of `return`, `break` and `throw` in transaction blocks.'
Reference:
- https://github.com/rails/rails/commit/15aa4200e083
TransactionMethods:
- transaction
- with_lock
Enabled: pending
VersionAdded: '2.14'

Expand Down
10 changes: 10 additions & 0 deletions docs/modules/ROOT/pages/cops_rails.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -6190,6 +6190,16 @@ ApplicationRecord.transaction do
end
----

=== Configurable attributes

|===
| Name | Default value | Configurable values

| TransactionMethods
| `transaction`, `with_lock`
| Array
|===

=== References

* https://github.com/rails/rails/commit/15aa4200e083
Expand Down
8 changes: 5 additions & 3 deletions lib/rubocop/cop/rails/transaction_exit_statement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ class TransactionExitStatement < Base
Exit statement `%<statement>s` is not allowed. Use `raise` (rollback) or `next` (commit).
MSG

RESTRICT_ON_SEND = %i[transaction with_lock].freeze

def_node_search :exit_statements, <<~PATTERN
({return | break | send nil? :throw} ...)
PATTERN
Expand Down Expand Up @@ -85,6 +83,10 @@ def on_send(node)

private

def transaction_methods
cop_config['TransactionMethods'] || []
end

def statement(statement_node)
if statement_node.return_type?
'return'
Expand All @@ -98,7 +100,7 @@ def statement(statement_node)
def nested_block?(statement_node)
block_node = statement_node.ancestors.find(&:block_type?)

RESTRICT_ON_SEND.none? { |name| block_node.method?(name) }
transaction_methods.none? { |name| block_node.method?(name) }
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/rails/transaction_exit_statement_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,10 @@

it_behaves_like 'flags transaction exit statements', :transaction
it_behaves_like 'flags transaction exit statements', :with_lock

context 'transaction methods' do
let(:cop_config) { { 'TransactionMethods' => %w[writable_transaction] } }

it_behaves_like 'flags transaction exit statements', :writable_transaction
end
end

0 comments on commit ad71894

Please sign in to comment.