Sha256: 4c51b796946bb71eb0bfbf8fb8775790f7c039dd07906df11b5cf0a4038934ac

Contents?: true

Size: 1.12 KB

Versions: 10

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for methods invoked via the :: operator instead
      # of the . operator (like FileUtils::rmdir instead of FileUtils.rmdir).
      class ColonMethodCall < Cop
        MSG = 'Do not use `::` for method calls.'.freeze

        def_node_matcher :java_type_node?, <<-PATTERN
          (send
            (const nil :Java)
            {:boolean :byte :char :double :float :int :long :short})
        PATTERN

        def on_send(node)
          # ignore Java interop code like Java::int
          return if java_type_node?(node)

          receiver, method_name, *_args = *node

          # discard methods with nil receivers and op methods(like [])
          return unless receiver && node.loc.dot && node.loc.dot.is?('::')
          return if allowed_name(method_name.to_s)

          add_offense(node, :dot)
        end

        def allowed_name(method_name)
          method_name.match(/^[A-Z]/)
        end

        def autocorrect(node)
          ->(corrector) { corrector.replace(node.loc.dot, '.') }
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/colon_method_call.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/colon_method_call.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/colon_method_call.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/colon_method_call.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/colon_method_call.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/colon_method_call.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/colon_method_call.rb
rubocop-0.47.1 lib/rubocop/cop/style/colon_method_call.rb
rubocop-0.47.0 lib/rubocop/cop/style/colon_method_call.rb
rubocop-0.46.0 lib/rubocop/cop/style/colon_method_call.rb