Sha256: cd6d5dc3fba7f8dcf9f0562eb1f2a9f79adc11126f91984ec70eb7d1489fd7af

Contents?: true

Size: 1.41 KB

Versions: 25

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

require 'set'
require_relative 'base_detector'

module Reek
  module SmellDetectors
    #
    # Class variables form part of the global runtime state, and as such make
    # it easy for one part of the system to accidentally or inadvertently
    # depend on another part of the system. So the system becomes more prone to
    # problems where changing something over here breaks something over there.
    # In particular, class variables can make it hard to set up tests (because
    # the context of the test includes all global state).
    #
    # See {file:docs/Class-Variable.md} for details.
    class ClassVariable < BaseDetector
      def self.contexts # :nodoc:
        [:class, :module]
      end

      #
      # Checks whether the given class or module declares any class variables.
      #
      # @return [Array<SmellWarning>]
      #
      def sniff
        class_variables_in_context.map do |variable, occurences|
          lines = occurences.map(&:line)
          smell_warning(
            lines: lines,
            message: "declares the class variable '#{variable}'",
            parameters: { name: variable.to_s })
        end
      end

      #
      # Collects the names of the class variables declared and/or used
      # in the given module.
      #
      def class_variables_in_context
        context.local_nodes([:cvar, :cvasgn, :cvdecl]).group_by(&:name)
      end
    end
  end
end

Version data entries

25 entries across 23 versions & 2 rubygems

Version Path
reek-6.1.1 lib/reek/smell_detectors/class_variable.rb
reek-6.1.0 lib/reek/smell_detectors/class_variable.rb
reek-6.0.6 lib/reek/smell_detectors/class_variable.rb
reek-6.0.5 lib/reek/smell_detectors/class_variable.rb
reek-6.0.4 lib/reek/smell_detectors/class_variable.rb
reek-6.0.3 lib/reek/smell_detectors/class_variable.rb
reek-6.0.2 lib/reek/smell_detectors/class_variable.rb
reek-6.0.1 lib/reek/smell_detectors/class_variable.rb
reek-6.0.0 lib/reek/smell_detectors/class_variable.rb
reek-5.6.0 lib/reek/smell_detectors/class_variable.rb
reek-5.5.0 lib/reek/smell_detectors/class_variable.rb
reek-5.4.1 lib/reek/smell_detectors/class_variable.rb
reek-5.4.0 lib/reek/smell_detectors/class_variable.rb
reek-5.3.2 lib/reek/smell_detectors/class_variable.rb
reek-5.3.1 lib/reek/smell_detectors/class_variable.rb
reek-5.3.0 lib/reek/smell_detectors/class_variable.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/reek-5.2.0/lib/reek/smell_detectors/class_variable.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/reek-5.2.0/lib/reek/smell_detectors/class_variable.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/reek-5.2.0/lib/reek/smell_detectors/class_variable.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/reek-5.2.0/lib/reek/smell_detectors/class_variable.rb