Sha256: bc1e4d0fc74735668dd818f90fe4a80cd4526042a8eba993ec38d79f169dbb98

Contents?: true

Size: 1.28 KB

Versions: 36

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

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.
      #
      # @example
      #
      #   # bad
      #   def foo()
      #     # does a thing
      #   end
      #
      #   # good
      #   def foo
      #     # does a thing
      #   end
      #
      #   # also good
      #   def foo() does_a_thing end
      #
      # @example
      #
      #   # bad
      #   def Baz.foo()
      #     # does a thing
      #   end
      #
      #   # good
      #   def Baz.foo
      #     # does a thing
      #   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?
          return unless !node.arguments? && (node_arguments_loc_begin = node.arguments.loc.begin)

          add_offense(node_arguments_loc_begin) do |corrector|
            corrector.remove(node_arguments_loc_begin)
            corrector.remove(node.arguments.loc.end)
          end
        end
        alias on_defs on_def
      end
    end
  end
end

Version data entries

36 entries across 36 versions & 3 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/def_with_parentheses.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/def_with_parentheses.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/def_with_parentheses.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/def_with_parentheses.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/def_with_parentheses.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.12.1 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.12.0 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.11.0 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.10.0 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.9.1 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.9.0 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.8.1 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.8.0 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.7.0 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.6.1 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.6.0 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.5.2 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.5.1 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.5.0 lib/rubocop/cop/style/def_with_parentheses.rb