Sha256: 2b91dd05d44e307b6d9599fae2e05f24bdfb68a776596f367eab25aa9a5ac3f7

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

# encoding: utf-8

module Rubocop
  module Cop
    # 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_emtpy }
    #   end
    #
    #   # good
    #   describe MyClass do
    #     let(:foo) { [] }
    #     it { expect(foo).to be_emtpy }
    #   end
    class RSpecInstanceVariable < 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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-rspec-1.0.rc2 lib/rubocop/cop/rspec_instance_variable.rb
rubocop-rspec-1.0.rc1 lib/rubocop/cop/rspec_instance_variable.rb