-
-
Notifications
You must be signed in to change notification settings - Fork 269
/
return_from_stub.rb
169 lines (140 loc) · 4.73 KB
/
return_from_stub.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# frozen_string_literal: true
module RuboCop
module Cop
module RSpec
# Checks for consistent style of stub's return setting.
#
# Enforces either `and_return` or block-style return in the cases
# where the returned value is constant. Ignores dynamic returned values
# are the result would be different
#
# This cop can be configured using the `EnforcedStyle` option
#
# @example `EnforcedStyle: and_return` (default)
# # bad
# allow(Foo).to receive(:bar) { "baz" }
# expect(Foo).to receive(:bar) { "baz" }
#
# # good
# allow(Foo).to receive(:bar).and_return("baz")
# expect(Foo).to receive(:bar).and_return("baz")
# # also good as the returned value is dynamic
# allow(Foo).to receive(:bar) { bar.baz }
#
# @example `EnforcedStyle: block`
# # bad
# allow(Foo).to receive(:bar).and_return("baz")
# expect(Foo).to receive(:bar).and_return("baz")
#
# # good
# allow(Foo).to receive(:bar) { "baz" }
# expect(Foo).to receive(:bar) { "baz" }
# # also good as the returned value is dynamic
# allow(Foo).to receive(:bar).and_return(bar.baz)
#
class ReturnFromStub < Base
extend AutoCorrector
include ConfigurableEnforcedStyle
MSG_AND_RETURN = 'Use `and_return` for static values.'
MSG_BLOCK = 'Use block for static values.'
RESTRICT_ON_SEND = %i[and_return].freeze
# @!method contains_stub?(node)
def_node_search :contains_stub?, '(send nil? :receive (...))'
# @!method stub_with_block?(node)
def_node_matcher :stub_with_block?, '(block #contains_stub? ...)'
# @!method and_return_value(node)
def_node_search :and_return_value, <<~PATTERN
$(send _ :and_return $(...))
PATTERN
def on_send(node)
return unless style == :block
return unless contains_stub?(node)
check_and_return_call(node)
end
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
return unless style == :and_return
return unless stub_with_block?(node)
check_block_body(node)
end
private
def check_and_return_call(node)
and_return_value(node) do |and_return, args|
unless dynamic?(args)
add_offense(and_return.loc.selector, message: MSG_BLOCK) do |corr|
AndReturnCallCorrector.new(and_return).call(corr)
end
end
end
end
def check_block_body(block)
body = block.body
unless dynamic?(body) # rubocop:disable Style/GuardClause
add_offense(block.loc.begin, message: MSG_AND_RETURN) do |corrector|
BlockBodyCorrector.new(block).call(corrector)
end
end
end
def dynamic?(node)
node && !node.recursive_literal_or_const?
end
# :nodoc:
class AndReturnCallCorrector
def initialize(node)
@node = node
@receiver = node.receiver
@arg = node.first_argument
end
def call(corrector)
# Heredoc autocorrection is not yet implemented.
return if heredoc?
corrector.replace(range, " { #{replacement} }")
end
private
attr_reader :node, :receiver, :arg
def heredoc?
arg.loc.is_a?(Parser::Source::Map::Heredoc)
end
def range
Parser::Source::Range.new(
node.source_range.source_buffer,
receiver.source_range.end_pos,
node.source_range.end_pos
)
end
def replacement
if hash_without_braces?
"{ #{arg.source} }"
else
arg.source
end
end
def hash_without_braces?
arg.hash_type? && !arg.braces?
end
end
# :nodoc:
class BlockBodyCorrector
def initialize(block)
@block = block
@node = block.parent
@body = block.body || NULL_BLOCK_BODY
end
def call(corrector)
# Heredoc autocorrection is not yet implemented.
return if heredoc?
corrector.replace(
block,
"#{block.send_node.source}.and_return(#{body.source})"
)
end
private
attr_reader :node, :block, :body
def heredoc?
body.loc.is_a?(Parser::Source::Map::Heredoc)
end
NULL_BLOCK_BODY = Struct.new(:loc, :source).new(nil, 'nil')
end
end
end
end
end