Sha256: 46b690b4180ae343aafc122378d8c3c23ac5418cb284c7edd363db18e056c2d9

Contents?: true

Size: 1.26 KB

Versions: 27

Compression:

Stored size: 1.26 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

27 entries across 27 versions & 4 rubygems

Version Path
rubocop-1.25.1 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.25.0 lib/rubocop/cop/style/def_with_parentheses.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.24.1 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.24.0 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.23.0 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.22.3 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.22.2 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.22.1 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.22.0 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.21.0 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.20.0 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.19.1 lib/rubocop/cop/style/def_with_parentheses.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/rubocop-1.18.3/lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.19.0 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.18.4 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.18.3 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.18.2 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.18.1 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.18.0 lib/rubocop/cop/style/def_with_parentheses.rb