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

Inherits:
Object
  • Object
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 collapse

Instance Method Summary collapse

Constructor Details

#initialize(block_node, comments) ⇒ CaskBlock

Returns a new instance of CaskBlock.



15
16
17
18
# File 'rubocops/cask/ast/cask_block.rb', line 15

def initialize(block_node, comments)
  @block_node = block_node
  @comments = comments
end

Instance Attribute Details

#block_nodeObject (readonly) Also known as: cask_node

Returns the value of attribute block_node.



20
21
22
# File 'rubocops/cask/ast/cask_block.rb', line 20

def block_node
  @block_node
end

#commentsObject (readonly)

Returns the value of attribute comments.



20
21
22
# File 'rubocops/cask/ast/cask_block.rb', line 20

def comments
  @comments
end

Instance Method Details

#headerObject



26
27
28
# File 'rubocops/cask/ast/cask_block.rb', line 26

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

#sorted_toplevel_stanzasObject



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

def sorted_toplevel_stanzas
  @sorted_toplevel_stanzas ||= sort_stanzas(toplevel_stanzas)
end

#stanzasObject



30
31
32
33
34
35
36
# File 'rubocops/cask/ast/cask_block.rb', line 30

def stanzas
  return [] unless cask_body

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

#toplevel_stanzasObject



38
39
40
41
42
43
44
45
46
47
48
# File 'rubocops/cask/ast/cask_block.rb', line 38

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