Sha256: 21f820a553087cf92256bb9fc5d63886c6d7d18a43818c700ce64f39c02b0523

Contents?: true

Size: 1.34 KB

Versions: 11

Compression:

Stored size: 1.34 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()
      #     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
      #
      # @example
      #
      #   # 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

11 entries across 11 versions & 3 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/def_with_parentheses.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.29.1 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.29.0 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.28.2 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.28.1 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.28.0 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.27.0 lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.26.1 lib/rubocop/cop/style/def_with_parentheses.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/def_with_parentheses.rb
rubocop-1.26.0 lib/rubocop/cop/style/def_with_parentheses.rb