|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module RuboCop |
| 4 | + module Cop |
| 5 | + module RSpec |
| 6 | + # Checks for matchers that are used in void context. |
| 7 | + # |
| 8 | + # Matcher calls like `change`, `receive`, etc. that appear as |
| 9 | + # standalone expressions have their result silently discarded. |
| 10 | + # This usually means a missing `.and` to chain compound matchers. |
| 11 | + # |
| 12 | + # @example |
| 13 | + # # bad |
| 14 | + # specify do |
| 15 | + # expect { result }.to \ |
| 16 | + # change { obj.foo }.from(1).to(2) |
| 17 | + # change { obj.bar }.from(3).to(4) |
| 18 | + # end |
| 19 | + # |
| 20 | + # # good |
| 21 | + # specify do |
| 22 | + # expect { result }.to \ |
| 23 | + # change { obj.foo }.from(1).to(2) |
| 24 | + # .and change { obj.bar }.from(3).to(4) |
| 25 | + # end |
| 26 | + # |
| 27 | + # # good |
| 28 | + # specify do |
| 29 | + # expect { result }.to change { obj.foo }.from(1).to(2) |
| 30 | + # end |
| 31 | + # |
| 32 | + class DiscardedMatcher < Base |
| 33 | + MSG = 'The result of `%<method>s` is not used. ' \ |
| 34 | + 'Did you mean to chain it with `.and`?' |
| 35 | + |
| 36 | + MATCHER_METHODS = %i[ |
| 37 | + change |
| 38 | + receive |
| 39 | + receive_messages |
| 40 | + receive_message_chain |
| 41 | + ].to_set.freeze |
| 42 | + |
| 43 | + RESTRICT_ON_SEND = MATCHER_METHODS.freeze |
| 44 | + |
| 45 | + # @!method matcher_call?(node) |
| 46 | + def_node_matcher :matcher_call?, <<~PATTERN |
| 47 | + (send nil? MATCHER_METHODS ...) |
| 48 | + PATTERN |
| 49 | + |
| 50 | + def on_send(node) |
| 51 | + return unless matcher_call?(node) |
| 52 | + return unless inside_example?(node) |
| 53 | + |
| 54 | + target = find_outermost_chain(node) |
| 55 | + return unless void_value?(target) |
| 56 | + |
| 57 | + add_offense(target, message: format(MSG, method: node.method_name)) |
| 58 | + end |
| 59 | + |
| 60 | + def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler |
| 61 | + return unless matcher_call?(node.send_node) |
| 62 | + return unless inside_example?(node) |
| 63 | + |
| 64 | + target = find_outermost_chain(node) |
| 65 | + return unless void_value?(target) |
| 66 | + |
| 67 | + add_offense(target, message: format(MSG, method: node.method_name)) |
| 68 | + end |
| 69 | + |
| 70 | + private |
| 71 | + |
| 72 | + def void_value?(node) |
| 73 | + case node.parent.type |
| 74 | + when :begin |
| 75 | + true |
| 76 | + when :block |
| 77 | + example?(node.parent) |
| 78 | + when :if |
| 79 | + void_value_in_branch?(node, node.parent) |
| 80 | + when :and, :or |
| 81 | + void_in_logical_operator?(node, node.parent) |
| 82 | + when :when, :case |
| 83 | + void_in_case_branch?(node, node.parent) |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + def void_value_in_branch?(node, parent) |
| 88 | + (parent.if_branch == node || parent.else_branch == node) && |
| 89 | + void_value?(parent) |
| 90 | + end |
| 91 | + |
| 92 | + def void_in_logical_operator?(node, parent) |
| 93 | + parent.rhs == node && void_value?(parent) |
| 94 | + end |
| 95 | + |
| 96 | + def void_in_case_branch?(node, parent) |
| 97 | + if parent.when_type? |
| 98 | + parent.body == node && void_value?(parent.parent) |
| 99 | + else |
| 100 | + parent.else_branch == node && void_value?(parent) |
| 101 | + end |
| 102 | + end |
| 103 | + |
| 104 | + def find_outermost_chain(node) |
| 105 | + current = node |
| 106 | + current = current.parent while current.parent.receiver == current |
| 107 | + current |
| 108 | + end |
| 109 | + |
| 110 | + def inside_example?(node) |
| 111 | + node.each_ancestor(:block).any? { |ancestor| example?(ancestor) } |
| 112 | + end |
| 113 | + end |
| 114 | + end |
| 115 | + end |
| 116 | +end |
0 commit comments