Sha256: 367a112b9b05400315039ec475b5281b3de9817e7c13688d244f4f7eed030816

Contents?: true

Size: 1.17 KB

Versions: 7

Compression:

Stored size: 1.17 KB

Contents

# encoding: utf-8

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.'

        JAVA_TYPES = [:byte, :boolean, :byte, :short, :char,
                      :int, :long, :float, :double]

        JAVA_TYPE_NODES =
          JAVA_TYPES.map { |t| s(:send, s(:const, nil, :Java), t) }

        def on_send(node)
          # ignore Java interop code like Java::int
          return if JAVA_TYPE_NODES.include?(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)
          @corrections << lambda do |corrector|
            corrector.replace(node.loc.dot, '.')
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rubocop-0.30.1 lib/rubocop/cop/style/colon_method_call.rb
rubocop-0.30.0 lib/rubocop/cop/style/colon_method_call.rb
rubocop-0.29.1 lib/rubocop/cop/style/colon_method_call.rb
rubocop-0.29.0 lib/rubocop/cop/style/colon_method_call.rb
rubocop-0.28.0 lib/rubocop/cop/style/colon_method_call.rb
rubocop-0.27.1 lib/rubocop/cop/style/colon_method_call.rb
rubocop-0.27.0 lib/rubocop/cop/style/colon_method_call.rb