Fix toplevel_stanzas.

This commit is contained in:
Markus Reiter 2023-03-21 14:43:39 +01:00
parent 8c6f31a7ac
commit 3c89f99df1
No known key found for this signature in database
GPG Key ID: 245293B51702655B
3 changed files with 11 additions and 5 deletions

View File

@ -36,7 +36,15 @@ module RuboCop
end end
def toplevel_stanzas def toplevel_stanzas
@toplevel_stanzas ||= stanzas.select(&:toplevel_stanza?) # If a `cask` block only contains one stanza, it is that stanza's direct parent,
# otherwise stanzas are grouped in a block and `cask` is that block's parent.
is_toplevel_stanza = if cask_body.begin_block?
->(stanza) { stanza.parent_node.parent.cask_block? }
else
->(stanza) { stanza.parent_node.cask_block? }
end
@toplevel_stanzas ||= stanzas.select(&is_toplevel_stanza)
end end
def sorted_toplevel_stanzas def sorted_toplevel_stanzas

View File

@ -52,10 +52,6 @@ module RuboCop
stanza_group == other.stanza_group stanza_group == other.stanza_group
end end
def toplevel_stanza?
parent_node.cask_block? || parent_node.parent.cask_block?
end
def ==(other) def ==(other)
self.class == other.class && stanza_node == other.stanza_node self.class == other.class && stanza_node == other.stanza_node
end end

View File

@ -17,6 +17,8 @@ module RuboCop
def_node_matcher :cask_block?, "(block (send nil? :cask _) args ...)" def_node_matcher :cask_block?, "(block (send nil? :cask _) args ...)"
def_node_matcher :arch_variable?, "(lvasgn _ (send nil? :on_arch_conditional ...))" def_node_matcher :arch_variable?, "(lvasgn _ (send nil? :on_arch_conditional ...))"
def_node_matcher :begin_block?, "(begin ...)"
def stanza? def stanza?
return true if arch_variable? return true if arch_variable?