rubocop-rspec 1.22.0 → 1.22.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 63bb2b52f05acc607af023c9e7afa5375d5815cf026b73fda3dc10c8648806d2
4
- data.tar.gz: c0bf3717a359f99ec9dd9519f5897c95dcc3e35f592baaad3dc037f1ae25c7d8
3
+ metadata.gz: ace522f3766b6db17eecaffa9d4a5b2efc6f4cf81a581211afed52fb7ed18a1a
4
+ data.tar.gz: d38f31b69bc99ff14ca490b0a7ad1a84284d1b30bc73d347ba01e8a90831198d
5
5
  SHA512:
6
- metadata.gz: 4af0c95c0baf2a8dc469767c5455105276ea42f1c66ffadd09760e40eeecd0d362afcc7886b77931f0eafcc3a0785f0126ab4a3924ac4a07b1a8d94344c26d7a
7
- data.tar.gz: 3712863da50c7d61207b76a92002432469e7021a87dcc1a4af1a8779581fc6a7b8beb9fc2e2017b8a45adace69f3e7a5dc6f9e9e2d47ca067c84df576cb8919d
6
+ metadata.gz: 8efef00f7046db8b8bf54d8585e43e999a570204aaa5b645a5b73271d84bedd7d9694ac2de40af45b457c4f5f3469a04219cb3d140d4cb6b001cb316751969f4
7
+ data.tar.gz: af58c7c3e41514372c3d88dbab4e4613256b0233075b7e71ee18af6158aa14d0828c7ff0eb2f4327d75fbb74e465bd1ae57378073f5d4e23817d3ef7665b7edc
@@ -2,6 +2,10 @@
2
2
 
3
3
  ## Master (Unreleased)
4
4
 
5
+ ## 1.22.1 (2018-01-17)
6
+
7
+ * Fix false positives in `RSpec/ReturnFromStub`. ([@Darhazer][])
8
+
5
9
  ## 1.22.0 (2018-01-10)
6
10
 
7
11
  * Updates `describe_class` to account for RSpecs `:system` wrapper of rails system tests. ([@EliseFitz15][])
@@ -39,18 +39,23 @@ module RuboCop
39
39
  MSG_AND_RETURN = 'Use `and_return` for static values.'.freeze
40
40
  MSG_BLOCK = 'Use block for static values.'.freeze
41
41
 
42
- def_node_matcher :and_return_value, <<-PATTERN
43
- (send
44
- (send nil? :receive (...)) :and_return $(...)
45
- )
42
+ def_node_search :contains_stub?, '(send nil? :receive (...))'
43
+ def_node_search :and_return_value, <<-PATTERN
44
+ $(send _ :and_return $(...))
46
45
  PATTERN
47
46
 
48
47
  def on_send(node)
49
- if style == :block
50
- check_and_return_call(node)
51
- elsif node.method_name == :receive
52
- check_block_body(node)
53
- end
48
+ return unless contains_stub?(node)
49
+ return unless style == :block
50
+
51
+ check_and_return_call(node)
52
+ end
53
+
54
+ def on_block(node)
55
+ return unless contains_stub?(node)
56
+ return unless style == :and_return
57
+
58
+ check_block_body(node)
54
59
  end
55
60
 
56
61
  def autocorrect(node)
@@ -64,26 +69,23 @@ module RuboCop
64
69
  private
65
70
 
66
71
  def check_and_return_call(node)
67
- and_return_value(node) do |args|
72
+ and_return_value(node) do |and_return, args|
68
73
  unless dynamic?(args)
69
74
  add_offense(
70
- node,
71
- location: :expression,
75
+ and_return,
76
+ location: :selector,
72
77
  message: MSG_BLOCK
73
78
  )
74
79
  end
75
80
  end
76
81
  end
77
82
 
78
- def check_block_body(node)
79
- block = node.each_ancestor(:block).first
80
- return unless block
81
-
82
- _receiver, _args, body = *block
83
+ def check_block_body(block)
84
+ body = block.body
83
85
  unless body && dynamic?(body) # rubocop:disable Style/GuardClause
84
86
  add_offense(
85
- node,
86
- location: :expression,
87
+ block,
88
+ location: :begin,
87
89
  message: MSG_AND_RETURN
88
90
  )
89
91
  end
@@ -138,9 +140,9 @@ module RuboCop
138
140
 
139
141
  # :nodoc:
140
142
  class BlockBodyCorrector
141
- def initialize(node)
142
- @block = node.each_ancestor(:block).first
143
- @node = node
143
+ def initialize(block)
144
+ @block = block
145
+ @node = block.parent
144
146
  @body = block.body || NULL_BLOCK_BODY
145
147
  end
146
148
 
@@ -148,7 +150,10 @@ module RuboCop
148
150
  # Heredoc autocorrection is not yet implemented.
149
151
  return if heredoc?
150
152
 
151
- corrector.replace(range, ".and_return(#{body.source})")
153
+ corrector.replace(
154
+ block.loc.expression,
155
+ "#{block.send_node.source}.and_return(#{body.source})"
156
+ )
152
157
  end
153
158
 
154
159
  private
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module RSpec
5
5
  # Version information for the RSpec RuboCop plugin.
6
6
  module Version
7
- STRING = '1.22.0'.freeze
7
+ STRING = '1.22.1'.freeze
8
8
  end
9
9
  end
10
10
  end
@@ -12,7 +12,7 @@ RSpec.describe RuboCop::Cop::RSpec::ReturnFromStub, :config do
12
12
  expect_offense(<<-RUBY)
13
13
  it do
14
14
  allow(Foo).to receive(:bar) { 42 }
15
- ^^^^^^^^^^^^^ Use `and_return` for static values.
15
+ ^ Use `and_return` for static values.
16
16
  end
17
17
  RUBY
18
18
  end
@@ -21,7 +21,7 @@ RSpec.describe RuboCop::Cop::RSpec::ReturnFromStub, :config do
21
21
  expect_offense(<<-RUBY)
22
22
  it do
23
23
  allow(Foo).to receive(:bar) {}
24
- ^^^^^^^^^^^^^ Use `and_return` for static values.
24
+ ^ Use `and_return` for static values.
25
25
  end
26
26
  RUBY
27
27
  end
@@ -30,7 +30,7 @@ RSpec.describe RuboCop::Cop::RSpec::ReturnFromStub, :config do
30
30
  expect_offense(<<-RUBY)
31
31
  it do
32
32
  allow(Foo).to receive(:bar) { [42, 43] }
33
- ^^^^^^^^^^^^^ Use `and_return` for static values.
33
+ ^ Use `and_return` for static values.
34
34
  end
35
35
  RUBY
36
36
  end
@@ -39,7 +39,16 @@ RSpec.describe RuboCop::Cop::RSpec::ReturnFromStub, :config do
39
39
  expect_offense(<<-RUBY)
40
40
  it do
41
41
  allow(Foo).to receive(:bar) { {a: 42, b: 43} }
42
- ^^^^^^^^^^^^^ Use `and_return` for static values.
42
+ ^ Use `and_return` for static values.
43
+ end
44
+ RUBY
45
+ end
46
+
47
+ it 'finds static values in a block when there are chained methods' do
48
+ expect_offense(<<-RUBY)
49
+ it do
50
+ allow(Question).to receive(:meaning).with(:universe) { 42 }
51
+ ^ Use `and_return` for static values.
43
52
  end
44
53
  RUBY
45
54
  end
@@ -92,7 +101,7 @@ RSpec.describe RuboCop::Cop::RSpec::ReturnFromStub, :config do
92
101
  expect_offense(<<-RUBY)
93
102
  it do
94
103
  allow(Foo).to receive(:bar) do
95
- ^^^^^^^^^^^^^ Use `and_return` for static values.
104
+ ^^ Use `and_return` for static values.
96
105
  "You called" \
97
106
  "me"
98
107
  end
@@ -124,6 +133,10 @@ RSpec.describe RuboCop::Cop::RSpec::ReturnFromStub, :config do
124
133
  'allow(Foo).to receive(:bar) { { foo: 42 } }',
125
134
  'allow(Foo).to receive(:bar).and_return({ foo: 42 })'
126
135
 
136
+ include_examples 'autocorrect',
137
+ 'allow(Foo).to receive(:bar).with(1) { 42 }',
138
+ 'allow(Foo).to receive(:bar).with(1).and_return(42)'
139
+
127
140
  include_examples 'autocorrect',
128
141
  'allow(Foo).to receive(:bar) {}',
129
142
  'allow(Foo).to receive(:bar).and_return(nil)'
@@ -149,7 +162,16 @@ RSpec.describe RuboCop::Cop::RSpec::ReturnFromStub, :config do
149
162
  expect_offense(<<-RUBY)
150
163
  it do
151
164
  allow(Foo).to receive(:bar).and_return(42)
152
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use block for static values.
165
+ ^^^^^^^^^^ Use block for static values.
166
+ end
167
+ RUBY
168
+ end
169
+
170
+ it 'finds static values returned from chained method' do
171
+ expect_offense(<<-RUBY)
172
+ it do
173
+ allow(Foo).to receive(:bar).with(1).and_return(42)
174
+ ^^^^^^^^^^ Use block for static values.
153
175
  end
154
176
  RUBY
155
177
  end
@@ -183,6 +205,10 @@ RSpec.describe RuboCop::Cop::RSpec::ReturnFromStub, :config do
183
205
  'allow(Foo).to receive(:bar).and_return(42)',
184
206
  'allow(Foo).to receive(:bar) { 42 }'
185
207
 
208
+ include_examples 'autocorrect',
209
+ 'allow(Foo).to receive(:bar).with(1).and_return(foo: 42)',
210
+ 'allow(Foo).to receive(:bar).with(1) { { foo: 42 } }'
211
+
186
212
  include_examples 'autocorrect',
187
213
  'allow(Foo).to receive(:bar).and_return({ foo: 42 })',
188
214
  'allow(Foo).to receive(:bar) { { foo: 42 } }'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-rspec
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.22.0
4
+ version: 1.22.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Backus
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2018-01-10 00:00:00.000000000 Z
13
+ date: 2018-01-17 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rubocop
@@ -272,7 +272,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
272
272
  version: '0'
273
273
  requirements: []
274
274
  rubyforge_project:
275
- rubygems_version: 2.7.2
275
+ rubygems_version: 2.7.4
276
276
  signing_key:
277
277
  specification_version: 4
278
278
  summary: Code style checking for RSpec files