Sha256: 4af9396159fe90a81f6cb41b5a041b8b809c617c7d13f4dad217dee422a561ce

Contents?: true

Size: 1.02 KB

Versions: 3

Compression:

Stored size: 1.02 KB

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Style
      # This cop checks for single-line method definitions.
      # It can optionally accept single-line methods with no body.
      class SingleLineMethods < Cop
        MSG = 'Avoid single-line method definitions.'

        def allow_empty?
          SingleLineMethods.config['AllowIfMethodIsEmpty']
        end

        def on_def(node)
          check(node)

          super
        end

        def on_defs(node)
          check(node)

          super
        end

        private

        def check(node)
          start_line = node.loc.keyword.line
          end_line = node.loc.end.line

          if node.type == :def
            empty_body = node.children[2].nil?
          else
            empty_body = node.children[3].nil?
          end

          if start_line == end_line && !(allow_empty? && empty_body)
            add_offence(:convention,
                        node.loc.expression,
                        MSG)
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
rubocop-0.9.1 lib/rubocop/cop/style/single_line_methods.rb
sabat-rubocop-0.9.0 lib/rubocop/cop/style/single_line_methods.rb
rubocop-0.9.0 lib/rubocop/cop/style/single_line_methods.rb