Sha256: 26711c38eaff1ccf50be8457329e00caae7ab6178f9c6f90592ee71c5572dd46

Contents?: true

Size: 993 Bytes

Versions: 5

Compression:

Stored size: 993 Bytes

Contents

# encoding: utf-8

module RuboCop
  module Cop
    module Style
      # This cop checks for inheritance from Struct.new.
      #
      # @example
      #   # bad
      #   class Person < Struct.new(:first_name, :last_name)
      #   end
      #
      #   # good
      #   Person = Struct.new(:first_name, :last_name)
      class StructInheritance < Cop
        MSG = "Don't extend an instance initialized by `Struct.new`."

        def on_class(node)
          _name, superclass, _body = *node
          return unless struct_constructor?(superclass)

          add_offense(node, superclass.loc.expression, MSG)
        end

        private

        def struct_constructor?(node)
          if node && node.send_type?
            receiver, method_name = *node

            receiver &&
              receiver.const_type? &&
              receiver.children.last == :Struct &&
              method_name == :new
          else
            false
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rubocop-0.31.0 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-0.30.1 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-0.30.0 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-0.29.1 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-0.29.0 lib/rubocop/cop/style/struct_inheritance.rb