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: BreakableSpaceSeparator, EmptyWithCommentsFormatter, QSymbolsFormatter, QWordsFormatter

Constant Summary collapse

BREAKABLE_SPACE_SEPARATOR =
BreakableSpaceSeparator.new.freeze

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(lbracket:, contents:, location:) ⇒ ArrayLiteral



1141
1142
1143
1144
1145
1146
# File 'lib/syntax_tree/node.rb', line 1141

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



1139
1140
1141
# File 'lib/syntax_tree/node.rb', line 1139

def comments
  @comments
end

#contentsObject (readonly)

nil | Args

the contents of the array



1136
1137
1138
# File 'lib/syntax_tree/node.rb', line 1136

def contents
  @contents
end

#lbracketObject (readonly)

LBracket

the bracket that opens this array



1133
1134
1135
# File 'lib/syntax_tree/node.rb', line 1133

def lbracket
  @lbracket
end

Instance Method Details

#===(other) ⇒ Object



1214
1215
1216
1217
# File 'lib/syntax_tree/node.rb', line 1214

def ===(other)
  other.is_a?(ArrayLiteral) && lbracket === other.lbracket &&
    contents === other.contents
end

#accept(visitor) ⇒ Object



1148
1149
1150
# File 'lib/syntax_tree/node.rb', line 1148

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

#child_nodesObject Also known as: deconstruct



1152
1153
1154
# File 'lib/syntax_tree/node.rb', line 1152

def child_nodes
  [lbracket, contents]
end

#copy(lbracket: nil, contents: nil, location: nil) ⇒ Object



1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
# File 'lib/syntax_tree/node.rb', line 1156

def copy(lbracket: nil, contents: nil, location: nil)
  node =
    ArrayLiteral.new(
      lbracket: lbracket || self.lbracket,
      contents: contents || self.contents,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



1170
1171
1172
1173
1174
1175
1176
1177
# File 'lib/syntax_tree/node.rb', line 1170

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

#format(q) ⇒ Object



1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
# File 'lib/syntax_tree/node.rb', line 1179

def format(q)
  if lbracket.comments.empty? && contents && contents.comments.empty? &&
       contents.parts.length > 1
    if qwords?
      QWordsFormatter.new(contents).format(q)
      return
    end

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

  if empty_with_comments?
    EmptyWithCommentsFormatter.new(lbracket).format(q)
    return
  end

  q.group do
    q.format(lbracket)

    if contents
      q.indent do
        q.breakable_empty
        q.format(contents)
        q.if_break { q.text(",") } if q.trailing_comma?
      end
    end

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