forked from rubocop/rubocop-rspec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance_variable.rb
42 lines (39 loc) · 1.15 KB
/
instance_variable.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
# encoding: utf-8
# frozen_string_literal: true
module RuboCop
module Cop
module RSpec
# When you have to assign a variable instead of using an instance
# variable, use let.
#
# @example
# # bad
# describe MyClass do
# before { @foo = [] }
# it { expect(@foo).to be_empty }
# end
#
# # good
# describe MyClass do
# let(:foo) { [] }
# it { expect(foo).to be_empty }
# end
class InstanceVariable < Cop
MESSAGE = 'Use `let` instead of an instance variable'
EXAMPLE_GROUP_METHODS = [
:example_group, :describe, :context, :xdescribe, :xcontext,
:fdescribe, :fcontext, :shared_examples, :shared_context,
:share_examples_for, :shared_examples_for, :feature
].freeze
def on_block(node)
method, _args, _body = *node
_receiver, method_name, _object = *method
@in_spec = true if EXAMPLE_GROUP_METHODS.include?(method_name)
end
def on_ivar(node)
add_offense(node, :expression, MESSAGE) if @in_spec
end
end
end
end
end