Class: SyntaxTree::ArrayLiteral

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

ArrayLiteral represents an array literal, which can optionally contain elements.

[]
[one, two, three]

Defined Under Namespace

Classes: QSymbolsFormatter, QWordsFormatter, VarRefsFormatter

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#pretty_print, #to_json

Constructor Details

#initialize(lbracket:, contents:, location:, comments: []) ⇒ ArrayLiteral

Returns a new instance of ArrayLiteral.



813
814
815
816
817
818
# File 'lib/syntax_tree/node.rb', line 813

def initialize(lbracket:, contents:, location:, comments: [])
  @lbracket = lbracket
  @contents = contents
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



811
812
813
# File 'lib/syntax_tree/node.rb', line 811

def comments
  @comments
end

#contentsObject (readonly)

nil | Args

the contents of the array



808
809
810
# File 'lib/syntax_tree/node.rb', line 808

def contents
  @contents
end

#lbracketObject (readonly)

LBracket

the bracket that opens this array



805
806
807
# File 'lib/syntax_tree/node.rb', line 805

def lbracket
  @lbracket
end

Instance Method Details

#accept(visitor) ⇒ Object



820
821
822
# File 'lib/syntax_tree/node.rb', line 820

def accept(visitor)
  visitor.visit_array(self)
end

#child_nodesObject Also known as: deconstruct



824
825
826
# File 'lib/syntax_tree/node.rb', line 824

def child_nodes
  [lbracket, contents]
end

#deconstruct_keys(keys) ⇒ Object



830
831
832
833
834
835
836
837
# File 'lib/syntax_tree/node.rb', line 830

def deconstruct_keys(keys)
  {
    lbracket: lbracket,
    contents: contents,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
# File 'lib/syntax_tree/node.rb', line 839

def format(q)
  if qwords?
    QWordsFormatter.new(contents).format(q)
    return
  end

  if qsymbols?
    QSymbolsFormatter.new(contents).format(q)
    return
  end

  if var_refs?(q)
    VarRefsFormatter.new(contents).format(q)
    return
  end

  q.group do
    q.format(lbracket)

    if contents
      q.indent do
        q.breakable("")
        q.format(contents)
      end
    end

    q.breakable("")
    q.text("]")
  end
end