Merge pull request #15028 from reitermarkus/toplevel-stanzas

Fix `toplevel_stanzas`.
This commit is contained in:
Issy Long 2023-03-21 22:42:12 +00:00 committed by GitHub
commit a8b1e2c18d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 5 deletions

View File

@ -36,7 +36,15 @@ module RuboCop
end
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
def sorted_toplevel_stanzas

View File

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

View File

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