Skip to content

Commit

Permalink
Modify Style/OpMethod to use NodePattern
Browse files Browse the repository at this point in the history
  • Loading branch information
rrosenblum authored and bbatsov committed Nov 15, 2016
1 parent 8e54dc4 commit faaadc5
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions lib/rubocop/cop/style/op_method.rb
Expand Up @@ -18,19 +18,17 @@ class OpMethod < Cop
'name its argument `other`.'.freeze

OP_LIKE_METHODS = [:eql?, :equal?].freeze

BLACKLISTED = [:+@, :-@, :[], :[]=, :<<, :`].freeze

TARGET_ARGS = [s(:args, s(:arg, :other)),
s(:args, s(:arg, :_other))].freeze
def_node_matcher :op_method_candidate?, <<-PATTERN
(def $_ (args $(arg [!:other !:_other])) _)
PATTERN

def on_def(node)
name, args, _body = *node
return unless op_method?(name) &&
args.children.one? &&
!TARGET_ARGS.include?(args)

add_offense(args.children.first, :expression, format(MSG, name))
op_method_candidate?(node) do |name, arg|
return unless op_method?(name)
add_offense(arg, :expression, format(MSG, name))
end
end

def op_method?(name)
Expand Down

0 comments on commit faaadc5

Please sign in to comment.