Sha256: 5d219cd8de9b9bd3abc9cf5ca699d7c62cafaef1b42b89213cf09fb104871d55

Contents?: true

Size: 1.11 KB

Versions: 4

Compression:

Stored size: 1.11 KB

Contents

# encoding: utf-8

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
        ]

        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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-rspec-1.3.1 lib/rubocop/cop/rspec/instance_variable.rb
rubocop-rspec-1.3.0 lib/rubocop/cop/rspec/instance_variable.rb
rubocop-rspec-1.2.2 lib/rubocop/cop/rspec/instance_variable.rb
rubocop-rspec-1.2.1 lib/rubocop/cop/rspec/instance_variable.rb