Sha256: bc3147c4c9c55650403b298d906ed08b005b97d90d6ee63d769bc917820f63ec

Contents?: true

Size: 1.56 KB

Versions: 32

Compression:

Stored size: 1.56 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 < Base
        include RangeHelp
        extend AutoCorrector

        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.parent_class.source_range) do |corrector|
            corrector.remove(range_with_surrounding_space(range: node.loc.keyword, newlines: false))
            corrector.replace(node.loc.operator, '=')

            correct_parent(node.parent_class, corrector)
          end
        end

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

        private

        def correct_parent(parent, corrector)
          if parent.block_type?
            corrector.remove(range_with_surrounding_space(range: parent.loc.end, newlines: false))
          else
            corrector.insert_after(parent.loc.expression, ' do')
          end
        end
      end
    end
  end
end

Version data entries

32 entries across 32 versions & 3 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/struct_inheritance.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/struct_inheritance.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/struct_inheritance.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/struct_inheritance.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/struct_inheritance.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/struct_inheritance.rb
rubocop-1.10.0 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-1.9.1 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-1.9.0 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-1.8.1 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-1.8.0 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-1.7.0 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-1.6.1 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-1.6.0 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-1.5.2 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-1.5.1 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-1.5.0 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-1.4.2 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-1.4.1 lib/rubocop/cop/style/struct_inheritance.rb
rubocop-1.4.0 lib/rubocop/cop/style/struct_inheritance.rb