Why doen't this send pattern match?

515 Views Asked by At

I have written a custom rubocop cop, the excerpts are like this:

def_node_matcher :is_foo_bar?, <<-PATTERN
  (send (const nil :Foo) :bar)
PATTERN

def on_send(node)
  puts "Match" if is_foo_bar?(node)
end

I'm trying the cop on the following code:

Foo.bar

Interestingly enough, node.to_s says the following, which exactly matches my pattern:

"(send
  (const nil :Foo) :bar)"

But the node does not match. If I change the pattern to the following, it works though:

(send (...) :bar)

Why doesn't my original match work?

My versions:

  • ruby 2.6.2
  • ast 2.4.0
  • rubocop 0.77.0
  • parser 2.6.4.1
1

There are 1 best solutions below

0
On

Some time back, we changed the matcher for nil to be nil?. This has the unfortunate side-effect that you can no longer copy-paste output from ruby-parse or node.to_s and instantly have a working matcher.

Just adding the additional question mark should make your pattern work again:

def_node_matcher :is_foo_bar?, <<-PATTERN
  (send (const nil? :Foo) :bar)
PATTERN