Class: Rubocop::Cop::Style::ColonMethodCall

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/style/colon_method_call.rb

Overview

This cop checks for methods invoked via the :: operator instead of the . operator (like FileUtils::rmdir instead of FileUtils.rmdir).

Constant Summary collapse

MSG =
'Do not use :: for method calls.'

Constants inherited from Cop

Cop::OPERATOR_METHODS

Instance Attribute Summary

Attributes inherited from Cop

#config, #corrections, #offences, #processed_source

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, all, #autocorrect?, #convention, #cop_config, cop_name, #cop_name, cop_type, #debug?, #ignore_node, inherited, #initialize, lint?, #message, non_rails, rails?, style?, #support_autocorrect?, #warning

Constructor Details

This class inherits a constructor from Rubocop::Cop::Cop

Instance Method Details

#allowed_name(method_name) ⇒ Object



21
22
23
# File 'lib/rubocop/cop/style/colon_method_call.rb', line 21

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

#autocorrect(node) ⇒ Object



25
26
27
28
29
# File 'lib/rubocop/cop/style/colon_method_call.rb', line 25

def autocorrect(node)
  @corrections << lambda do |corrector|
    corrector.replace(node.loc.dot, '.')
  end
end

#on_send(node) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/rubocop/cop/style/colon_method_call.rb', line 11

def on_send(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)

  convention(node, :dot)
end