Sha256: 1946c990d0f80207fd554361d39e6fb39df6a2e94f111c24582bd8901bd69aae

Contents?: true

Size: 846 Bytes

Versions: 4

Compression:

Stored size: 846 Bytes

Contents

# encoding: utf-8

module Rubocop
  module Cop
    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].type == :nil
        else
          empty_body = node.children[3].type == :nil
        end

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-0.8.3 lib/rubocop/cop/single_line_methods.rb
rubocop-0.8.2 lib/rubocop/cop/single_line_methods.rb
rubocop-0.8.1 lib/rubocop/cop/single_line_methods.rb
rubocop-0.8.0 lib/rubocop/cop/single_line_methods.rb