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)
为什么我原来的匹配不起作用?
我的版本:
不久前,我们将匹配器更改nil为nil?。这有一个不幸的副作用,您无法再复制粘贴输出ruby-parse或node.to_s立即拥有一个可用的匹配器。
只需添加额外的问号就可以让您的模式再次发挥作用:
def_node_matcher :is_foo_bar?, <<-PATTERN
(send (const nil? :Foo) :bar)
PATTERN
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
490 次 |
| 最近记录: |