class SyntaxTree::ArrayLiteral

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

[]
[one, two, three]

Constants

BREAKABLE_SPACE_SEPARATOR

Attributes

comments[R]
Array[ Comment | EmbDoc ]

the comments attached to this node

contents[R]
nil | Args

the contents of the array

lbracket[R]
nil | LBracket | QSymbolsBeg | QWordsBeg | SymbolsBeg | WordsBeg

the

bracket that opens this array

Public Class Methods

new(lbracket:, contents:, location:) click to toggle source
# File lib/syntax_tree/node.rb, line 1153
def initialize(lbracket:, contents:, location:)
  @lbracket = lbracket
  @contents = contents
  @location = location
  @comments = []
end

Public Instance Methods

===(other) click to toggle source
# File lib/syntax_tree/node.rb, line 1229
def ===(other)
  other.is_a?(ArrayLiteral) && lbracket === other.lbracket &&
    contents === other.contents
end
accept(visitor) click to toggle source
# File lib/syntax_tree/node.rb, line 1160
def accept(visitor)
  visitor.visit_array(self)
end
child_nodes() click to toggle source
# File lib/syntax_tree/node.rb, line 1164
def child_nodes
  [lbracket, contents]
end
Also aliased as: deconstruct
copy(lbracket: nil, contents: nil, location: nil) click to toggle source
# File lib/syntax_tree/node.rb, line 1168
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()
Alias for: child_nodes
deconstruct_keys(_keys) click to toggle source
# File lib/syntax_tree/node.rb, line 1182
def deconstruct_keys(_keys)
  {
    lbracket: lbracket,
    contents: contents,
    location: location,
    comments: comments
  }
end
format(q) click to toggle source
# File lib/syntax_tree/node.rb, line 1191
def format(q)
  lbracket = self.lbracket
  contents = self.contents

  if lbracket.is_a?(LBracket) && 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