Class: RuboCop::Cask::AST::CaskBlock

Inherits:
StanzaBlock show all
Extended by:
Forwardable
Defined in:
rubocops/cask/ast/cask_block.rb

Overview

This class wraps the AST block node that represents the entire cask definition. It includes various helper methods to aid cops in their analysis.

Instance Attribute Summary

Attributes inherited from StanzaBlock

#block_node, #comments

Instance Method Summary collapse

Methods inherited from StanzaBlock

#initialize

Constructor Details

This class inherits a constructor from RuboCop::Cask::AST::StanzaBlock

Instance Method Details

#cask_nodeObject



49
50
51
# File 'rubocops/cask/ast/cask_block.rb', line 49

def cask_node
  block_node
end

#headerObject



55
56
57
# File 'rubocops/cask/ast/cask_block.rb', line 55

def header
  @header ||= CaskHeader.new(block_node.method_node)
end

#stanzasObject



60
61
62
63
64
65
66
# File 'rubocops/cask/ast/cask_block.rb', line 60

def stanzas
  return [] unless cask_body

  @stanzas ||= cask_body.each_node
                        .select(&:stanza?)
                        .map { |node| Stanza.new(node, comments) }
end

#toplevel_stanzasObject



68
69
70
71
72
73
74
75
76
77
78
# File 'rubocops/cask/ast/cask_block.rb', line 68

def toplevel_stanzas
  # 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