Sha256: 4e9df5dee9ef5d2b0d2a0cedcde1c8899a316b3167922762af34756868e1db04

Contents?: true

Size: 932 Bytes

Versions: 5

Compression:

Stored size: 932 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?
          cop_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)
            convention(node, :expression)
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rubocop-0.15.0 lib/rubocop/cop/style/single_line_methods.rb
rubocop-0.14.1 lib/rubocop/cop/style/single_line_methods.rb
rubocop-0.14.0 lib/rubocop/cop/style/single_line_methods.rb
rubocop-0.13.1 lib/rubocop/cop/style/single_line_methods.rb
rubocop-0.13.0 lib/rubocop/cop/style/single_line_methods.rb