Sha256: 9b112ba836d3dd150e14d984f7457ae85318efb4ed8707c73354f7e3a48c3a2f

Contents?: true

Size: 1.92 KB

Versions: 42

Compression:

Stored size: 1.92 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

        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 (send _ {:define_method} _) ...)
        PATTERN
      end
    end
  end
end

Version data entries

42 entries across 42 versions & 5 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/top_level_method_definition.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/top_level_method_definition.rb
rubocop-1.34.1 lib/rubocop/cop/style/top_level_method_definition.rb
rubocop-1.34.0 lib/rubocop/cop/style/top_level_method_definition.rb
rubocop-1.33.0 lib/rubocop/cop/style/top_level_method_definition.rb
rubocop-1.32.0 lib/rubocop/cop/style/top_level_method_definition.rb
rubocop-1.31.1 lib/rubocop/cop/style/top_level_method_definition.rb
rubocop-1.31.0 lib/rubocop/cop/style/top_level_method_definition.rb
rubocop-1.30.1 lib/rubocop/cop/style/top_level_method_definition.rb
rubocop-1.30.0 lib/rubocop/cop/style/top_level_method_definition.rb
rubocop-1.29.1 lib/rubocop/cop/style/top_level_method_definition.rb
rubocop-1.29.0 lib/rubocop/cop/style/top_level_method_definition.rb
rubocop-1.28.2 lib/rubocop/cop/style/top_level_method_definition.rb
rubocop-1.28.1 lib/rubocop/cop/style/top_level_method_definition.rb
rubocop-1.28.0 lib/rubocop/cop/style/top_level_method_definition.rb
rubocop-1.27.0 lib/rubocop/cop/style/top_level_method_definition.rb
rubocop-1.26.1 lib/rubocop/cop/style/top_level_method_definition.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/top_level_method_definition.rb
rubocop-1.26.0 lib/rubocop/cop/style/top_level_method_definition.rb
rubocop-1.25.1 lib/rubocop/cop/style/top_level_method_definition.rb