Sha256: 14866754c889d52997c8c20c4025d95c4d3a34044b1dbc8b6120d4be3e01b3a6

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module ThreadSafety
      # Avoid instance variables in class methods.
      #
      # @example
      #   # bad
      #   class User
      #     def self.notify(info)
      #       @info = validate(info)
      #       Notifier.new(@info).deliver
      #     end
      #   end
      class InstanceVariableInClassMethod < Cop
        MSG = 'Avoid instance variables in class methods.'

        def on_ivar(node)
          return unless class_method_definition?(node)
          return if synchronized?(node)

          add_offense(node, location: :name, message: MSG)
        end
        alias on_ivasgn on_ivar

        private

        def class_method_definition?(node)
          in_defs?(node) ||
            in_def_sclass?(node) ||
            singleton_method_definition?(node)
        end

        def in_defs?(node)
          node.ancestors.any? do |ancestor|
            ancestor.type == :defs
          end
        end

        def in_def_sclass?(node)
          defn = node.ancestors.find do |ancestor|
            ancestor.type == :def
          end

          defn&.ancestors&.any? do |ancestor|
            ancestor.type == :sclass
          end
        end

        def singleton_method_definition?(node)
          node.ancestors.any? do |ancestor|
            next unless ancestor.children.first.is_a? AST::SendNode

            ancestor.children.first.command? :define_singleton_method
          end
        end

        def synchronized?(node)
          node.ancestors.find do |ancestor|
            next unless ancestor.block_type?

            s = ancestor.children.first
            s.send_type? && s.children.last == :synchronize
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-thread_safety-0.4.0 lib/rubocop/cop/thread_safety/instance_variable_in_class_method.rb