Skip to content

Commit 7e2d6dc

Browse files
authored
Merge pull request #946 from andrykonchin/optimize-performance-instance-variable
Performance. RSpec/InstanceVariable
2 parents c51c544 + dae7025 commit 7e2d6dc

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

lib/rubocop/cop/rspec/instance_variable.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,11 @@ module RSpec
4747
# end
4848
#
4949
class InstanceVariable < Cop
50+
include RuboCop::RSpec::TopLevelGroup
51+
5052
MSG = 'Avoid instance variables – use let, ' \
5153
'a method call, or a local variable (if possible).'
5254

53-
EXAMPLE_GROUP_METHODS = ExampleGroups::ALL + SharedGroups::ALL
54-
55-
def_node_matcher :spec_group?, EXAMPLE_GROUP_METHODS.block_pattern
56-
5755
def_node_matcher :dynamic_class?, <<-PATTERN
5856
(block (send (const nil? :Class) :new ...) ...)
5957
PATTERN
@@ -69,9 +67,7 @@ class InstanceVariable < Cop
6967

7068
def_node_search :ivar_assigned?, '(ivasgn % ...)'
7169

72-
def on_block(node)
73-
return unless spec_group?(node)
74-
70+
def on_top_level_group(node)
7571
ivar_usage(node) do |ivar, name|
7672
next if valid_usage?(ivar)
7773
next if assignment_only? && !ivar_assigned?(node, name)

spec/rubocop/cop/rspec/instance_variable_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,5 +152,19 @@ def serialize
152152
end
153153
RUBY
154154
end
155+
156+
it 'flags an instance variable when it is also assigned ' \
157+
'in a sibling example group' do
158+
expect_offense(<<-RUBY)
159+
describe MyClass do
160+
context 'foo' do
161+
before { @foo = [] }
162+
end
163+
164+
it { expect(@foo).to be_empty }
165+
^^^^ Avoid instance variables – use let, a method call, or a local variable (if possible).
166+
end
167+
RUBY
168+
end
155169
end
156170
end

0 commit comments

Comments
 (0)