|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module RuboCop |
| 4 | + module Cop |
| 5 | + module RSpecRails |
| 6 | + # Prefer bang methods for persistence. |
| 7 | + # |
| 8 | + # @safety |
| 9 | + # This cop is unsafe because by replacing non-bang versions |
| 10 | + # of the persistence methods, you might: |
| 11 | + # 1. Catch persistence failures. This is what you want to catch. |
| 12 | + # 2. Might replace wrong object calls. |
| 13 | + # The cop is unaware if the object is ActiveRecord instance |
| 14 | + # |
| 15 | + # |
| 16 | + # @example |
| 17 | + # # bad |
| 18 | + # before do |
| 19 | + # post.update(title: "new title") |
| 20 | + # end |
| 21 | + # |
| 22 | + # # good |
| 23 | + # before do |
| 24 | + # post.update!(type: "new title") |
| 25 | + # end |
| 26 | + # |
| 27 | + class BangPersistence < ::RuboCop::Cop::Base |
| 28 | + extend AutoCorrector |
| 29 | + |
| 30 | + MSG = 'Prefer bang versions of the persistence methods.' |
| 31 | + |
| 32 | + PERSISTENCE_METHODS = %i[ |
| 33 | + create |
| 34 | + destroy |
| 35 | + save |
| 36 | + update |
| 37 | + update_attribute |
| 38 | + ].to_set.freeze |
| 39 | + |
| 40 | + # Match `before` blocks |
| 41 | + # @!method before_block?(node) |
| 42 | + def_node_matcher :before_block?, <<-PATTERN |
| 43 | + (block (send nil? :before) ...) |
| 44 | + PATTERN |
| 45 | + |
| 46 | + # Match `save` and `update` method calls |
| 47 | + # @!method peristence_calls(node) |
| 48 | + def_node_search :peristence_calls, <<-PATTERN |
| 49 | + (send _ {#{PERSISTENCE_METHODS.map { |method| ":#{method}" }.join(' ')}} ...) |
| 50 | + PATTERN |
| 51 | + |
| 52 | + def on_block(node) |
| 53 | + return unless before_block?(node) |
| 54 | + |
| 55 | + peristence_calls(node) do |method_call| |
| 56 | + add_offense(method_call) do |corrector| |
| 57 | + corrector.replace(method_call.loc.selector, |
| 58 | + "#{method_call.method_name}!") |
| 59 | + end |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + alias on_numblock on_block |
| 64 | + end |
| 65 | + end |
| 66 | + end |
| 67 | +end |
0 commit comments