diff --git a/Library/Homebrew/rubocops/cask/ast/cask_block.rb b/Library/Homebrew/rubocops/cask/ast/cask_block.rb index 4f45f6771c..ac0dca7592 100644 --- a/Library/Homebrew/rubocops/cask/ast/cask_block.rb +++ b/Library/Homebrew/rubocops/cask/ast/cask_block.rb @@ -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 diff --git a/Library/Homebrew/rubocops/cask/ast/stanza.rb b/Library/Homebrew/rubocops/cask/ast/stanza.rb index 2088a43655..88badac8cd 100644 --- a/Library/Homebrew/rubocops/cask/ast/stanza.rb +++ b/Library/Homebrew/rubocops/cask/ast/stanza.rb @@ -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 diff --git a/Library/Homebrew/rubocops/cask/extend/node.rb b/Library/Homebrew/rubocops/cask/extend/node.rb index 20e1e4b7cc..ec79aa4f3f 100644 --- a/Library/Homebrew/rubocops/cask/extend/node.rb +++ b/Library/Homebrew/rubocops/cask/extend/node.rb @@ -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?