Sha256: 9ada09cd98e083a3a6fb5e959bb4aed2c12c60b15e6f1f2255fb0ccc71a47ee6

Contents?: true

Size: 1.77 KB

Versions: 3

Compression:

Stored size: 1.77 KB

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Style
      # This cop checks for parentheses in the definition of a method,
      # that does not take any arguments. Both instance and
      # class/singleton methods are checked.
      class DefWithParentheses < Cop
        MSG = "Omit the parentheses in defs when the method doesn't accept " +
            'any arguments.'

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

          return if start_line == end_line

          _, args = *node
          if args.children == [] && args.loc.begin
            add_offence(:convention, args.loc.begin, MSG)
          end

          super
        end

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

          return if start_line == end_line

          _, _, args = *node
          if args.children == [] && args.loc.begin
            add_offence(:convention, args.loc.begin, MSG)
          end

          super
        end
      end

      # This cop checks for missing parentheses in the definition of a
      # method, that takes arguments. Both instance and
      # class/singleton methods are checked.
      class DefWithoutParentheses < Cop
        MSG = 'Use def with parentheses when there are arguments.'

        def on_def(node)
          _, args = *node

          if args.children.size > 0 && args.loc.begin.nil?
            add_offence(:convention, args.loc.expression, MSG)
          end

          super
        end

        def on_defs(node)
          _, _, args = *node

          if args.children.size > 0 && args.loc.begin.nil?
            add_offence(:convention, args.loc.expression, MSG)
          end

          super
        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/def_parentheses.rb
sabat-rubocop-0.9.0 lib/rubocop/cop/style/def_parentheses.rb
rubocop-0.9.0 lib/rubocop/cop/style/def_parentheses.rb