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, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

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

Returns a new instance of ArrayLiteral.



1153
1154
1155
1156
1157
1158
# File 'lib/syntax_tree/node.rb', line 1153

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



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

def comments
  @comments
end

#contentsObject (readonly)

nil | Args

the contents of the array



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

def contents
  @contents
end

#lbracketObject (readonly)

nil | LBracket | QSymbolsBeg | QWordsBeg | SymbolsBeg | WordsBeg

the

bracket that opens this array



1145
1146
1147
# File 'lib/syntax_tree/node.rb', line 1145

def lbracket
  @lbracket
end

Instance Method Details

#===(other) ⇒ Object



1229
1230
1231
1232
# File 'lib/syntax_tree/node.rb', line 1229

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

#accept(visitor) ⇒ Object



1160
1161
1162
# File 'lib/syntax_tree/node.rb', line 1160

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [lbracket, contents]
end

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



1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
# 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_keys(_keys) ⇒ Object



1182
1183
1184
1185
1186
1187
1188
1189
# File 'lib/syntax_tree/node.rb', line 1182

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

#format(q) ⇒ Object



1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
# 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