Class: SyntaxTree::ArrayLiteral
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
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#contents ⇒ Object
readonly
- nil | Args
-
the contents of the array.
-
#lbracket ⇒ Object
readonly
- nil | LBracket | QSymbolsBeg | QWordsBeg | SymbolsBeg | WordsBeg
-
the bracket that opens this array.
Attributes inherited from Node
Instance Method Summary collapse
- #===(other) ⇒ Object
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #copy(lbracket: nil, contents: nil, location: nil) ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(lbracket:, contents:, location:) ⇒ ArrayLiteral
constructor
A new instance of ArrayLiteral.
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.
1152 1153 1154 1155 1156 1157 |
# File 'lib/syntax_tree/node.rb', line 1152 def initialize(lbracket:, contents:, location:) @lbracket = lbracket @contents = contents @location = location @comments = [] end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
1150 1151 1152 |
# File 'lib/syntax_tree/node.rb', line 1150 def comments @comments end |
#contents ⇒ Object (readonly)
- nil | Args
-
the contents of the array
1147 1148 1149 |
# File 'lib/syntax_tree/node.rb', line 1147 def contents @contents end |
#lbracket ⇒ Object (readonly)
- nil | LBracket | QSymbolsBeg | QWordsBeg | SymbolsBeg | WordsBeg
-
the
bracket that opens this array
1144 1145 1146 |
# File 'lib/syntax_tree/node.rb', line 1144 def lbracket @lbracket end |
Instance Method Details
#===(other) ⇒ Object
1225 1226 1227 1228 |
# File 'lib/syntax_tree/node.rb', line 1225 def ===(other) other.is_a?(ArrayLiteral) && lbracket === other.lbracket && contents === other.contents end |
#accept(visitor) ⇒ Object
1159 1160 1161 |
# File 'lib/syntax_tree/node.rb', line 1159 def accept(visitor) visitor.visit_array(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
1163 1164 1165 |
# File 'lib/syntax_tree/node.rb', line 1163 def child_nodes [lbracket, contents] end |
#copy(lbracket: nil, contents: nil, location: nil) ⇒ Object
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 |
# File 'lib/syntax_tree/node.rb', line 1167 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
1181 1182 1183 1184 1185 1186 1187 1188 |
# File 'lib/syntax_tree/node.rb', line 1181 def deconstruct_keys(_keys) { lbracket: lbracket, contents: contents, location: location, comments: comments } end |
#format(q) ⇒ Object
1190 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 |
# File 'lib/syntax_tree/node.rb', line 1190 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 |