为什么此发送模式不匹配?

Rem*_*emo 1 ruby-parser rubocop

我写了一个自定义的 rubocop cop,摘录如下:

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
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用以下代码:

Foo.bar
Run Code Online (Sandbox Code Playgroud)

有趣的是,node.to_s下面的内容与我的模式完全匹配:

"(send
  (const nil :Foo) :bar)"
Run Code Online (Sandbox Code Playgroud)

但节点不匹配。如果我将模式更改为以下内容,它仍然有效:

(send (...) :bar)
Run Code Online (Sandbox Code Playgroud)

为什么我原来的匹配不起作用?

我的版本:

  • 红宝石2.6.2
  • AST 2.4.0
  • 鲁博科普 0.77.0
  • 解析器2.6.4.1

Dre*_*nmi 5

不久前,我们将匹配器更改nilnil?。这有一个不幸的副作用,您无法再复制粘贴输出ruby-parsenode.to_s立即拥有一个可用的匹配器。

只需添加额外的问号就可以让您的模式再次发挥作用:

def_node_matcher :is_foo_bar?, <<-PATTERN
  (send (const nil? :Foo) :bar)
PATTERN
Run Code Online (Sandbox Code Playgroud)