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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ArrayLiteral.



1034
1035
1036
1037
1038
1039
# File 'lib/syntax_tree/node.rb', line 1034

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



1032
1033
1034
# File 'lib/syntax_tree/node.rb', line 1032

def comments
  @comments
end

#contentsObject (readonly)

nil | Args

the contents of the array



1026
1027
1028
# File 'lib/syntax_tree/node.rb', line 1026

def contents
  @contents
end

#lbracketObject (readonly)

LBracket

the bracket that opens this array



1023
1024
1025
# File 'lib/syntax_tree/node.rb', line 1023

def lbracket
  @lbracket
end

#locationObject (readonly)

Location

the location of this node



1029
1030
1031
# File 'lib/syntax_tree/node.rb', line 1029

def location
  @location
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



1041
1042
1043
# File 'lib/syntax_tree/node.rb', line 1041

def child_nodes
  [lbracket, contents]
end

#deconstruct_keys(keys) ⇒ Object



1047
1048
1049
1050
1051
1052
1053
1054
# File 'lib/syntax_tree/node.rb', line 1047

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

#format(q) ⇒ Object



1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
# File 'lib/syntax_tree/node.rb', line 1056

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



1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
# File 'lib/syntax_tree/node.rb', line 1087

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



1098
1099
1100
1101
1102
# File 'lib/syntax_tree/node.rb', line 1098

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