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
Some time back, we changed the matcher for
nil
to benil?
. This has the unfortunate side-effect that you can no longer copy-paste output fromruby-parse
ornode.to_s
and instantly have a working matcher.Just adding the additional question mark should make your pattern work again: