Sha256: 01864f36430c52a3f5fd902348d0e8f535a477a2c4cc0bf242c56bd9a61fa11d

Contents?: true

Size: 1.01 KB

Versions: 3

Compression:

Stored size: 1.01 KB

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.'.freeze

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

          add_offense(node, location: superclass.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

3 entries across 3 versions & 1 rubygems

Version Path
rubocop-0.67.2 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-0.67.1 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-0.67.0 lib/rubocop/cop/style/struct_inheritance.rb