Sha256: e40624e3d89960f6a00d60c900b507388e24412ddd64428a797eb6a364e1d231

Contents?: true

Size: 1.04 KB

Versions: 8

Compression:

Stored size: 1.04 KB

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)
          return false unless node

          send_node = node.block_type? ? node.children.first : node
          return false unless send_node.send_type?

          receiver, method_name = *send_node

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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rubocop-0.35.1 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-0.35.0 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-0.34.2 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-0.34.1 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-0.34.0 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-0.33.0 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-0.32.1 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-0.32.0 lib/rubocop/cop/style/struct_inheritance.rb