Sha256: 02e112c61f3e646995ca2918b5986a2b67e1bc10d97b860283c3502c8acd50d8

Contents?: true

Size: 1.31 KB

Versions: 9

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Checks for parentheses in the definition of a method,
      # that does not take any arguments. Both instance and
      # class/singleton methods are checked.
      #
      # @example
      #
      #   # bad
      #   def foo()
      #     do_something
      #   end
      #
      #   # good
      #   def foo
      #     do_something
      #   end
      #
      #   # bad
      #   def foo() = do_something
      #
      #   # good
      #   def foo = do_something
      #
      #   # good (without parentheses it's a syntax error)
      #   def foo() do_something end
      #
      #   # bad
      #   def Baz.foo()
      #     do_something
      #   end
      #
      #   # good
      #   def Baz.foo
      #     do_something
      #   end
      class DefWithParentheses < Base
        extend AutoCorrector

        MSG = "Omit the parentheses in defs when the method doesn't accept any arguments."

        def on_def(node)
          return if node.single_line? && !node.endless?
          return unless !node.arguments? && (node_arguments = node.arguments.source_range)

          add_offense(node_arguments) do |corrector|
            corrector.remove(node_arguments)
          end
        end
        alias on_defs on_def
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rubocop-1.70.0 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.69.2 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.69.1 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.69.0 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.68.0 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.67.0 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.66.1 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.66.0 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.65.1 lib/rubocop/cop/style/def_with_parentheses.rb