Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix toplevel_stanzas. #15028

Merged
merged 1 commit into from Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion Library/Homebrew/rubocops/cask/ast/cask_block.rb
Expand Up @@ -36,7 +36,15 @@ def stanzas
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
Expand Down
4 changes: 0 additions & 4 deletions Library/Homebrew/rubocops/cask/ast/stanza.rb
Expand Up @@ -52,10 +52,6 @@ def same_group?(other)
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
Expand Down
2 changes: 2 additions & 0 deletions Library/Homebrew/rubocops/cask/extend/node.rb
Expand Up @@ -17,6 +17,8 @@ class Node
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?

Expand Down