Sha256: 115a2246d34052907d69007e6067a56f7446aee66175206fcce86b7e64d90ede

Contents?: true

Size: 1000 Bytes

Versions: 40

Compression:

Stored size: 1000 Bytes

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for inheritance from Struct.new.
      #
      # @example
      #   # bad
      #   class Person < Struct.new(:first_name, :last_name)
      #     def age
      #       42
      #     end
      #   end
      #
      #   # good
      #   Person = Struct.new(:first_name, :last_name) do
      #     def age
      #       42
      #     end
      #   end
      class StructInheritance < Cop
        MSG = "Don't extend an instance initialized by `Struct.new`. " \
              'Use a block to customize the struct.'

        def on_class(node)
          return unless struct_constructor?(node.parent_class)

          add_offense(node, location: node.parent_class.source_range)
        end

        def_node_matcher :struct_constructor?, <<~PATTERN
          {(send (const nil? :Struct) :new ...)
           (block (send (const nil? :Struct) :new ...) ...)}
        PATTERN
      end
    end
  end
end

Version data entries

40 entries across 29 versions & 5 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/style/struct_inheritance.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/style/struct_inheritance.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-0.85.1 lib/rubocop/cop/style/struct_inheritance.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-0.85.0 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-0.84.0 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-0.83.0 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-0.82.0 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-0.81.0 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-0.80.1 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-0.80.0 lib/rubocop/cop/style/struct_inheritance.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/style/struct_inheritance.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/rubocop-0.79.0/lib/rubocop/cop/style/struct_inheritance.rb
rubocop-0.79.0 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-0.78.0 lib/rubocop/cop/style/struct_inheritance.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.77.0/lib/rubocop/cop/style/struct_inheritance.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/style/struct_inheritance.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/cop/style/struct_inheritance.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.75.1/lib/rubocop/cop/style/struct_inheritance.rb