-
-
Notifications
You must be signed in to change notification settings - Fork 263
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
Add new Rails/PrivateTransactionOption
cop
#1236
Open
wata727
wants to merge
3
commits into
rubocop:master
Choose a base branch
from
wata727:add_new_rails_private_transaction_option
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#1236](https://github.com/rubocop/rubocop-rails/pull/1236): Add new `Rails/PrivateTransactionOption`. ([@wata727][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# Checks whether `ActiveRecord::Base.transaction(joinable: _)` is used. | ||
# | ||
# The `joinable` option is a private API and is not intended to be called | ||
# from outside Active Record core. | ||
# https://github.com/rails/rails/issues/39912#issuecomment-665483779 | ||
# https://github.com/rails/rails/issues/46182#issuecomment-1265966330 | ||
# | ||
# Passing `joinable: false` may cause unexpected behavior such as the | ||
# `after_commit` callback not firing at the appropriate time. | ||
# | ||
# @safety | ||
# This Cop is unsafe because it cannot accurately identify | ||
# the `ActiveRecord::Base.transaction` method call. | ||
# | ||
# @example | ||
# # bad | ||
# ActiveRecord::Base.transaction(requires_new: true, joinable: false) | ||
# | ||
# # good | ||
# ActiveRecord::Base.transaction(requires_new: true) | ||
# | ||
class PrivateTransactionOption < Base | ||
MSG = 'Use a negated `requires_new` option instead of the internal `joinable`.' | ||
RESTRICT_ON_SEND = %i[transaction].freeze | ||
|
||
# @!method match_transaction_with_joinable(node) | ||
def_node_matcher :match_transaction_with_joinable, <<~PATTERN | ||
(send _ :transaction (hash <$(pair (sym :joinable) {true false}) ...>)) | ||
PATTERN | ||
|
||
def on_send(node) | ||
match_transaction_with_joinable(node) do |option_node| | ||
add_offense(option_node) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::PrivateTransactionOption, :config do | ||
it 'registers an offense when using `ActiveRecord::Base.joinable: false`' do | ||
expect_offense(<<~RUBY) | ||
ActiveRecord::Base.transaction(requires_new: true, joinable: false) do | ||
^^^^^^^^^^^^^^^ Use a negated `requires_new` option instead of the internal `joinable`. | ||
# ... | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `Account.transaction(joinable: false)`' do | ||
expect_offense(<<~RUBY) | ||
Account.transaction(requires_new: true, joinable: false) do | ||
^^^^^^^^^^^^^^^ Use a negated `requires_new` option instead of the internal `joinable`. | ||
# ... | ||
end | ||
RUBY | ||
end | ||
|
||
wata727 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
it 'does not register an offense when using only `requires_new: true`' do | ||
expect_no_offenses(<<~RUBY) | ||
ActiveRecord::Base.transaction(requires_new: true) do | ||
# ... | ||
end | ||
RUBY | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I bet
transaction(joinable: true/false)
would be a Rails transaction call with very high certainty.I'd remove this clause and made the cop safe.
I would accept a single case of such a false positive from https://github.com/jeromedalbert/real-world-ruby-apps as a counter-argument.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, this is up for debate. Personally, I believe it should be marked as unsafe as long as there is theoretically a possibility of false positives.
I'd be interested to hear from other maintainers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that the issue is not a false positive in the cop, but rather the change in behavior caused by the autocorrect. Therefore, shouldn't it be
SafeAutoCorrect: false
instead ofSafe: false
?