-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2d257f2
commit e3fa25b
Showing
7 changed files
with
172 additions
and
0 deletions.
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
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
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,67 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpecRails | ||
# Prefer bang methods for persistence. | ||
# | ||
# @safety | ||
# This cop is unsafe because by replacing non-bang versions | ||
# of the persistence methods, you might: | ||
# 1. Catch persistence failures. This is what you want to catch. | ||
# 2. Might replace wrong object calls. | ||
# The cop is unaware if the object is ActiveRecord instance | ||
# | ||
# | ||
# @example | ||
# # bad | ||
# before do | ||
# post.update(title: "new title") | ||
# end | ||
# | ||
# # good | ||
# before do | ||
# post.update!(type: "new title") | ||
# end | ||
# | ||
class BangPersistence < ::RuboCop::Cop::Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Prefer bang versions of the persistence methods.' | ||
|
||
PERSISTENCE_METHODS = %i[ | ||
create | ||
destroy | ||
save | ||
update | ||
update_attribute | ||
].to_set.freeze | ||
|
||
# Match `before` blocks | ||
# @!method before_block?(node) | ||
def_node_matcher :before_block?, <<-PATTERN | ||
(block (send nil? :before) ...) | ||
PATTERN | ||
|
||
# Match `save` and `update` method calls | ||
# @!method peristence_calls(node) | ||
def_node_search :peristence_calls, <<-PATTERN | ||
(send _ {#{PERSISTENCE_METHODS.map { |method| ":#{method}" }.join(' ')}} ...) | ||
PATTERN | ||
|
||
def on_block(node) | ||
return unless before_block?(node) | ||
|
||
peristence_calls(node) do |method_call| | ||
add_offense(method_call) do |corrector| | ||
corrector.replace(method_call.loc.selector, | ||
"#{method_call.method_name}!") | ||
end | ||
end | ||
end | ||
|
||
alias on_numblock on_block | ||
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,49 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpecRails::BangPersistence do | ||
context 'with `save!` in `before`' do | ||
it 'registers no offense' do | ||
expect_no_offenses(<<~RUBY) | ||
before { post.save! } | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with `update!` in `before`' do | ||
it 'registers no offense' do | ||
expect_no_offenses(<<~RUBY) | ||
before { post.update!(title: "new title") } | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with `save` in `before`' do | ||
it 'registers offense' do | ||
expect_offense(<<~RUBY) | ||
before { post.save } | ||
^^^^^^^^^ Prefer bang versions of the persistence methods. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
before { post.save! } | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with `update_attribute` in `before`' do | ||
it 'registers offense' do | ||
expect_offense(<<~RUBY) | ||
before do | ||
post.update_attribute(title: "new title") | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer bang versions of the persistence methods. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
before do | ||
post.update_attribute!(title: "new title") | ||
end | ||
RUBY | ||
end | ||
end | ||
end |