class RuboCop::Cask::AST::CaskBlock

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

Attributes

block_node[R]
cask_node[R]
comments[R]

Public Class Methods

new(block_node, comments) click to toggle source
# File lib/rubocop/cask/ast/cask_block.rb, line 12
def initialize(block_node, comments)
  @block_node = block_node
  @comments = comments
end

Public Instance Methods

header() click to toggle source
# File lib/rubocop/cask/ast/cask_block.rb, line 23
def header
  @header ||= CaskHeader.new(cask_node.method_node)
end
sorted_toplevel_stanzas() click to toggle source
# File lib/rubocop/cask/ast/cask_block.rb, line 39
def sorted_toplevel_stanzas
  @sorted_toplevel_stanzas ||= sort_stanzas(toplevel_stanzas)
end
stanzas() click to toggle source
# File lib/rubocop/cask/ast/cask_block.rb, line 27
def stanzas
  return [] unless cask_body

  @stanzas ||= cask_body.each_node
    .select(&:stanza?)
    .map { |node| Stanza.new(node, stanza_comments(node)) }
end
toplevel_stanzas() click to toggle source
# File lib/rubocop/cask/ast/cask_block.rb, line 35
def toplevel_stanzas
  @toplevel_stanzas ||= stanzas.select(&:toplevel_stanza?)
end

Private Instance Methods

comments_hash() click to toggle source
# File lib/rubocop/cask/ast/cask_block.rb, line 67
def comments_hash
  @comments_hash ||= Parser::Source::Comment
    .associate_locations(cask_node, comments)
end
sort_stanzas(stanzas) click to toggle source
# File lib/rubocop/cask/ast/cask_block.rb, line 45
def sort_stanzas(stanzas)
  stanzas.sort do |s1, s2|
    i1 = stanza_order_index(s1)
    i2 = stanza_order_index(s2)
    if i1 == i2
      i1 = stanzas.index(s1)
      i2 = stanzas.index(s2)
    end
    i1 - i2
  end
end
stanza_comments(stanza_node) click to toggle source
# File lib/rubocop/cask/ast/cask_block.rb, line 61
def stanza_comments(stanza_node)
  stanza_node.each_node.reduce([]) do |comments, node|
    comments | comments_hash[node.loc]
  end
end
stanza_order_index(stanza) click to toggle source
# File lib/rubocop/cask/ast/cask_block.rb, line 57
def stanza_order_index(stanza)
  Constants::STANZA_ORDER.index(stanza.stanza_name)
end