Sha256: b8ced91d92edbfd0bb9f0f646ac23ba34e60fa41b6a7bc7a6e58419947ddaa5f

Contents?: true

Size: 1015 Bytes

Versions: 4

Compression:

Stored size: 1015 Bytes

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)
        end

        def on_defs(node)
          check(node)
        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

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-0.12.0 lib/rubocop/cop/style/single_line_methods.rb
rubocop-0.11.1 lib/rubocop/cop/style/single_line_methods.rb
rubocop-0.11.0 lib/rubocop/cop/style/single_line_methods.rb
rubocop-0.10.0 lib/rubocop/cop/style/single_line_methods.rb