Class: RuboCop::Cask::AST::Stanza

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
rubocops/cask/ast/stanza.rb

Overview

This class wraps the AST send/block node that encapsulates the method call that comprises the stanza. It includes various helper methods to aid cops in their analysis.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method_node, all_comments) ⇒ Stanza

Returns a new instance of Stanza.



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

def initialize(method_node, all_comments)
  @method_node = method_node
  @all_comments = all_comments
end

Instance Attribute Details

#all_commentsObject (readonly)

Returns the value of attribute all_comments.



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

def all_comments
  @all_comments
end

#method_nodeObject (readonly) Also known as: stanza_node

Returns the value of attribute method_node.



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

def method_node
  @method_node
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



70
71
72
# File 'rubocops/cask/ast/stanza.rb', line 70

def ==(other)
  self.class == other.class && stanza_node == other.stanza_node
end

#commentsObject



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

def comments
  @comments ||= stanza_node.each_node.reduce([]) do |comments, node|
    comments | comments_hash[node.loc]
  end
end

#comments_hashObject



66
67
68
# File 'rubocops/cask/ast/stanza.rb', line 66

def comments_hash
  @comments_hash ||= Parser::Source::Comment.associate_locations(stanza_node.parent, all_comments)
end

#same_group?(other) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'rubocops/cask/ast/stanza.rb', line 56

def same_group?(other)
  stanza_group == other.stanza_group
end

#source_rangeObject



28
29
30
# File 'rubocops/cask/ast/stanza.rb', line 28

def source_range
  stanza_node.location_expression
end

#source_range_with_commentsObject



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

def source_range_with_comments
  comments.reduce(source_range) do |range, comment|
    range.join(comment.loc.expression)
  end
end

#stanza_groupObject



48
49
50
# File 'rubocops/cask/ast/stanza.rb', line 48

def stanza_group
  Constants::STANZA_GROUP_HASH[stanza_name]
end

#stanza_indexObject



52
53
54
# File 'rubocops/cask/ast/stanza.rb', line 52

def stanza_index
  Constants::STANZA_ORDER.index(stanza_name)
end

#stanza_nameObject



42
43
44
45
46
# File 'rubocops/cask/ast/stanza.rb', line 42

def stanza_name
  return :on_arch_conditional if arch_variable?

  stanza_node.method_name
end