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, VarRefsFormatter

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

Returns a new instance of ArrayLiteral.



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

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



1168
1169
1170
# File 'lib/syntax_tree/node.rb', line 1168

def comments
  @comments
end

#contentsObject (readonly)

nil | Args

the contents of the array



1165
1166
1167
# File 'lib/syntax_tree/node.rb', line 1165

def contents
  @contents
end

#lbracketObject (readonly)

LBracket

the bracket that opens this array



1162
1163
1164
# File 'lib/syntax_tree/node.rb', line 1162

def lbracket
  @lbracket
end

Instance Method Details

#===(other) ⇒ Object



1245
1246
1247
1248
# File 'lib/syntax_tree/node.rb', line 1245

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

#accept(visitor) ⇒ Object



1177
1178
1179
# File 'lib/syntax_tree/node.rb', line 1177

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

#child_nodesObject Also known as: deconstruct



1181
1182
1183
# File 'lib/syntax_tree/node.rb', line 1181

def child_nodes
  [lbracket, contents]
end

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



1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
# File 'lib/syntax_tree/node.rb', line 1185

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



1199
1200
1201
1202
1203
1204
1205
1206
# File 'lib/syntax_tree/node.rb', line 1199

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

#format(q) ⇒ Object



1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
# File 'lib/syntax_tree/node.rb', line 1208

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

  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