Sha256: cf4e053e499cd08e5d03e9b062f6cb7eaa8376cd2d5dff43ee7d657cacb89c5d

Contents?: true

Size: 1.97 KB

Versions: 164

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Newcomers to ruby applications may write top-level methods,
      # when ideally they should be organized in appropriate classes or modules.
      # This cop looks for definitions of top-level methods and warns about them.
      #
      # However for ruby scripts it is perfectly fine to use top-level methods.
      # Hence this cop is disabled by default.
      #
      # @example
      #   # bad
      #   def some_method
      #   end
      #
      #   # bad
      #   def self.some_method
      #   end
      #
      #   # bad
      #   define_method(:foo) { puts 1 }
      #
      #   # good
      #   module Foo
      #     def some_method
      #     end
      #   end
      #
      #   # good
      #   class Foo
      #     def self.some_method
      #     end
      #   end
      #
      #   # good
      #   Struct.new do
      #     def some_method
      #     end
      #   end
      #
      #   # good
      #   class Foo
      #     define_method(:foo) { puts 1 }
      #   end
      class TopLevelMethodDefinition < Base
        MSG = 'Do not define methods at the top-level.'

        RESTRICT_ON_SEND = %i[define_method].freeze

        def on_def(node)
          return unless top_level_method_definition?(node)

          add_offense(node)
        end
        alias on_defs on_def
        alias on_send on_def

        def on_block(node)
          return unless define_method_block?(node) && top_level_method_definition?(node)

          add_offense(node)
        end

        alias on_numblock on_block

        private

        def top_level_method_definition?(node)
          if node.parent&.begin_type?
            node.parent.root?
          else
            node.root?
          end
        end

        # @!method define_method_block?(node)
        def_node_matcher :define_method_block?, <<~PATTERN
          ({block numblock} (send _ {:define_method} _) ...)
        PATTERN
      end
    end
  end
end

Version data entries

164 entries across 159 versions & 15 rubygems

Version Path
harbr-0.1.93 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/top_level_method_definition.rb
harbr-0.1.91 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/top_level_method_definition.rb
harbr-0.1.90 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/top_level_method_definition.rb
harbr-0.1.89 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/top_level_method_definition.rb
harbr-0.1.88 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/top_level_method_definition.rb
harbr-0.1.87 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/top_level_method_definition.rb
harbr-0.1.86 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/top_level_method_definition.rb
harbr-0.1.85 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/top_level_method_definition.rb
harbr-0.1.84 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/top_level_method_definition.rb
harbr-0.1.83 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/top_level_method_definition.rb
harbr-0.1.82 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/top_level_method_definition.rb
harbr-0.1.81 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/top_level_method_definition.rb
harbr-0.1.80 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/top_level_method_definition.rb
harbr-0.1.79 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/top_level_method_definition.rb
harbr-0.1.78 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/top_level_method_definition.rb
harbr-0.1.77 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/top_level_method_definition.rb
harbr-0.1.76 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/top_level_method_definition.rb
harbr-0.1.75 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/top_level_method_definition.rb
harbr-0.1.74 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/top_level_method_definition.rb
harbr-0.1.73 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/top_level_method_definition.rb