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

Constructor Details

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

Returns a new instance of ArrayLiteral.



998
999
1000
1001
1002
1003
# File 'lib/syntax_tree/node.rb', line 998

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



996
997
998
# File 'lib/syntax_tree/node.rb', line 996

def comments
  @comments
end

#contentsObject (readonly)

nil | Args

the contents of the array



993
994
995
# File 'lib/syntax_tree/node.rb', line 993

def contents
  @contents
end

#lbracketObject (readonly)

LBracket

the bracket that opens this array



990
991
992
# File 'lib/syntax_tree/node.rb', line 990

def lbracket
  @lbracket
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



1005
1006
1007
# File 'lib/syntax_tree/node.rb', line 1005

def child_nodes
  [lbracket, contents]
end

#deconstruct_keys(keys) ⇒ Object



1011
1012
1013
1014
1015
1016
1017
1018
# File 'lib/syntax_tree/node.rb', line 1011

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

#format(q) ⇒ Object



1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
# File 'lib/syntax_tree/node.rb', line 1020

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

#pretty_print(q) ⇒ Object



1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
# File 'lib/syntax_tree/node.rb', line 1051

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("array")

    q.breakable
    q.pp(contents)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



1062
1063
1064
1065
1066
# File 'lib/syntax_tree/node.rb', line 1062

def to_json(*opts)
  { type: :array, cnts: contents, loc: location, cmts: comments }.to_json(
    *opts
  )
end