Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance. RSpec/InstanceVariable #946

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 3 additions & 7 deletions lib/rubocop/cop/rspec/instance_variable.rb
Expand Up @@ -47,13 +47,11 @@ module RSpec
# end
#
class InstanceVariable < Cop
include RuboCop::RSpec::TopLevelGroup

MSG = 'Avoid instance variables – use let, ' \
'a method call, or a local variable (if possible).'

EXAMPLE_GROUP_METHODS = ExampleGroups::ALL + SharedGroups::ALL

def_node_matcher :spec_group?, EXAMPLE_GROUP_METHODS.block_pattern

def_node_matcher :dynamic_class?, <<-PATTERN
(block (send (const nil? :Class) :new ...) ...)
PATTERN
Expand All @@ -69,9 +67,7 @@ class InstanceVariable < Cop

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

def on_block(node)
return unless spec_group?(node)

def on_top_level_group(node)
ivar_usage(node) do |ivar, name|
next if valid_usage?(ivar)
next if assignment_only? && !ivar_assigned?(node, name)
Expand Down
14 changes: 14 additions & 0 deletions spec/rubocop/cop/rspec/instance_variable_spec.rb
Expand Up @@ -152,5 +152,19 @@ def serialize
end
RUBY
end

it 'flags an instance variable when it is also assigned ' \
Darhazer marked this conversation as resolved.
Show resolved Hide resolved
'in a sibling example group' do
expect_offense(<<-RUBY)
describe MyClass do
context 'foo' do
before { @foo = [] }
end

it { expect(@foo).to be_empty }
^^^^ Avoid instance variables – use let, a method call, or a local variable (if possible).
end
RUBY
end
end
end