Sha256: 8747aadc319e29125a07768cdc328a2798fcf5cc3518b00413d5fad089e52e65

Contents?: true

Size: 1.41 KB

Versions: 16

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

require_relative 'base_detector'

module Reek
  module SmellDetectors
    #
    # Subclassing core classes in Ruby can lead to unexpected side effects.
    # Knowing that Ruby has a core library, which is written in C, and a standard
    # library, which is written in Ruby, if you do not know exactly how these core
    # classes operate at the C level, you are gonna have a bad time.
    #
    # Source: http://words.steveklabnik.com/beware-subclassing-ruby-core-classes
    #
    class SubclassedFromCoreClass < BaseDetector
      CORE_CLASSES = ['Array', 'Hash', 'String'].freeze

      def self.contexts
        [:class, :casgn]
      end

      # Checks +ctx+ for either expressions:
      #
      # Foo = Class.new(Bar)
      #
      # class Foo < Bar; end;
      #
      # @return [Array<SmellWarning>]
      def sniff
        superclass = expression.superclass

        return [] unless superclass

        sniff_superclass superclass.name
      end

      private

      def sniff_superclass(superclass_name)
        return [] unless CORE_CLASSES.include?(superclass_name)

        [build_smell_warning(superclass_name)]
      end

      def build_smell_warning(ancestor_name)
        smell_warning(lines:      [source_line],
                      message:    "inherits from core class '#{ancestor_name}'",
                      parameters: { ancestor: ancestor_name })
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
reek-6.4.0 lib/reek/smell_detectors/subclassed_from_core_class.rb
reek-6.3.0 lib/reek/smell_detectors/subclassed_from_core_class.rb
reek-6.2.0 lib/reek/smell_detectors/subclassed_from_core_class.rb
reek-6.1.4 lib/reek/smell_detectors/subclassed_from_core_class.rb
reek-6.1.3 lib/reek/smell_detectors/subclassed_from_core_class.rb
reek-6.1.2 lib/reek/smell_detectors/subclassed_from_core_class.rb
reek-6.1.1 lib/reek/smell_detectors/subclassed_from_core_class.rb
reek-6.1.0 lib/reek/smell_detectors/subclassed_from_core_class.rb
reek-6.0.6 lib/reek/smell_detectors/subclassed_from_core_class.rb
reek-6.0.5 lib/reek/smell_detectors/subclassed_from_core_class.rb
reek-6.0.4 lib/reek/smell_detectors/subclassed_from_core_class.rb
reek-6.0.3 lib/reek/smell_detectors/subclassed_from_core_class.rb
reek-6.0.2 lib/reek/smell_detectors/subclassed_from_core_class.rb
reek-6.0.1 lib/reek/smell_detectors/subclassed_from_core_class.rb
reek-6.0.0 lib/reek/smell_detectors/subclassed_from_core_class.rb
reek-5.6.0 lib/reek/smell_detectors/subclassed_from_core_class.rb