Class: Rubocop::Cop::OpMethod

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

Constant Summary collapse

MSG =
'When defining the %s operator, name its argument other.'
BLACKLISTED =
[:+@, :-@, :[], :[]=, :<<]
TARGET_ARGS =
s(:args, s(:arg, :other))

Instance Attribute Summary

Attributes inherited from Cop

#debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, cop_name, #has_report?, inherited, #initialize, #inspect, #name, #on_comment

Constructor Details

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

Instance Method Details

#on_def(node) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rubocop/cop/op_method.rb', line 12

def on_def(node)
  name, args, _body = *node

  if name !~ /\A\w/ && !BLACKLISTED.include?(name) &&
      args.children.size == 1 && args != TARGET_ARGS
    add_offence(:convention,
                node.loc.line,
                sprintf(MSG, name))
  end

  super
end